无法使用Crypto ++解密格式正确的密文 [英] Having trouble decrypting a well-formed cipher text using Crypto++

查看:730
本文介绍了无法使用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.

这些功能的完整源代码可以在我的GoHub的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.

我搜索了关于这个问题,并想出可能是因为尾随 \0 一旦你加密纯文本。但是,我无法解决问题。请帮助我,这只是让我疯狂。

I searched around about this problem and figured out it could be because of the trailing \0 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 ++解密格式正确的密文的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆