我使用密碼加密和解密的消息:Android的密碼加密/解密
public String encrypt(String string) throws InvalidKeyException, IllegalBlockSizeException, BadPaddingException, UnsupportedEncodingException {
cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec);
byte[] stringBytes = string.getBytes("UTF-8");
byte[] encryptedBytes = cipher.doFinal(stringBytes);
return android.util.Base64.encodeToString(encryptedBytes, android.util.Base64.DEFAULT);
}
public String decrypt(String string) throws InvalidKeyException, IllegalBlockSizeException, BadPaddingException, UnsupportedEncodingException {
cipher.init(Cipher.DECRYPT_MODE, secretKeySpec);
byte[] stringBytes = android.util.Base64.decode(string.getBytes(), android.util.Base64.DEFAULT);
byte[] decryptedBytes = cipher.doFinal(stringBytes);
return new String(decryptedBytes,"UTF-8");
}
出於某種原因,雖然我用的Base64編碼和解碼字符串,我仍然得到這個錯誤:
javax.crypto.IllegalBlockSizeException: last block incomplete in decryption
我在做什麼錯?
編輯:
這是我的JSONObject - 我試圖解密 「M」:
{"m":"Cu7FR2be0E6ZP2BrZaLU2ZWQSfycNg0-fPibphTIZno\r\n"}
奇怪的是,該錯誤只出現在Android中。我的服務器是用Java編寫的,而且我使用的是Apache Base64編碼器,它工作的很好。
解密(「\ u0000的」)或某些Unicode字符將始終是 – 2012-12-24 15:11:03
的嘮叨我不明白:) –
這就是問題所在:) – 2012-12-24 15:55:00