本文主要是介绍javax.crypto.BadPaddingException: Given final block not properly padded. Suc,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
在在进行token加密解密的时候报错:
javax.crypto.BadPaddingException: Given final block not properly padded. Suc
然后百度一下:
对着这哥们的修改一波:
https://www.cnblogs.com/zempty/p/4318902.html
主要就是修改加密部分:
在linux上面 错误部分:
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;}
下面是我自己修改的,也是网上找的工具类,比较复杂…
这篇关于javax.crypto.BadPaddingException: Given final block not properly padded. Suc的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!