如何使用 AES 以 sha256 散列作为密钥 crypto++
How to encrypt using AES with sha256 hash as key crypto++
我有一个使用 SHA256 散列的字符串用作密钥,但我如何使用此密钥在 CBC 模式和 crypto++ 中使用 AES 加密字符串?
谢谢。
我最终使用以下代码实现了预期的结果。
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;
我有一个使用 SHA256 散列的字符串用作密钥,但我如何使用此密钥在 CBC 模式和 crypto++ 中使用 AES 加密字符串? 谢谢。
我最终使用以下代码实现了预期的结果。
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;