zoukankan      html  css  js  c++  java
  • 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;
    
        }
    

    下面是我自己修改的,也是网上找的工具类,比较复杂…
    在这里插入图片描述

    世界上所有的不公平都是由于当事人能力不足造成的.
  • 相关阅读:
    POJ 1548 Robots(最小路径覆盖)
    <html>
    站点开发-日志-1
    JSP入门实战下
    rancher官方资源
    window10死机
    window10桌面图标空白
    sentry使用docker-compose部署
    docker下一步步部署sentry
    docker-compose编排服务
  • 原文地址:https://www.cnblogs.com/javayida/p/13346810.html
Copyright © 2011-2022 走看看