遇到的问题:
1.javax.crypto.BadPaddingException: Given final block not properly padded
加密的密钥和解密的密钥不一致抛出该异常。
引用Stack Overflow:
If you try to decrypt PKCS5-padded data with the wrong key, and then unpad it (which is done by the Cipher class automatically), you most likely will get the BadPaddingException (with probably of slightly less than 255/256, around 99.61%), because the padding has a special structure which is validated during unpad and very few keys would produce a valid padding.
So, if you get this exception, catch it and treat it as “wrong key”.
2.含中文的数据加密后,进行解密会乱码
在解密的方法字节数组转字符串方法中,指明编码方式为UTF-8
3.在Windows系统中,上述代码没有问题。在linux下,报错javax.crypto.BadPaddingException,报错原因见https://blog.csdn.net/qq_23888451/article/details/94719836,即修改加密方法中kgen.init(128, new SecureRandom(key.getBytes()));这行代码为
SecureRandom random = SecureRandom.getInstance("SHA1PRNG");
random.setSeed(key.getBytes());
kgen.init(128, random);