以下代码在 Windows 上的 Oracle 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);