【问题标题】:Using std::string for key with AES encryption in Crypto++在 Crypto++ 中使用 std::string 作为 AES 加密的密钥
【发布时间】:2015-11-17 09:28:29
【问题描述】:

我正在使用Example of AES using Crypto++。我想用这个密钥加密:

std::string key = "mykey";

为键分配内存

byte key[ CryptoPP::AES::DEFAULT_KEYLENGTH ], iv[ CryptoPP::AES::BLOCKSIZE ];
memset( key, 0x00, CryptoPP::AES::DEFAULT_KEYLENGTH );
memset( iv, 0x00, CryptoPP::AES::BLOCKSIZE );

加密:

CryptoPP::AES::Encryption aesEncryption(key, CryptoPP::AES::DEFAULT_KEYLENGTH);
CryptoPP::CBC_Mode_ExternalCipher::Encryption cbcEncryption( aesEncryption, iv );

如何将 std::string 类型的密钥传递给 aesEncryption 函数?

【问题讨论】:

  • 只需将您的字符串 memcpy 到键字节数组中。密钥必须为 16 个字节,因此您需要填充“myKey”
  • 我做了`std::string keyraw = "0123456789012345"; memcpy(key, keyraw,16);` 我得到 ** no known conversion for argument 1 from âstd::string {aka std::basic_string}â 到 âconst byte* **
  • 好的,那么你准备好了。要么使用 ProcessString 对象的 ProcessString 方法,要么像您的示例一样创建 StreamTransformation。
  • @KurinchiMalar - 另见 Crypto++ wiki 上的 Advanced Encryption Standard

标签: c++ encryption aes crypto++


【解决方案1】:

解决办法如下:

std::string sKey = "mykey";

if(CryptoPP::AES::DEFAULT_KEYLENGTH < sKey.size())
    sKey = sKey.substr(0, CryptoPP::AES::DEFAULT_KEYLENGTH); // chop if too long
else if(CryptoPP::AES::DEFAULT_KEYLENGTH > sKey.size())
    sKey += std::string(CryptoPP::AES::DEFAULT_KEYLENGTH - sKey.size(), '*'); // pad

memcpy(key, sKey.c_str(), CryptoPP::AES::DEFAULT_KEYLENGTH);

【讨论】:

  • 我通常不会投反对票,但我认为这个答案值得。答案还有很多不足之处。特别是关于用星号填充的部分。你能引用一些推荐这种键拉伸方法的东西吗?此外,您通常不会删除额外的位。你提取熵并使用它们。
  • 我必须说你让我很开心,谢谢。
【解决方案2】:

这是另一个使用 sha256 和密码 cat 的示例!

#include <iomanip>
#include <iostream>

#include <cryptopp/modes.h>
#include <cryptopp/aes.h>
#include <cryptopp/filters.h>
#include <cryptopp/sha.h>
#include <cryptopp/hex.h>


int main(int argc, char* argv[]) {



    CryptoPP::SHA256 hash;
    CryptoPP::byte digest[CryptoPP::SHA256::DIGESTSIZE];
    std::string message = "cat";

    hash.CalculateDigest(digest, (CryptoPP::byte*)message.c_str(), message.length());

    CryptoPP::HexEncoder encoder;
    std::string sKey;
    encoder.Attach(new CryptoPP::StringSink(sKey));
    encoder.Put(digest, sizeof(digest));
    encoder.MessageEnd();

    CryptoPP::byte key[CryptoPP::AES::MAX_KEYLENGTH]; //16 Bytes MAXKEYLENGTH 32 BYTES(SHA 256)
    CryptoPP::byte  iv[CryptoPP::AES::BLOCKSIZE]; 
    memcpy(key, sKey.c_str(), CryptoPP::AES::MAX_KEYLENGTH);;
    memset(iv, 0x00, CryptoPP::AES::BLOCKSIZE);

    //
    // String and Sink setup
    //
    std::string plaintext = "Now is the time for all good men to come to the aide...";
    std::string ciphertext;
    std::string decryptedtext;

    //
    // Dump Plain Text
    //
    std::cout << "Plain Text (" << plaintext.size() << " bytes)" << std::endl;
    std::cout << plaintext;
    std::cout << std::endl << std::endl;

    //
    // Create Cipher Text
    //

    CryptoPP::AES::Encryption aesEncryption(key, CryptoPP::AES::MAX_KEYLENGTH);
    CryptoPP::CBC_Mode_ExternalCipher::Encryption cbcEncryption(aesEncryption, iv);

    CryptoPP::StreamTransformationFilter stfEncryptor(cbcEncryption, new CryptoPP::StringSink(ciphertext));
    stfEncryptor.Put(reinterpret_cast<const unsigned char*>(plaintext.c_str()), plaintext.length());
    stfEncryptor.MessageEnd();

    //
    // Dump Cipher Text
    //
    std::cout << "Cipher Text (" << ciphertext.size() << " bytes)" << std::endl;

    for (int i = 0; i < ciphertext.size(); i++) {

        std::cout << "0x" << std::hex << (0xFF & static_cast<CryptoPP::byte>(ciphertext[i])) << " ";
    }

    std::cout << std::endl << std::endl;

    //
    // Decrypt
    //
    CryptoPP::AES::Decryption aesDecryption(key, CryptoPP::AES::MAX_KEYLENGTH);
    CryptoPP::CBC_Mode_ExternalCipher::Decryption cbcDecryption(aesDecryption, iv);

    CryptoPP::StreamTransformationFilter stfDecryptor(cbcDecryption, new CryptoPP::StringSink(decryptedtext));
    stfDecryptor.Put(reinterpret_cast<const unsigned char*>(ciphertext.c_str()), ciphertext.size());
    stfDecryptor.MessageEnd();

    //
    // Dump Decrypted Text
    //
    std::cout << "Decrypted Text: " << std::endl;
    std::cout << decryptedtext;
    std::cout << std::endl << std::endl;

    return 0;

}

【讨论】:

    猜你喜欢
    • 2018-01-09
    • 2018-12-27
    • 1970-01-01
    • 1970-01-01
    • 2011-06-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多