可能重复:
Java AES Encrypt Entire String
我遇到了一个小问题。由于某些原因,我无法使用与加密字符串相同的方法解密某些字符串。例如,我使用这个代码解密:
SecretKeySpec key = new SecretKeySpec(salt.getBytes(), "AES");
Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding", "SunJCE");
cipher.init(Cipher.DECRYPT_MODE, key);
String result = new String(cipher.doFinal(message));
System.out.println("Decrypted:" + result);总之,当盐是"1231231231231231“,而我试图解密的信息是”读“。我知道这个错误:
Exception in thread "main" javax.crypto.BadPaddingException: Given final block not properly padded但加密器(其工作方式相同)表示加密的值是
I¡?Þd↨Ú#à, 7êLO* 我如何解决这个问题,或者避免用户输入这样的字符串?坦斯克
发布于 2011-10-16 07:04:00
解决方案:不要将其存储在字符串中。
对Cipher.doFinal的调用应该存储在byte数组(byte[])中,解密的输入也应该是byte数组。如果您接受String输入,请使用.getBytes();在为输出创建String时,使用new String(myArray)
https://stackoverflow.com/questions/7782574
复制相似问题