首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >代码在Windows 7上工作,而在Linux 7上不工作

代码在Windows 7上工作,而在Linux 7上不工作
EN

Stack Overflow用户
提问于 2013-04-16 08:18:09
回答 2查看 400关注 0票数 1

下面的代码在Windows的JDK 7中运行得很好,但是在Linux上失败了:javax.crypto.IllegalBlockSizeException: Input length must be multiple of 8 when decrypting with padded cipherCipher.doFinal(ciphertextArray)行--这是使用完全相同的Jar文件和完全相同的命令行,等等。但是,文本和密码的值来自命令行,我怀疑问题就在这里,我只是不知道.在哪里

代码语言:javascript
复制
String saltD = text.substring(0,12);
String ciphertext = text.substring(12,text.length());

// BASE64Decode the bytes for the salt and the ciphertext
Base64 decoder = new Base64();
byte[] saltArray = decoder.decode(saltD);
byte[] ciphertextArray = decoder.decode(ciphertext);

// Create the PBEKeySpec with the given password
PBEKeySpec keySpec = new PBEKeySpec(password.trim().toCharArray());

// Get a SecretKeyFactory for PBEWithSHAAndTwofish
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(encryptionMethod);

// Create our key
SecretKey key = keyFactory.generateSecret(keySpec);

// Now create a parameter spec for our salt and iterations
PBEParameterSpec paramSpec = new PBEParameterSpec(saltArray, ITERATIONS);

// Create a cipher and initialize it for encrypting
Cipher cipher = Cipher.getInstance(encryptionMethod);
cipher.init(Cipher.DECRYPT_MODE, key, paramSpec);

// Perform the actual decryption
byte[] plaintextArray = cipher.doFinal(ciphertextArray);
return new String(plaintextArray);
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-04-17 03:00:06

问题是,文本字符串中包含"$“字符,而在Linux中的命令行中,这些字符是转义字符。它们需要在字符串本身中转换为"\$“。

票数 0
EN

Stack Overflow用户

发布于 2013-04-16 09:09:02

这一观察似乎是由于两个平台上默认字符集的差异造成的。

您需要确保Stringbyte[]转换(反之亦然)是使用指定的字符集执行的,而不是依赖于平台默认值。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/16032002

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档