为啥解密时会出现“BadPaddingException”?

Posted

技术标签:

【中文标题】为啥解密时会出现“BadPaddingException”?【英文标题】:Why am I getting 'BadPaddingException' when decrypting?为什么解密时会出现“BadPaddingException”? 【发布时间】:2012-02-07 01:41:44 【问题描述】:

这是我的加密设置:

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

错误:Unable to decrypt message due to bad padding - null.

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

【问题讨论】:

另外,您的异常处理并不是真正的“处理”......您只是将堆栈跟踪的一部分扔掉,以换取描述性较少的异常类型和更具描述性的消息。 我应该指出,我只挑选了关键方法,一切都正确打开/关闭。密钥不能不同,因为它内置在服务器和客户端中(目前 - 显然这会改变)。 我们的信息太少,无法编译,因为我们没有代码,例如对于溪流。我说一切都很好,但与此同时,您的代码将无法运行。所以它是正确的。 无论如何你都挑出了问题 - 键不一样。当我读取 md5hash(密码 + salt)作为加密密码时,它也在后面的评论中读取。 【参考方案1】:

来自我最初的评论:

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

密钥确实计算不正确。

【讨论】:

【参考方案2】:

BadPaddingException 加密/解密错误

我遇到了这样的错误,但这对我有帮助

http://themasterofmagik.wordpress.com/2014/03/19/simple-aes-encryption-and-decryption-in-java-part1/

希望对你也有帮助。

【讨论】:

以上是关于为啥解密时会出现“BadPaddingException”?的主要内容,如果未能解决你的问题,请参考以下文章

为啥使用 SingleAsync 时会出现此错误?

为啥插入子视图时会出现黑屏?

为啥在使用 ssl 时会出现连接问题?

为啥创建/更新通知时会出现 RemoteServiceException?

为啥在使用 OpenEntityManagerInViewFilter 时会出现 LazyInitializationException? (使用 Spring Roo)

为啥调用firebase函数时会出现FCM错误