记IBM JDK1.7 兼容 SUN JDK AES 解密
作者:互联网
开发环境: SUN JDK 1.7
生产环境: IBM JDK 1.7
问题出现原因:
接收第三方系统AES加密内容,使用IBM JDK解密的时候,报Given final block not properly padded
//注册SUN Provider 进行解密
sun.security.provider.Sun sunProvider = new sun.security.provider.Sun();
//注册SHA1PRNG 算法,否则报no such algorithm: SHA1PRNG for provider SUN
sunProvider.put("SecureRandom.SHA1PRNG", "sun.security.provider.SecureRandom");
Security.addProvider(sunProvider);
//进行解密调用
KeyGenerator kgen = KeyGenerator.getInstance("AES");
//使用SUN 进行解密算法
SecureRandom secureRandom = SecureRandom.getInstance("SHA1PRNG","SUN");
System.out.println(secureRandom.getProvider());
// 防止linux下 随机生成key
secureRandom.setSeed(password.getBytes("UTF-8"));
kgen.init(128, secureRandom);
SecretKey secretKey = kgen.generateKey();
byte[] enCodeFormat = secretKey.getEncoded();
SecretKeySpec key = new SecretKeySpec(enCodeFormat, "AES");
Cipher cipher = Cipher.getInstance("AES");// 创建密码器
cipher.init(Cipher.DECRYPT_MODE, key);// 初始化
byte[] result = cipher.doFinal(content);
//IBM JDK 1.7 将sun.security.provider.SunEntries.putEntries(Map<Object, Object>) 加密算法全部注释掉了 导致必须手动去注册SHA1PRNG算法. //SUN JDK 1.7 楼主的解决办法是,直接去SUN JDK sun.security.provider.SunEntries.putEntries(Map<Object, Object>)方法中,找到对应的加密算法,手动注册即可: sunProvider.put("SecureRandom.SHA1PRNG", "sun.security.provider.SecureRandom");
目前测试下来,只有IBM JKD 1.7 会存在该问题,IBM JDK 1.8 不存在该问题,可以直接使用
标签:AES,IBM,JDK,SUN,provider,sun,security,SecureRandom 来源: https://blog.csdn.net/kemoing/article/details/122602952