Вопрос

I am new to crypto++ and just follow an example from its cryptest project (test.cpp). I generated both public and private keys using RSA. I try to use the keys, exactly as in the example. It works perfectly well in crypto++ own project and generates unhandled exception in mine. Below is the basic code, which breaks at decryption stage. any suggestions on why?

#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;
}

In my project the program breaks at line

StringSource(ciphertext, true, new HexDecoder(new PK_DecryptorFilter(GlobalRNG(), priv, new StringSink(result))));

And debugger points to rjindael.cpp, function AESNI_Enc_Block, line 1005.

Это было полезно?

Решение

As pointed out by Yakk, I missed initialisation of the variable s_globalRNG. The following code fixes my problem.

//just below main()

std::string seed2 = IntToString(time(NULL));
seed2.resize(16);
s_globalRNG.SetKeyWithIV((byte *)seed2.data(), 16, (byte *)seed2.data());

thanks a lot!

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top