我是crypto++的新手,只是遵循其cryptest项目(test.cpp)中的一个例子。我使用 RSA 生成了公钥和私钥。我尝试使用键,与示例中完全相同。它在crypto++自己的项目中运行良好,并在我的项目中生成未经处理的异常。下面是在解密阶段中断的基本代码。有什么建议吗?
#include "stdafx.h"
#include <core/osrng.h>
#include <core/modes.h>
#include <core/hex.h>
#include <core/files.h>
#include <core/rsa.h>
#include <core/sha.h>
#include <core/cryptlib.h>
#include <iostream>
using namespace CryptoPP;
using namespace std;
static OFB_Mode<AES>::Encryption s_globalRNG;
RandomNumberGenerator & GlobalRNG()
{
return s_globalRNG;
}
string RSAEncryptString(const char *pubFilename, const char *seed, const char *message)
{
FileSource pubFile(pubFilename, true, new HexDecoder);
RSAES_OAEP_SHA_Encryptor pub(pubFile);
RandomPool randPool;
randPool.IncorporateEntropy((byte *)seed, strlen(seed));
string result;
StringSource(message, true, new PK_EncryptorFilter(randPool, pub, new HexEncoder(new StringSink(result))));
return result;
}
string RSADecryptString(const char *privFilename, const char *ciphertext)
{
FileSource privFile(privFilename, true, new HexDecoder);
RSAES_OAEP_SHA_Decryptor priv(privFile);
string result;
StringSource(ciphertext, true, new HexDecoder(new PK_DecryptorFilter(GlobalRNG(), priv, new StringSink(result))));
return result;
}
int _tmain(int argc, _TCHAR* argv[])
{
char privFilename[128] = "pri4096";
char pubFilename[128] = "pub4096";
char seed[1024] = "seed";
char message[1024] = "test";
try
{
string ciphertext = RSAEncryptString(pubFilename, seed, message);
string decrypted = RSADecryptString(privFilename, ciphertext.c_str());
}
catch(CryptoPP::Exception &e)
{
cout << "nCryptoPP::Exception caught: " << e.what() << endl;
}
return 0;
}
在我的项目中,程序中断了
StringSource(ciphertext, true, new HexDecoder(new PK_DecryptorFilter(GlobalRNG(), priv, new StringSink(result))));
调试器指向 rjindael.cpp,函数 AESNI_Enc_Block,第 1005 行。
正如 Yakk 所指出的,我错过了变量s_globalRNG的初始化。以下代码解决了我的问题。
//just below main()
std::string seed2 = IntToString(time(NULL));
seed2.resize(16);
s_globalRNG.SetKeyWithIV((byte *)seed2.data(), 16, (byte *)seed2.data());
多谢!