当前位置: 首页 > 面试题库 >

为什么解密时出现“ BadPaddingException”?

唐宏壮
2023-03-14
问题内容

这是我的加密设置:

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整除)


问题答案:

从我最初的评论:

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

密钥确实计算不正确。



 类似资料:
  • 除此之外的所有站点都在解析,但是这里有大约10秒的延迟, 发生以下错误: Traceback(最近一次调用last):文件“D:\ Timur \ OpenServer \ domains \ Parser \ Parser . py”,第13行,main()文件“D:\ Timur \ OpenServer \ domains \ Parser \ Parser . py”,第9行,main p

  • 问题内容: 当我运行以下代码时: 我得到一个异常说: 为什么会出现此异常? 编辑:tmpList是一个LinkedList,其每个节点都包含DepConfAttr类型的对象。 我正在基于内存(首先是最高内存)对tmpList进行排序,这是DepConfAttr对象的属性之一。 上面的代码反映了我要通过以下代码实现的目标 问题答案: 为什么会出现此异常? 您要遍历列表,而不是通过迭代器从列表中删除一

  • 问题内容: 我正在使用ant生成javadocs,但是一遍又一遍地获取此异常-为什么? 我正在使用JDK 1.6.0_06 版本。 问题答案: 看来这已被报告为Java错误。这似乎是由于使用了第三方库(例如JUnit)中的注释,而在Javadoc调用中不包含带有该注释的jar。 如果是这种情况,只需在javadoc上使用-classpath选项并包括额外的jar文件。

  • 问题内容: 我已经调整了控制器的构造函数和fxml,以便将控制器的fxml的所有设置都放在fxml中,除了FXML的构造和fxml的加载。这是我的控制器: 和我的fxml文件: 当调用fxmlLoader.load()并返回FXMLLoader时会发生stackoverflow fxmlLoader = new FXMLLoader(…),然后再次调用fxmlLoader.load()…为什么会发

  • 问题内容: 当我从.py文件尝试此代码时,此代码有效,但在命令行解释器和Idle中失败。 我正在使用python 2.6 问题答案: 对于Python 3,它是一个函数而不是语句,因此,如果您使用的是Python 3,则需要在参数周围加上括号,如中所示。 但是,插入符号指向的位置比使用Python 3的位置早,因此您必须使用Python2.x。在这种情况下,错误是因为您要在交互式解释器中输入此内容

  • 我想在RxJava中实现一个下载一些文件的处理队列。我想下载的文件数量可能高达100个左右。 一切都是在Android上使用RxJava 1.1.1开发的 我做错了什么?