簡體   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