为什么在一个Java类中加密而在另一个Java类中解密会导致BadPaddingError?

时间:2018-10-23 00:39:59

标签: java encryption cbc-mode block-cipher

我有一个测试函数,该函数在一个Java类FirstClass中返回一个byte []:

public static byte[] testA(){
        CipherClass crypto = new CipherClass();
        byte[] a = crypto.encrypt("helloWorld");
        return a;
    }

CipherClass包含我的java.crypto.Cipher方法来加密和解密输入。

在另一个Java类SecondClass中,我实例化了先前的FirstClass并调用了它的testA()方法,该方法保存到byte[]变量中,然后尝试对其进行解密:

FirstClass fc = new FirstClass();
byte[] b = fc.testA();
System.out.println(crypto.decrypt(b)); //Outputs BadPaddingError

为什么加密一个Java文件中的字符串,然后将结果字节[]传递到另一个Java文件中以对其进行解密会导致BadPaddingError?

如何解决此问题?

如果两者加密和解密都属于以下同一类,为什么密码仅起作用

CipherClass crypto = new CipherClass();
byte[] a = crypto.encrypt("helloWorld"); //Outputs [B@5474c6c
byte[] b = a;
System.out.println(crypto.decrypt(b));  //Outputs "helloWorld"

编辑:我的CipherClass代码如下。

SecretKeyIVParameterSpec生成器:

public List<KeyGenerator> getKeyGenerator(){

        List<KeyGenerator> list = new ArrayList<KeyGenerator>();
        KeyGenerator keyGenerator;
        int keyBitSize = 128;

        try 
        {
            SecureRandom secureRandom = new SecureRandom();
            keyGenerator = KeyGenerator.getInstance("AES");
            keyGenerator.init(keyBitSize, secureRandom);

            list.add(keyGenerator);

            return list;
        } 
        catch (NoSuchAlgorithmException e)
        {
            e.printStackTrace();
        }

        return list;
    }

    public List<SecretKey> getSecretKey(List<KeyGenerator> keygen) {

        List<SecretKey> list = new ArrayList<SecretKey>();
        KeyGenerator keyGenerator;
        SecretKey secretKey;

        keyGenerator = keygen.get(0);
        secretKey = keyGenerator.generateKey();

        list.add(secretKey);

        return list;
    }

    public List<IvParameterSpec> getIvSpec(List<SecretKey> secretKey) {

        List<IvParameterSpec> list = new ArrayList<IvParameterSpec>();
        Cipher cipher;

        byte[] iv;

        try 
        {
            cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
            iv = new byte[cipher.getBlockSize()];           
            IvParameterSpec ivSpec = new IvParameterSpec(iv);

            list.add(ivSpec);
        } 

        catch (NoSuchAlgorithmException e) 
        {       
            e.printStackTrace();
        } 

        catch (NoSuchPaddingException e) 
        {
            e.printStackTrace();
        }

        return list;
    }

    List<KeyGenerator> kgList = getKeyGenerator();
    List<SecretKey> skList = getSecretKey(kgList); //skList.get(0) is the SecretKey
    List<IvParameterSpec> ivList = getIvSpec(skList); //ivList.get(0) is the IVParameterSpec

我的encrypt()方法:

    public byte[] encrypt(String inputStr) {

            Cipher cipher;

            SecretKey secretKey = skList.get(0);
            IvParameterSpec ivSpec = ivList.get(0);

            byte[] plainText;
            byte[] cipherText = new byte[]{};       

            try 
            {
                cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");

                try 
                {
                    secretKey = skList.get(0);
                    ivSpec = ivList.get(0);
                    cipher.init(Cipher.ENCRYPT_MODE, secretKey, ivSpec);                

                    try 
                    {
                        plainText = inputStr.getBytes("UTF-8");

                        cipherText = cipher.doFinal(plainText);

                        return cipherText;
                    }

                    catch (IllegalBlockSizeException e) 
                    {
                        e.printStackTrace();
                    } 

                    catch (BadPaddingException e) 
                    {
                        e.printStackTrace();
                    }

                    catch (UnsupportedEncodingException e) 
                    {
                        e.printStackTrace();
                    }                
                } 

                catch (InvalidKeyException e) 
                {
                    e.printStackTrace();
                }

                catch (InvalidAlgorithmParameterException e1) 
                {
                    e1.printStackTrace();
                }
            } 

            catch (NoSuchAlgorithmException e) 
            {       
                e.printStackTrace();
            } 

            catch (NoSuchPaddingException e) 
            {
                e.printStackTrace();
            }

            return cipherText;
        }

我的decrypt()方法:

public String decrypt(byte[] cipherText){
        Cipher cipherDe;

        SecretKey secretKey = skList.get(0);
        IvParameterSpec ivSpec = ivList.get(0);

        byte[] deciByte;

        String decryptText = "";

        try 
        {
            cipherDe = Cipher.getInstance("AES/CBC/PKCS5Padding");

            try 
            {           
                secretKey = skList.get(0);
                ivSpec = ivList.get(0);
                cipherDe.init(Cipher.DECRYPT_MODE, secretKey, ivSpec);              

                try 
                {               
                    //De-cryption
                    deciByte = cipherDe.doFinal(cipherText);

                    decryptText = new String(deciByte);

                    return decryptText;
                }

                catch (IllegalBlockSizeException e) 
                {
                    e.printStackTrace();
                } 

                catch (BadPaddingException e) 
                {
                    e.printStackTrace();
                } 
            } 

            catch (InvalidKeyException e) 
            {
                e.printStackTrace();
            }

            //De-cryption
            catch (InvalidAlgorithmParameterException e1) 
            {
                e1.printStackTrace();
            }
        } 

        catch (NoSuchAlgorithmException e) 
        {       
            e.printStackTrace();
        } 

        catch (NoSuchPaddingException e) 
        {
            e.printStackTrace();
        }

        return decryptText;
    }

0 个答案:

没有答案