2013-04-16 36 views
1

下面的代碼精美的作品在甲骨文的JDK 7的Windows,但無法在Linux上以下錯誤:javax.crypto.IllegalBlockSizeException: Input length must be multiple of 8 when decrypting with padded cipher在該行Cipher.doFinal(ciphertextArray)這是使用帶有完全相同的Jar文件完全相同的命令行等文字和密碼的值是通過命令行雖然快到了,我懷疑的問題是這裏的某個地方,我只是不知道在哪裏...守則適用於Windows的JDK 7,但不是在Linux上JDK 7

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); 
+0

爲了確保它不是一個錯誤,我會運行這與每個的最新版本。我懷疑1.7.0_09-icedtea太舊了。 –

+0

所以,爲什麼不使用正在工作的那個;) – Abraham

+0

好吧,所以我只是在另一臺測試機器上安裝了相同的JDK,並且它排除了這一點。所以我調整了這個問題。但我現在記得閱讀某處需要對命令行參數做些什麼的事情,因爲Windows和Linux之間存在細微差別。我只是不記得什麼,何時何地或如何:( –

回答

0

問題在於文本字符串中包含「$」字符,而在Linux中的命令行中,這些字符都是轉義字符。它們需要在字符串本身中轉換爲「\ $」。

2

它似乎觀察結果是由於兩個平臺上的默認字符集不同所致。

您需要確保Stringbyte[]轉換(反之亦然)使用的是指定字符集,而不是依賴於平臺的默認值進行的。