下面的代码在Windows的JDK 7中运行得很好,但是在Linux上失败了:javax.crypto.IllegalBlockSizeException: Input length must be multiple of 8 when decrypting with padded cipher在Cipher.doFinal(ciphertextArray)行--这是使用完全相同的Jar文件和完全相同的命令行,等等。但是,文本和密码的值来自命令行,我怀疑问题就在这里,我只是不知道.在哪里
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);发布于 2013-04-17 03:00:06
问题是,文本字符串中包含"$“字符,而在Linux中的命令行中,这些字符是转义字符。它们需要在字符串本身中转换为"\$“。
发布于 2013-04-16 09:09:02
这一观察似乎是由于两个平台上默认字符集的差异造成的。
您需要确保String到byte[]转换(反之亦然)是使用指定的字符集执行的,而不是依赖于平台默认值。
https://stackoverflow.com/questions/16032002
复制相似问题