java - 为什么我解密时得到 'BadPaddingException'?

标签 java encryption aes padding

这是我的加密设置:

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");
    }
}

错误:由于填充错误 - null,无法解密消息。

我在解密时遇到 BadPaddingException,为什么?消息正好是 168 个字符,填充后为 176 个字符(可被 16 整除)

最佳答案

来 self 最初的评论:

典型的场景是 key 与另一端使用的 key 不同。这是最可能的原因,但您可能还想检查处理流的方式,因为您确实缺少 .close() 和可能的 .flush() 语句。您还假设您始终可以将所有数据读入缓冲区,但情况可能并非如此。

key 确实计算错误。

关于java - 为什么我解密时得到 'BadPaddingException'?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48624282/

相关文章:

Java - 如何将应急措施构建到调用 Web 服务的服务层中

java - Lombok 继承 : how to set default value for field in superclass without redeclaring it?

java - 如何正确使用 java manifold 库中的 Jailbreak 进行 junit 测试?

PHP:简单,验证字符串是否为十六进制?

c# - 如何在 C# 中解密由 mcrypt 在 PHP 中加密的加密 MCRYPT_RIJNDAEL_256 值?

java-me - J2ME 中使用 BouncycaSTLe 示例的 AES 加密/解密

java - 什么时候需要使用clone()方法进行转换?

html - 如何隐藏 HTML 页面的源代码

python - 如何加密字典数据?

encryption - Mega 的加密如何用于共享?