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

后端 未结 1 841
伪装坚强ぢ
伪装坚强ぢ 2021-02-06 07:52
public class AES {

    public String getEncrypt(String pass){
        String password = encrypt(pass);
        return password;
    }

    public String getDecrypt(Stri         


        
1条回答
  •  再見小時候
    2021-02-06 08:29

    You are mixing Strings and byte arrays. That is not always a good thing to do. At the very least specify what charset you are using for the byte to char conversion. Even then it is not 100% safe. Better to treat strings as strings and byte arrays as byte arrays.

    If that does not solve it then there are many things that can cause a "Bad Padding" error. Basically anything that causes the end of the last block not to match the expected padding will throw the error. Possible causes include: incorrect padding setting, incorrect key, corrupted cyphertext and others.

    To try and diagnose the problem, set the decryption side to NoPadding. This will accept anything, and allow you to examine the output:

    • complete garbage: you probably have an error in the key or different mode settings.

    • first block garbage: you may have a key error or an IV error.

    • last block garbage: likely a corrupt end to the cyphertext file.

    • a correct decryption with some strange bytes at the end: the strange bytes are the padding.

    If it really is just the padding, then set the decryption function to expect that sort of padding. Otherwise check that the key/IV/cyphertext is byte-for-byte the same for both encryption and decryption.

    It is vital that you set a padding mode after diagnosis. NoPadding is insecure.

    0 讨论(0)
提交回复
热议问题