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;

    }

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

posted @ 2019-11-04 19:25  你就像甜甜的益达  阅读(1888)  评论(0编辑  收藏  举报