java aes javax.crypto.BadPaddingException:给定的最终块未正确填充 [英] java aes javax.crypto.BadPaddingException: Given final block not properly padded

查看:21
本文介绍了java aes javax.crypto.BadPaddingException:给定的最终块未正确填充的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

public class AES {

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

    public String getDecrypt(String pass){
        String key = "AesSEcREtkeyABCD";
        byte[] passwordByte = decrypt(key,pass);
        String password = new String(passwordByte);
        return password;
    }

    private byte[] decrypt(String key, String encrypted) {
        try {
            SecretKeySpec skeySpec = new SecretKeySpec(key.getBytes(), "AES");
            Cipher cipher = Cipher.getInstance("AES");
            cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(skeySpec.getEncoded(), "AES"));
            //getting error here
            byte[] original = cipher.doFinal(encrypted.getBytes());
            return original;
        } catch (IllegalBlockSizeException ex) {
            ex.printStackTrace();
        } catch (BadPaddingException ex) {
            ex.printStackTrace();
        } catch (InvalidKeyException ex) {
            ex.printStackTrace();
        } catch (NoSuchAlgorithmException ex) {
            ex.printStackTrace();
        } catch (NoSuchPaddingException ex) {
            ex.printStackTrace();
        }
        return null;
    } 

    private String encrypt(String value) {
        try {
            byte[] raw = new byte[]{'A', 'e', 's', 'S', 'E', 'c', 'R', 'E', 't', 'k', 'e', 'y','A','B','C','D'};
            SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
            Cipher cipher = Cipher.getInstance("AES");
            cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
            byte[] encrypted = cipher.doFinal(value.getBytes());
            System.out.println("encrypted string:" + (new String(encrypted)));
            return new String(encrypted);
        } catch (NoSuchAlgorithmException ex) {
            ex.printStackTrace();
        } catch (IllegalBlockSizeException ex) {
            ex.printStackTrace();       
        } catch (BadPaddingException ex) {
            ex.printStackTrace();
        } catch (InvalidKeyException ex) {
            ex.printStackTrace();
        } catch (NoSuchPaddingException ex) {
            ex.printStackTrace();
        }
        return null;
    }

**每当我解密时,我都会有一个空指针.有时它会给我正确的解密密码,但有时它会给我一个空指针.无法猜测这里的问题是什么**

** I am having a null pointer whenever I decrypt. sometimes it gives me the correct decrypted password but sometimes it gives me a null pointer. can't guess what the problem is here **

推荐答案

您正在混合使用字符串和字节数组.这并不总是一件好事.至少指定用于字节到字符转换的字符集.即便如此,它也不是 100% 安全的.最好将字符串视为字符串,将字节数组视为字节数组.

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.

如果这不能解决问题,那么有很多事情会导致Bad Padding"错误.基本上任何导致最后一个块的结尾与预期的填充不匹配的东西都会抛出错误.可能的原因包括:填充设置不正确、密钥不正确、密文损坏等.

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.

要尝试诊断问题,请将解密端设置为 NoPadding.这将接受任何内容,并允许您检查输出:

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.

第一块垃圾:你可能有一个关键错误或一个 IV 错误.

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.

如果它真的只是填充,那么将解密函数设置为期望那种填充.否则,请检查密钥/IV/密文对于加密和解密是否逐字节相同.

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.

在诊断后设置填充模式至关重要.NoPadding 不安全.

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

这篇关于java aes javax.crypto.BadPaddingException:给定的最终块未正确填充的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆