c++ - 使用 RSA key 时,rjindael.cpp、函数 AESNI_Enc_Block 崩溃

标签 c++ encryption crypto++

我是 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/

相关文章:

c++ - 查找 std::vector 中的最后一个非零元素

java - 如何在Java中解密私钥(不使用BC openssl)

c++ - AES 和 key 长度错误

c++ - Crypto++ 验证大文件签名

c++ - 在 C++03 中模拟 =delete 以限制复制/赋值操作的最简单方法是什么?

c++ - CUDA 在使用函数指针时将主机函数作为内核启动

c++ - 我需要知道如何扩展程序,以便它读取并转换所有输入数字,直到标准输入文件结束

c++ - c++中的加密问题

c++ - 加密程序错误

c++ - 生成的加密字符串在 PyCrypto 和 Crypto++ 中的大小不同