我正在使用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函数?
最佳答案
解决方法如下:
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);
关于c++ - 在Crypto++中将std::string用于具有AES加密的 key ,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/33753293/