javax.crypto.BadPaddingException: Given final block not properly padded solution javax.crypto.BadPaddingException: Given final block not properly padded solution

JAVA's AES encryption and decryption are tested on windows and everything is normal, but an error occurs when uploading to the space and decrypting. space is a linux system

Check the log and find this exception

  javax.crypto.BadPaddingException: Given final block not properly padded

Later, Baidu finally solved it, and there was an error when generating the key.

Original code:

copy code
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;

    }
copy code

Mainly the red part of the problem

Modified code:

copy code
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;

    }
copy code

In fact, it is the error caused by the different ways of creating SecureRandom. I don't understand the specific principle, because the encryption and decryption codes are all searched on the Internet, and I have not studied this specifically. Anyway, it can solve the problem.

I found the solution from here: http://wenku.baidu.com/link?url=wOibKHENi2Z5gFOL5prjGBE8RES1dZEZlrvfY1NTl89QJWtTwXUNLmgEXVYWGBGXR25oRvOKPJTI5M3o95KW0yIHwgFVEnJiZt1-0YvRQua

Write something down as you like, just in case you need it.

JAVA's AES encryption and decryption are tested on windows and everything is normal, but an error occurs when uploading to the space and decrypting. space is a linux system

Check the log and find this exception

  javax.crypto.BadPaddingException: Given final block not properly padded

Later, Baidu finally solved it, and there was an error when generating the key.

Original code:

copy code
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;

    }
copy code

Mainly the red part of the problem

Modified code:

copy code
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;

    }
copy code

In fact, it is the error caused by the different ways of creating SecureRandom. I don't understand the specific principle, because the encryption and decryption codes are all searched on the Internet, and I have not studied this specifically. Anyway, it can solve the problem.

I found the solution from here: http://wenku.baidu.com/link?url=wOibKHENi2Z5gFOL5prjGBE8RES1dZEZlrvfY1NTl89QJWtTwXUNLmgEXVYWGBGXR25oRvOKPJTI5M3o95KW0yIHwgFVEnJiZt1-0YvRQua

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324978627&siteId=291194637