简体   繁体   English

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

[英]Why am I getting 'BadPaddingException' when decrypting?

Here are my encryption settings:这是我的加密设置:

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

Here is my code to decrypt:这是我的解密代码:

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

Here is my code to encrypt:这是我要加密的代码:

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

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

I am getting a BadPaddingException when decrypting, why?解密时出现 BadPaddingException,为什么? The message is exactly 168 characters which is 176 after padding (divisible by 16)该消息正好是 168 个字符,即填充后为 176(可被 16 整除)

From my initial comment:从我最初的评论:

A typical scenario is one where the key is different from the one used at the other side.典型的场景是密钥与另一侧使用的密钥不同。 This is the most probable cause, but you might also want to check the way you handle streams, because you really lack .close() and possibly .flush() statements.这是最可能的原因,但您可能还想检查处理流的方式,因为您确实缺少 .close() 和可能的 .flush() 语句。 You also assume that you always can read all the data into the buffer, which may not be the case.您还假设您始终可以将所有数据读入缓冲区,但情况可能并非如此。

The key was indeed calculated incorrectly.密钥确实计算错误。

BadPaddingException Error in enryption/decryption BadPaddingException 加密/解密错误

I encountered such an error, but this helped me我遇到了这样的错误,但这对我有帮助

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

hope it helps you too.希望它也能帮助你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM