【问题标题】:Having trouble decrypting a well-formed cipher text using Crypto++使用 Crypto++ 解密格式良好的密文时遇到问题
【发布时间】:2015-12-08 22:30:05
【问题描述】:

背景

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

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

qDebug() << p;

如果我们使用 AES 256,AFAIK,密钥长度必须为 32 字节,密文长度必须为 16 字节的倍数,对于我的 sn-p 代码,所有这些条件都满足。

请注意,我使用带有密码短语的 SHA256 馈送来生成 32 字节的密钥。因此,这确保了所有密钥的长度为 32 个字节。

Full source codes 这些函数可以在我在 GitHub 上的 repo 中找到(在分支 Part1)。


我的问题

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

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

我搜索了这个问题,发现这可能是因为你加密了纯文本后尾随的\0。但是,我不能仅仅解决问题。请帮帮我,这简直把我逼疯了。

【问题讨论】:

  • 你是怎么得到密文的?使用相同的方法? PKCS#7 填充错误是唯一可能产生的错误(除了与块大小相关的错误),它们显示不正确的代码、不正确的密钥或不正确的数据(对于 CBC 模式下的小密文,甚至不正确的 IV)。
  • StringSource(decodedKey, size, true, new HashFilter(*(new SHA256), new ArraySink(key, AES::MAX_KEYLENGTH)));:这不是一个好的 KBKDF,它隐藏了密钥的问题,并且在我的拙见中它相当不可读。如果你使用你指向的库以外的任何东西,那么解密肯定会失败(当然,除非在另一个库中应用了相同的非标准哈希)。
  • @MaartenBodewes 我从 encrypt 函数(从我链接到的那个 cpp 文件)生成了该密文。此外,IV 始终为0。对于密钥,我看不出 SHA256 有什么问题(我只需要从密码生成一个统一的密钥,我应该使用哪种算法并不重要)。

标签: qt encryption aes crypto++


【解决方案1】:

这些函数的完整源代码可以在我的 GitHub 上的 repo 中找到

我至少会做出这些改变:

QString CryptoUtils::encrypt(QString text, QString keyhex)
{
    ...

    // name the variable, kill the memory leak
    SHA256 sha256;
    StringSource ss1(decodedKey, size, true, new HashFilter(sha256, new ArraySink(key, AES::MAX_KEYLENGTH)));
    ...

    // name the variable
    StringSource ss2(plain, true, new StreamTransformationFilter(Encryptor, new HexEncoder(new StringSink(encrypted))));

    // verify embedded NULLs don't affect results
    QString qs = QString::fromStdString(encrypted);
    assert(qs.length() == encrypted.length());
}

还有:

QString CryptoUtils::decrypt(QString text, QString keyhex)
{
    // bad karma here...
    string encrypted = text.toStdString();
    assert(encrypted.length() == text.length());
    ...

    // name the variable, kill the memory leak
    SHA256 sha256;
    StringSource ss1(decodedKey, size, true, new HashFilter(sha256, new ArraySink(key, AES::MAX_KEYLENGTH)));
    ...

    // name the variable,
    StringSource ss2(encrypted, true, new HexDecoder(new StreamTransformationFilter(Decryptor, new StringSink(plain))));

    // verify embedded NULLs don't affect results
    QString qs = QString::fromStdString(plain);
    assert(qs.length() == plain.length());
}

【讨论】:

  • 感谢您的回答。但问题是因为我的hexEncode 函数。它为相同的输入生成不同的十六进制代码。
  • @Frogatto - “它为相同的输入生成不同的十六进制代码” - 这在某种程度上很有趣......使用 UTF-8;它最适合互操作性。或者,使用原始字节和类似 HKDF 的东西来提取和扩展熵。
【解决方案2】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-17
    • 2013-07-03
    相关资源
    最近更新 更多