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.doFinal(CipherCore.java:991)
at com.sun.crypto.provider.CipherCore.doFinal(CipherCore.java:847)
at com.sun.crypto.provider.AESCipher.engineDoFinal(AESCipher.java:446)
at javax.crypto.Cipher.doFinal(Cipher.java:2164)
at com.jugan.utils.AesUtil.decrypt(AesUtil.java:62)
at com.jugan.utils.AuthUtil.getAesPropertyValue(AuthUtil.java:48)
at com.jugan.utils.AuthUtil.initApiClient(AuthUtil.java:25)
at com.jugan.utils.NbIotDeviceUtil.startRefreshTokenTimer(NbIotDeviceUtil.java:21)
at com.jugan.JuganApplication$MyRunner.run(JuganApplication.java:24)
at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:800)
at org.springframework.boot.SpringApplication.callRunners(SpringApplication.java:784)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:338)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1258)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1246)
at com.jugan.JuganApplication.main(JuganApplication.java:17)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.springframework.boot.loader.MainMethodRunner.run(MainMethodRunner.java:48)
at org.springframework.boot.loader.Launcher.launch(Launcher.java:87)
at org.springframework.boot.loader.Launcher.launch(Launcher.java:50)
at org.springframework.boot.loader.JarLauncher.main(JarLauncher.java:51)
2018-11-13 13:27:03.425 INFO 27033 --- [ main] ConditionEvaluationReportLoggingListener :
Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2018-11-13 13:27:03.431 ERROR 27033 --- [ main] o.s.boot.SpringApplication : Application run failed
JAVA的AES加密解密在windows上测试一切正常,上传到空间上在解密时就出现错误。空间是Linux系统
查看日志发现出现此异常
javax.crypto.BadPaddingException: Given final block not properly padded
解决方案:
/**
* 编码格式
*/
private static final String ENCODING = "UTF-8";
/**
* 加密算法
*/
public static final String KEY_ALGORITHM = "AES";
/**
* 签名算法
*/
public static final String SIGN_ALGORITHMS = "SHA1PRNG";
/**
* 解密AES加密过的字符串
*
* @param content AES加密过过的内容
* @param password 加密时的密码
* @return 明文
*/
public static byte[] decrypt(byte[] content, String password) {
try {
KeyGenerator kgen = KeyGenerator.getInstance(KEY_ALGORITHM);// 创建AES的Key生产者
// //防止linux下 随机生成key
SecureRandom random = SecureRandom.getInstance(SIGN_ALGORITHMS);
random.setSeed(password.getBytes(ENCODING));
kgen.init(128, random);
// kgen.init(128, new SecureRandom(password.getBytes()));
SecretKey secretKey = kgen.generateKey();// 根据用户密码,生成一个密钥
byte[] enCodeFormat = secretKey.getEncoded();// 返回基本编码格式的密钥
SecretKeySpec key = new SecretKeySpec(enCodeFormat, "AES");// 转换为AES专用密钥
Cipher cipher = Cipher.getInstance("AES");// 创建密码器
cipher.init(Cipher.DECRYPT_MODE, key);// 初始化为解密模式的密码器
byte[] result = cipher.doFinal(content);
return result; // 明文
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (NoSuchPaddingException e) {
e.printStackTrace();
} catch (InvalidKeyException e) {
e.printStackTrace();
} catch (IllegalBlockSizeException e) {
e.printStackTrace();
} catch (BadPaddingException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return null;
}
参考:https://www.cnblogs.com/Darlin356230410/p/8602674.html