【问题标题】:Spring Security Crypto Module - BadPaddingException: Given final block not properly paddedSpring Security Crypto 模块 - BadPaddingException:给定最终块未正确填充
【发布时间】:2018-09-26 06:44:22
【问题描述】:

我正在尝试制作一个加密工具,我想加密字符串,保存在数据库中然后解密,我正在使用 TextEncryptor 可查询的弹簧安全加密模块,因为我想用于 rest apiKey,但我无法制作它工作。

这是我的代码:

        import org.springframework.security.crypto.encrypt.Encryptors;
        import org.springframework.security.crypto.encrypt.TextEncryptor;
        import org.springframework.security.crypto.keygen.KeyGenerators;

        public class CryptoUtil {

            public static String encrypt(String plain, String password) {
                String salt = KeyGenerators.string().generateKey();
                TextEncryptor textEncryptor = Encryptors.queryableText(password, salt);
                return textEncryptor.encrypt(plain);
            }

            public static String decrypt(String encrypted, String password) {
                String salt = KeyGenerators.string().generateKey();
                TextEncryptor textEncryptor = Encryptors.queryableText(password, salt);
                return textEncryptor.decrypt(encrypted);
            }
        }

    ----------------------------------------------------    

    public static void main(String[] args) {
        String password = "password";
        String plain = "hello";

        String encrypted = CryptoUtil.encrypt(plain,password);`enter code here`
        String decrypted = CryptoUtil.decrypt(encrypted, password);
    }

    ----------------------------------------------------

    Exception in thread "main" java.lang.IllegalStateException: Unable to invoke Cipher due to bad padding
            at org.springframework.security.crypto.encrypt.CipherUtils.doFinal(CipherUtils.java:142)
            at org.springframework.security.crypto.encrypt.AesBytesEncryptor.decrypt(AesBytesEncryptor.java:128)
            at org.springframework.security.crypto.encrypt.HexEncodingTextEncryptor.decrypt(HexEncodingTextEncryptor.java:40)
            at com.ind.app.util.CryptoUtil.decrypt(CryptoUtil.java:18)
            at com.ind.app.Test.main(UsuarioTest.java:11)
            Caused by: javax.crypto.BadPaddingException: Given final block not properly padded
                at com.sun.crypto.provider.CipherCore.doFinal(CipherCore.java:966)
                at com.sun.crypto.provider.CipherCore.doFinal(CipherCore.java:824)
                at com.sun.crypto.provider.AESCipher.engineDoFinal(AESCipher.java:436)
                at javax.crypto.Cipher.doFinal(Cipher.java:2165)
                at org.springframework.security.crypto.encrypt.CipherUtils.doFinal(CipherUtils.java:135)
                ... 4 more

【问题讨论】:

标签: java encryption spring-security


【解决方案1】:

致有同样问题的人:

String salt = KeyGenerators.string().generateKey();

根据文档每次调用此行:

创建一个对 8 字节长的 SecureRandom 密钥进行十六进制编码的 StringKeyGenerator。十六进制编码的字符串是 keyLength * 2 个字符的长度。

所以盐是随机的,例如: 普通:“你好” 密码:“密码” :“e606bfd5cf9f198e”

加密:“60e0e953841ca708b74ac657735a2236076f0a614ec85548d163fadf91e2be8f”

然后,当我尝试解密并获取明文时,该方法会生成另一个(随机)盐,因此 TextEncryptor.decrypt(String encrypted) 无法正确解密,因为盐不是一样。

Encryptors.queryableText(CharSequence 密码,CharSequence salt)

为使用标准的可查询文本字符串创建加密器 基于密码的加密。使用 16 字节全零初始化 向量,因此加密相同的数据会导致相同的加密 结果。这样做是为了允许查询加密数据。 加密文本是十六进制编码的。

CryptoUtil 用于加密和解密纯字符串,不推荐用于密码,但对例如 api 密钥很有用。

public class CryptoUtil {

private static final String salt = "e606bfd5cf9f198e"; //any random generated salt

        public static String encrypt(String plain, String password) {
            TextEncryptor textEncryptor = Encryptors.queryableText(password, salt);
            return textEncryptor.encrypt(plain);
        }

        public static String decrypt(String encrypted, String password) {
            TextEncryptor textEncryptor = Encryptors.queryableText(password, salt);
            return textEncryptor.decrypt(encrypted);
        }
    }

【讨论】:

    猜你喜欢
    • 2016-11-08
    • 2015-07-01
    • 2014-05-29
    • 1970-01-01
    • 2011-12-24
    • 2017-04-23
    • 1970-01-01
    • 2011-05-16
    • 1970-01-01
    相关资源
    最近更新 更多