私は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());
} }