如何使用带有sha256哈希的AES作为密钥加密++进行加密

时间:2017-08-14 07:03:30

标签: c++ encryption aes crypto++

我有一个使用SHA256作为密钥使用的字符串哈希但是如何使用此密钥在CBC模式和加密++中使用AES加密字符串? 感谢。

1 个答案:

答案 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;