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;
    
        }
    

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

    世界上所有的不公平都是由于当事人能力不足造成的.
  • 相关阅读:
    python练习:抓取统计log内ip数量
    再来说一说sudo
    Nginx 1 Web Server Implementation Cookbook系列--(1)debug mode
    MySQL 5.7 关闭严格模式
    beanstalkd 消息队列
    国内git项目托管平台
    Git忽略.gitignore规则不生效的解决办法
    Ubuntu 上搭建 Samba 服务器
    Memcached+PHP+Mysql+Linux 实践
    php use memcached in ubuntu 14.04
  • 原文地址:https://www.cnblogs.com/javayida/p/13346810.html
Copyright © 2011-2022 走看看