0
這是我如何創建我的AES加密,但我仍然不斷收到BadPaddingException的這個錯誤解密javax.crypto.BadPaddingException
// Get the key generator
KeyGenerator kg = KeyGenerator.getInstance("AES");
kg.init(128);
SecretKey sk = kg.generateKey();
byte[] iv = new byte[]{0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f};
AlgorithmParameterSpec paramSpec = new IvParameterSpec(iv);
ecipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
dcipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
ecipher.init(Cipher.ENCRYPT_MODE, sk,paramSpec);
dcipher.init(Cipher.DECRYPT_MODE, sk,paramSpec);
解密方法的代碼
public String decr(String str) {
try {
byte[] dec = new sun.misc.BASE64Decoder().decodeBuffer(str);
byte[] utf8 = dcipher.doFinal(dec);
return new String(utf8, "UTF8");
} catch (Exception ex) {
return null;
}
}
加密的doFinal()
塊方法
public String encr(String str) {
try {
byte[] utf8 = str.getBytes("UTF8");
byte[] enc = ecipher.doFinal(utf8);
return new sun.misc.BASE64Encoder().encode(enc);
} catch (Exception ex) {
return null;
}
}
你如何進行加密? – axtavt 2011-04-06 18:05:17
你傳遞給encr然後decr的字符串是什麼?我在本地測試了你的代碼,它工作得很好 – 2011-04-06 18:18:24
@Justin - 我的字符串是「Hello world」。 – yogsma 2011-04-06 18:39:53