我有一个使用SHA256作为密钥使用的字符串哈希但是如何使用此密钥在CBC模式和加密++中使用AES加密字符串? 感谢。
答案 0 :(得分:0)
我最终使用以下代码实现了预期的结果。
QString qhash = "hash";
std::string plain = "message";
std::string ciphertext;
std::string stdhash = qhash.toStdString();
CryptoPP::HexDecoder decoder;
decoder.Put((byte*)stdhash.data(),qhash.size());
decoder.MessageEnd();
CryptoPP::word64 size = decoder.MaxRetrievable();
char *decodedKey = new char[size];
decoder.Get((byte *)decodedKey, size);
byte hash[CryptoPP::AES::MAX_KEYLENGTH], iv[ CryptoPP::AES::BLOCKSIZE ];
CryptoPP::StringSource(reinterpret_cast<const char *>(decodedKey), true,new CryptoPP::ArraySink(hash, CryptoPP::AES::MAX_KEYLENGTH));
memset(iv, 0x00, CryptoPP::AES::BLOCKSIZE);
CryptoPP::CBC_Mode<CryptoPP::AES>::Encryption Encryptor(hash,sizeof(hash),iv);
CryptoPP::StringSource( plain, true, new CryptoPP::StreamTransformationFilter( Encryptor, new CryptoPP::HexEncoder(new CryptoPP::StringSink( ciphertext )) ) );
return ciphertext;