为什么我在解密时会收到“BadPaddingException”错误?

3

这是我的加密设置:

public static String encryptionAlgorithm = "AES";
public static short encryptionBitCount = 256;
public static int encryptionMessageLength = 176;
public static String hashingAlgorithm = "PBEWITHSHAAND128BITAES-CBC-BC";
       //PBEWithSHA256And256BitAES-CBC-BC"PBEWithMD5AndDES";//"PBKDF2WithHmacSHA1";
public static short hashingCount = 512;
public static String cipherTransformation = "AES/CBC/PKCS5Padding";

这是我的解密代码:

public byte[] readMessage () throws Exception
{
    byte[] iv = new byte[16];
    byte[] message = new byte[EncryptionSettings.encryptionMessageLength];

    try
    {
        // read IV from stream
        if (stream.read(iv) != 16)
            throw new Exception("Problem receiving full IV from stream");
    }
    catch (final IOException e)
    {
        throw new Exception("Unable to read IV from stream");
    }

    try
    {
        cipher.init(Cipher.DECRYPT_MODE, key, new IvParameterSpec(iv));
    }
    catch (final InvalidKeyException e)
    {
        throw new Exception("Invalid key");
    }
    catch (final InvalidAlgorithmParameterException e)
    {
        throw new Exception("Invalid algorithm parameter");
    }

    try
    {
        //read message from stream
        if (stream.read(message) != EncryptionSettings.encryptionMessageLength)
             throw new Exception("Problem receiving full encrypted message from stream");
    }
    catch (final IOException e)
    {
        throw new Exception("Unable to read message from stream");
    }

    try
    {
        return cipher.doFinal(message); //decipher message and return it.
    }
    catch (IllegalBlockSizeException e)
    {
        throw new Exception("Unable to decrypt message due to illegal block size - "
                          + e.getMessage());
    }
    catch (BadPaddingException e)
    {
        throw new Exception("Unable to decrypt message due to bad padding - "
                            + e.getMessage());
    }
}

这是我的加密代码:

public void writeMessage (final byte[] message) throws Exception
{
    try
    {
        // write iv
        byte b[] = cipher.getParameters().getParameterSpec(IvParameterSpec.class).getIV();
        System.out.println(b.length);
        stream.write(b);
    }
    catch (final InvalidParameterSpecException e) 
    {
        throw new Exception("Unable to write IV to stream due to invalid"+
                            " parameter specification");
    }
    catch (final IOException e)
    {
        throw new Exception("Unable to write IV to stream");
    }

    try
    {
        // write cipher text
        byte b[] = cipher.doFinal(message);
        System.out.println(b.length);
        stream.write(b);
    }
    catch (final IllegalBlockSizeException e)
    {
        throw new Exception("Unable to write cipher text to stream due to "+
                            "illegal block size");
    }
    catch (final BadPaddingException e)
    {
        throw new Exception("Unable to write cipher text to stream due to " +
                            "bad padding");
    }
    catch (final IOException e)
    {
        throw new Exception("Unable to write cipher text to stream");
    }
}

错误: 由于填充不良-无法解密消息-空。

当我解密时,为什么会出现BadPaddingException?该消息恰好有168个字符,在填充后为176(可被16整除)。


1
同时,你的异常处理并不真正地“处理”…你只是抛弃了堆栈跟踪的某些部分,换取了一个更不具描述性的异常类型和一个稍微具描述性的消息。 - Paŭlo Ebermann
我应该指出,我只挑选了关键的方法,并且一切都正确地打开/关闭。密钥不能不同,因为它内置于服务器和客户端中(目前是这样 - 显然会改变)。 - Cheetah
我们的信息太少了,因为我们没有代码(例如流),所以不能编译。如果说一切都没问题,那很好,但同时你的代码还是无法运行。因此,这是不正确的。 - Maarten Bodewes
1
你已经找出了问题 - 密钥不同。当我将md5hash(密码+盐)读入作为加密密码时,它也读入了其后的注释。 - Cheetah
2个回答

6

从我的最初评论:

一个典型的情况是密钥与另一侧使用的密钥不同。这是最有可能的原因,但您还需要检查如何处理流,因为您确实缺少 .close() 和可能的 .flush() 语句。您还假设您总是可以将所有数据读入缓冲区,但情况可能并非如此。

事实上,密钥确实被错误地计算了。


1

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接