如何解决字符串析构函数中的cryptopp dll崩溃
我正在使用 Crypto++ library 实现加密/解密程序。 我让cryptopp dll和新项目引用它。
现在,我在 std::string 析构函数中面临崩溃问题。 以下是我的代码。
//Encrypt
void Encryption(std::string encryptData,std::string& outString)
{
std::string plain,cipher,encoded,recovered;
plain = encryptData;
unsigned char key[CryptoPP::AES::DEFAULT_KEYLENGTH];
memset(key,0x00,CryptoPP::AES::DEFAULT_KEYLENGTH);
std::string rawKey = "testRawKey";
hex2byte(rawKey,rawKey.length(),key);
unsigned char iv[CryptoPP::AES::BLOCKSIZE];
memset(iv,CryptoPP::AES::BLOCKSIZE);
std::string rawIv = "testRawIV";
hex2byte(rawIv,rawIv.length(),iv);
encoded.clear();
CryptoPP::StringSource(key,sizeof(key),true,new CryptoPP::HexEncoder(
new CryptoPP::StringSink(encoded)
) // HexEncoder
); // StringSource
encoded.clear();
CryptoPP::StringSource(iv,sizeof(iv),new CryptoPP::HexEncoder(
new CryptoPP::StringSink(encoded)
) // HexEncoder
); // StringSource
try {
CryptoPP::CFB_Mode< CryptoPP::AES >::Encryption e;
e.SetKeyWithIV(key,iv);
CryptoPP::StreamTransformationFilter filter(e);
filter.Put((const unsigned char*)plain.data(),plain.size());
filter.MessageEnd();
const size_t ret = filter.MaxRetrievable();
cipher.resize(ret);
filter.Get((unsigned char*)cipher.data(),cipher.size());
}
catch (const CryptoPP::Exception& e) {
std::cerr << e.what() << std::endl;
cipher = "";
}
outString = cipher;
}
//Decrypt
std::string Decryption(std::string cipher)
{
std::string plain,recovered;
unsigned char* key = new unsigned char[CryptoPP::AES::DEFAULT_KEYLENGTH];
memset(key,key);
unsigned char* iv = new unsigned char[CryptoPP::AES::BLOCKSIZE];
memset(iv,iv);
encoded.clear();
CryptoPP::StringSource ss1(key,new CryptoPP::HexEncoder(
new CryptoPP::StringSink(encoded)
) // HexEncoder
); // StringSource
encoded.clear();
CryptoPP::StringSource ss2(iv,new CryptoPP::HexEncoder(
new CryptoPP::StringSink(encoded)
) // HexEncoder
); // StringSource
std::string plainText;
try {
CryptoPP::CFB_Mode< CryptoPP::AES >::Decryption d;
d.SetKeyWithIV(key,iv);
CryptoPP::StreamTransformationFilter filter(d);
filter.Put((const unsigned char*)cipher.data(),cipher.size());
filter.MessageEnd();
const size_t ret = filter.MaxRetrievable();
recovered.resize(ret);
filter.Get((unsigned char*)recovered.data(),recovered.size());
}
catch (const CryptoPP::Exception& e) {
std::cerr << e.what() << std::endl;
recovered = "";
}
return recovered;
}
void main()
{
std::string result = "";
Encryption("test",result);
cout << result << endl;
std::string result1 = Decryption(result);
cout << result1 << endl;
}
当我运行这段代码时,总是出现错误。 heap\debug_heap.cpp 行:904 表达式:_CrtlsValidHeapPointer(block)
testnesteddll.exe!std::string::~basic_string
根据调用堆栈,错误发生在 string 析构函数上。这就是为什么我认为崩溃发生在 string 析构函数上。
最后,我查看了与字符串相关的代码,并找出了问题所在。当我注释掉我使用 StringSource 类的位置时,错误消失了。 所以我注释掉了所有与 StringSource 相关的代码,然后我运行了它。效果很好,但这是我不想要的结果。
我真的很难解决这个问题。请您分享一下这方面的知识好吗? 提前致谢。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。