我是 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());
非常感谢!
关于c++ - 使用 RSA key 时,rjindael.cpp、函数 AESNI_Enc_Block 崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20655901/