javax.crypto.BadPaddingException: Given final block not properly padded解决方案
来源:互联网 发布:淘宝牛排5大品牌 编辑:程序博客网 时间:2024/05/16 06:51
JAVA的AES加密解密在windows上测试一切正常,上传到空间上在解密时就出现错误。空间是Linux系统
查看日志发现出现此异常
javax.crypto.BadPaddingException: Given final block not properly padded
后面百度了一下终于解决了,在生成key的时候出现错误的
原来的代码:
private Key initKeyForAES(String key) throws NoSuchAlgorithmException { if (null == key || key.length() == 0) { throw new NullPointerException("key not is null"); } SecretKeySpec key2 = null;try { KeyGenerator kgen = KeyGenerator.getInstance("AES"); kgen.init(128, new SecureRandom(key.getBytes())); SecretKey secretKey = kgen.generateKey(); byte[] enCodeFormat = secretKey.getEncoded(); key2 = new SecretKeySpec(enCodeFormat, "AES"); } catch (NoSuchAlgorithmException ex) { throw new NoSuchAlgorithmException(); } return key2; }
主要是红色部分的问题
修改后代码:
private Key initKeyForAES(String key) throws NoSuchAlgorithmException { if (null == key || key.length() == 0) { throw new NullPointerException("key not is null"); } SecretKeySpec key2 = null; SecureRandom random = SecureRandom.getInstance("SHA1PRNG"); random.setSeed(key.getBytes()); try { KeyGenerator kgen = KeyGenerator.getInstance("AES"); kgen.init(128, random); SecretKey secretKey = kgen.generateKey(); byte[] enCodeFormat = secretKey.getEncoded(); key2 = new SecretKeySpec(enCodeFormat, "AES"); } catch (NoSuchAlgorithmException ex) { throw new NoSuchAlgorithmException(); } return key2; }
其实就是SecureRandom创建的方式不同而引起的错误,具体原理我也不懂,因为加密解密代码都是网上搜的,具体没研究过这个。总之能解决问题就好。
我是从这找到解决方案的:http://wenku.baidu.com/link?url=wOibKHENi2Z5gFOL5prjGBE8RES1dZEZlrvfY1NTl89QJWtTwXUNLmgEXVYWGBGXR25oRvOKPJTI5M3o95KW0yIHwgFVEnJiZt1-0YvRQua
随心地记录一些东西,以备不时之需。
阅读全文
0 0
- javax.crypto.BadPaddingException: Given final block not properly padded解决方案
- IBMJCE AND SunJCE(javax.crypto.BadPaddingException:Given final block not properly padded)
- 使用Java 对称加密(AES),提示"javax.crypto.BadPaddingException: Given final block not properly padded"
- Linux下运行java DES解密失败,报javax.crypto.BadPaddingException:Given final block not properly padded
- Linux下运行java DES解密失败,报javax.crypto.BadPaddingException:Given final block not properly padded
- Linux下运行java DES解密失败,报javax.crypto.BadPaddingException:Given final block not properly padded
- Linux下运行java DES解密失败,报javax.crypto.BadPaddingException:Given final block not properly padded
- Linux下运行java DES解密失败,报javax.crypto.BadPaddingException:Given final block not properly padded
- javax.crypto.BadPaddingException: Given final block not
- Given final block not properly padded
- Given final block not properly padded
- given final block not properly padded aes
- AES解密 Given Final Block Not Properly Padded
- DESUtils 加解密时 Given final block not properly padded bug小记
- java DES 加密解密算法及报错 Given final block not properly padded
- 关于 android AES 部分机器 javax.crypto.BadPaddingException: pad block corrupted
- javax.crypto.BadPaddingException: error:0407106B:rsa routines:RSA_padding_check_PKCS1_type_2:block t
- AES加解密的错误提示 javax.crypto.BadPaddingException: pad block corrupted
- web图片上传和数据库取出Blob传到页面显示图片
- Spring源码阅读-- 解析自定义命名空间的标签
- 【SpringBoot】Hello SpringBoot
- 神坑的水题
- 排序算法
- javax.crypto.BadPaddingException: Given final block not properly padded解决方案
- 搭建hadoop
- OpenCV Error: Assertion failed (dsize.area() > 0) in cv::resize
- Spring注解详解
- mysql存储引擎
- 文章标题
- 20170927_快排应用_荷兰国旗问题
- 【51Nod1711】平均数
- 官网下载Spring的jar教程