本文介绍了使用 Crypto++ 解密格式正确的密文时遇到问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在努力解密一个明显格式良好的密文大约一天.假设我们有以下十六进制编码的密文,它正好包含 160 个字符,因此有 80 个字节.

I've been struggling with decrypting an apparently well-formed cipher text for about a day. Assume we've got the following hex-encoded cipher text which contains exactly 160 characters thereby having 80 bytes.

QString的C = 1BFAC407AF0D440A2D6176C0B5D125AA96088490299AC18C74623C0EF1BB1372E554FC4150A8066220E943697BE2491D8AE13AA036B298425AC510A8A917D59EBB69708B9040AB3A84C63043EAD4AB07";QString k = CryptoUtils::hexEncode("abc");QString p = CryptoUtils::decrypt(c, k);qDebug() <<p;

如果我们使用的是 AES 256、AFAIK,那么密钥的长度必须为 32 字节,密文的长度必须为 16 字节的倍数,关于我的代码片段,所有这些条件都满足.

Provided we're using AES 256, AFAIK, the key must be of length 32 bytes and cipher text of a length of multiple of 16 bytes, which all these consditions are met regarding my snippet code.

请注意,我使用 SHA256 提供密码短语来生成 32 字节的密钥.因此,这可确保所有密钥的长度均为 32 字节.

Please note that I'm using SHA256 feeding with a pass phrase to generate a 32 bytes key. So, this ensures that all keys are of length 32 bytes.

完整源代码 可以在我在 GitHub 上的 repo(在分支 Part1)中找到这些函数.

Full source codes of those function can be found on my repo on GitHub (at branch Part1).

当我想运行这段代码时,我的应用程序崩溃了.这是一个例外:

When I want to run this code, my app crashes. Here's the exception:

terminate called after throwing an instance of 'CryptoPP::InvalidCiphertext'
  what():  StreamTransformationFilter: invalid PKCS #7 block padding found
The program has unexpectedly finished.

我搜索了这个问题,发现这可能是因为在加密纯文本后尾随 .但是,我不能仅仅解决问题.请帮帮我,这让我发疯.

I searched around about this problem and figured out it could be because of the trailing once you encrypted the plain text. However, I couldn't just solve the problem. Please help me out, it's just driving me crazy.

推荐答案

hexEncode 函数似乎有问题:

QString CryptoUtils::hexEncode(QString text)
{
    byte *bytearray = (byte *) text.toLatin1().data();
    int length = text.toLatin1().length();

    return hexEncode(bytearray, length);
}

应替换为:

QString CryptoUtils::hexEncode(QString text)
{
    byte *bytearray = (byte *) text.toStdString().data();
    int length = text.length();

    return hexEncode(bytearray, length);
}

这篇关于使用 Crypto++ 解密格式正确的密文时遇到问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-19 06:34