[英]AES encryption on QStrings, libcrypto++
I have the followind piece of code that encrypts and decrypts the message. 我有一段代码加密和解密消息。
QString AesUtils::encrypt(QString message, QString aesKey)
{
string plain = message.toStdString();
qDebug() << "Encrypt" << plain.data() << " " << plain.size();
string ciphertext;
// Hex decode symmetric key:
HexDecoder decoder;
string stdAesKey = aesKey.toStdString();
decoder.Put((byte*)stdAesKey.data(), aesKey.size());
decoder.MessageEnd();
word64 size = decoder.MaxRetrievable();
char *decodedKey = new char[size];
decoder.Get((byte *)decodedKey, size);
// Generate Cipher, Key, and CBC
byte key[ AES::MAX_KEYLENGTH ], iv[ AES::BLOCKSIZE ];
StringSource( reinterpret_cast<const char *>(decodedKey), true,
new HashFilter(*(new SHA256), new ArraySink(key, AES::MAX_KEYLENGTH)) );
memset( iv, 0x00, AES::BLOCKSIZE );
CBC_Mode<AES>::Encryption Encryptor( key, sizeof(key), iv );
StringSource( plain, true, new StreamTransformationFilter( Encryptor,
new HexEncoder(new StringSink( ciphertext )) ) );
return QString::fromStdString(ciphertext);
}
QString AesUtils::decrypt(QString message, QString aesKey)
{
string plain;
string encrypted = message.toStdString();
// Hex decode symmetric key:
HexDecoder decoder;
string stdAesKey = aesKey.toStdString();
decoder.Put( (byte *)stdAesKey.data(), aesKey.size() );
decoder.MessageEnd();
word64 size = decoder.MaxRetrievable();
char *decodedKey = new char[size];
decoder.Get((byte *)decodedKey, size);
// Generate Cipher, Key, and CBC
byte key[ AES::MAX_KEYLENGTH ], iv[ AES::BLOCKSIZE ];
StringSource( reinterpret_cast<const char *>(decodedKey), true,
new HashFilter(*(new SHA256), new ArraySink(key, AES::MAX_KEYLENGTH)) );
memset( iv, 0x00, AES::BLOCKSIZE );
try {
CBC_Mode<AES>::Decryption Decryptor
( key, sizeof(key), iv );
StringSource( encrypted, true,
new HexDecoder(new StreamTransformationFilter( Decryptor,
new StringSink( plain )) ) );
}
catch (Exception &e) { // ...
qDebug() << "Exception while decrypting " << e.GetWhat().data();
}
catch (...) { // ...
}
qDebug() << "decrypt" << plain.data() << " " << AES::BLOCKSIZE;
return QString::fromStdString(plain);
}
The problem is that I randomly get: 问题是我随机得到:
StreamTransformationFilter: invalid PKCS #7 block padding found
When decrypting the content. 解密内容时。 The encryption should fully support
QString
, since it may contain some Unicode data. 加密应该完全支持
QString
,因为它可能包含一些Unicode数据。 But it doesn't work even with a basic, string which contains only [Az][az][0-9] 但即使只包含[Az] [az] [0-9]的基本字符串,它也不起作用
The aesKey
size is 256. aesKey
大小为256。
Following some answers on Stack Overflow, somebody suggested the use of HexDecoder
/ HexEncoder
, but it does not solve the problem in my case. 关于Stack Overflow的一些答案,有人建议使用
HexDecoder
/ HexEncoder
,但它并没有解决我的问题。
The fist problem with my code was that I was feeding normal string in aesKey QString. 我的代码的第一个问题是我在aesKey QString中输入正常的字符串。
So instead of "1231fsdf$5r4" you need to give a key in hex format: [0-9][AF] 因此,您需要以十六进制格式提供密钥而不是“1231fsdf $ 5r4”:[0-9] [AF]
Then, the problem was here: 然后,问题在这里:
char *decodedKey = new char[size];
decoder.Get((byte *)decodedKey, size);
I guess the string was full 64 bytes and there was no space for NULL at the end. 我猜这个字符串是64个字节,最后没有空格。 The problem dissapeared after I changed to:
我改为之后问题消失了:
char *decodedKey = new char[size+2];
Now the code works fine. 现在代码工作正常。 Hope this will help somebody in the future.
希望这将有助于将来的某些人。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.