我正在尝试使用cryptopp,下面的代码导致stringsource函数的访问冲突。这可能是什么原因?我以前成功地运行过类似的代码,差别不大。
AesHelper.cpp
#include "dll.h"
#include "AesHelper.h"
#include "aes.h"
using CryptoPP::AES;
#include "ccm.h"
using CryptoPP::CBC_Mode;
#include "filters.h"
using CryptoPP::StringSink;
using CryptoPP::StringSource;
using CryptoPP::StreamTransformationFilter;
#include "hex.h"
using CryptoPP::HexEncoder;
using CryptoPP::HexDecoder;
#include <string>
using namespace std;
#include "osrng.h"
using CryptoPP::AutoSeededRandomPool;
byte AesHelper::_key[AES::DEFAULT_KEYLENGTH];
byte AesHelper::_iv[AES::BLOCKSIZE];
void AesHelper::encrypt(const char* str, char ** outIv, char ** encrypted )
{
try
{
AutoSeededRandomPool prng;
byte key[AES::DEFAULT_KEYLENGTH];
prng.GenerateBlock(key, sizeof(key));
byte iv[AES::BLOCKSIZE];
prng.GenerateBlock(iv, sizeof(iv));
string cipher, encoded;
string plain = "CBC Test Mode";
CBC_Mode<AES>::Encryption e;
e.SetKeyWithIV(key, sizeof(key), iv);
// The StreamTransformationFilter removes
// padding as required.
StreamTransformationFilter *stf = new StreamTransformationFilter(e,
new StringSink(cipher),
CryptoPP::BlockPaddingSchemeDef::ZEROS_PADDING
);
StringSource s(plain, true, stf); // This line cause Access Violation
StreamTransformationFilter filter(e);
filter.Put((const byte*)plain.data(), plain.size());
filter.MessageEnd();
const size_t ret = filter.MaxRetrievable();
cipher.resize(ret);
filter.Get((byte*)cipher.data(), cipher.size());
//encode the cipher to hexadecimal
StringSource(cipher, true,
new HexEncoder(
new StringSink(encoded)
) // HexEncoder
); // StringSource
//set the output parameter
outIv = (char**)_iv;
encrypted = (char**)cipher.c_str();
}
catch(const CryptoPP::Exception& e)
{
cerr << "exception : " << e.what() << endl;
exit(1);
}
}
错误
PaymentManager.exe 中 0x550714CA (cryptopp.dll) 处未处理的异常:0xC0000005:访问冲突读取位置 0x74736554。
cryptopp.dll!memcpy(unsigned char * dst, unsigned char * src, unsigned long count) 第 188 行未知
更新: 将 DLL 和 Exe 程序都设置为“发布”后问题解决。但现在又出现了新问题。在这条线上,问题也在 stringsource 函数中
StringSource(cipher, true,
new HexEncoder(
new StringSink(encoded)
) // HexEncoder
); // StringSource
错误
PaymentManager.exe 已触发断点。
程序停在
void __cdecl _free_base (void * pBlock) {
int retval = 0;
if (pBlock == NULL)
return;
RTCCALLBACK(_RTC_Free_hook, (pBlock, 0));
retval = HeapFree(_crtheap, 0, pBlock); // program stop at this line
if (retval == 0)
{
errno = _get_errno_from_oserr(GetLastError());
} }