繁体   English   中英

春季安全加密模块-BadPaddingException:给定最终块未正确填充

[英]Spring Security Crypto Module - BadPaddingException: Given final block not properly padded

我正在尝试制作一个加密工具,我想加密字符串,保存在db中然后解密,我正在使用TextEncryptor可查询的春季安全加密模块,因为我想用于其余的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

对于有同样问题的任何人:

字符串盐= KeyGenerators.string()。generateKey();

根据文档,每次调用此行:

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

因此,盐是随机的,例如: plain :“ hello” 密码 :“ password” salt :“ e606bfd5cf9f198e”

加密 :“ 60e0e953841ca708b74ac657735a2236076f0a614ec85548d163fadf91e2be8f”

然后,当我尝试解密并得到普通密码时 ,该方法会生成另一个(随机)盐,因此TextEncryptor.decrypt(String encryption)不能正确解密,因为盐不相同。

Encryptors.queryableText(字符序列密码,字符序列盐)

为使用基于密码的标准加密的可查询文本字符串创建加密器。 使用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);
        }
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM