0
我加密一个字符串,并将其存储在客户端的cookie ..但是当我从js发送确切的加密字符串到java代码它给了我上述的例外。鉴于最后一块没有正确填充AES
我使用的加密和解密的代码是:
public static final String UNICODE_FORMAT = "UTF8";
public static String encrypt(String Data, SecretKeySpec skeySpec,IvParameterSpec ivspec) throws Exception {
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE, skeySpec, ivspec);
byte[] plainBytes = Data.getBytes(UNICODE_FORMAT);
byte[] encrypted = cipher.doFinal(plainBytes);
String encryption = bytesToString(encrypted);
return encryption;
}
public static String decrypt(String encryptedData,SecretKeySpec skeySpec,IvParameterSpec ivspec) throws Exception {
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
cipher.init(Cipher.DECRYPT_MODE, skeySpec, ivspec);
byte[] decryptval = hexToBytes(encryptedData);
byte[] decrypted = cipher.doFinal(decryptval);
return new String(decrypted);
}
public static String bytesToString(byte[] bytes) {
HexBinaryAdapter adapter = new HexBinaryAdapter();
String s = adapter.marshal(bytes);
return s;
}
public static byte[] hexToBytes(String hexString) {
HexBinaryAdapter adapter = new HexBinaryAdapter();
byte[] bytes = adapter.unmarshal(hexString);
return bytes;
}
你能告诉我的问题可能是什么?我已经尝试了在stackoverflow.com提及的解决方案,还有一些其他的解决方案,但没有工作..我得到这个错误,因为我发送加密的字符串到JS,它正在改变字符串的填充?
当你在服务器端运行加密/解密时它工作吗? – evgenyl 2013-04-08 06:18:12
是的,它确实工作,然后...问题是,只有当我从javascript – 2013-04-08 06:21:50
'新字符串(解密,UNICODE_FORMAT)'返回它,但不能是错误。 – 2013-04-08 06:22:45