AES对称加密适配linux系统

编程

javax.crypto.BadPaddingException: Given final block not properly padded. Such issues can arise if a bad key is used during decryption.

at com.sun.crypto.provider.CipherCore.unpad(CipherCore.java:975)

at com.sun.crypto.provider.CipherCore.fillOutputBuffer(CipherCore.java:1056)

at com.sun.crypto.provider.CipherCore.doFinal(CipherCore.java:853)

at com.sun.crypto.provider.AESCipher.engineDoFinal(AESCipher.java:446)

经过翻阅资料发现,windows和linux对相同明文加密产生的结果是不同的;其次linux对明文解密过程中会抛出异常。

因此进行了如下修改:

修改前:

KeyGenerator kgen = KeyGenerator.getInstance("AES");// 创建AES的Key生产者

kgen.init(128, new SecureRandom(password.getBytes()));

修改后:

String charset = "utf-8";

KeyGenerator kgen = KeyGenerator.getInstance("AES");// 创建AES的Key生产者

SecureRandom secureRandom = SecureRandom.getInstance("SHA1PRNG") ;

secureRandom.setSeed(password.getBytes(charset));

kgen.init(128, secureRandom);

参考: https://blog.csdn.net/cainiao0589/article/details/101015485

https://blog.csdn.net/seapeak007/article/details/79747309

 

以上是 AES对称加密适配linux系统 的全部内容, 来源链接: utcz.com/z/513221.html

回到顶部