Decrypting Base64 RSA Cipher with CryptoPP

84 Views Asked by At

I have created the RSA keypair elsewhere in the same manner as shown in the documentation.

AutoSeededRandomPool rng;

InvertibleRSAFunction params;
params.GenerateRandomWithKeySize(rng, 3072);

RSA::PrivateKey privateKey(params);
RSA::PublicKey  publicKey(params);

Below is the decryption function which accepts a base64 encoded cipher, which was encrypted using the public key and encoded, and accepts the previously created private key as well. This also comes from the aforementioned documentation.

const string RSA_Helpers::decrypt(const string& base64_cipher, const CryptoPP::RSA::PrivateKey privateKey) const
{
    using namespace CryptoPP;

    string cleartext = "";
    AutoSeededRandomPool RNG;
    RSAES_OAEP_SHA_Decryptor decryptor(privateKey);
    StringSource cleartext_str_src(base64_cipher, true, 
        new PK_DecryptorFilter(RNG, decryptor, new StringSink(cleartext))
    );

    return cleartext;
}

However, upon execution, an unhandled exception from CryptoPP is thrown on the line before the return: CryptoPP::InvalidCiphertext at memory location . . .

I have also tried decoding the cipher from base64, with no luck.

const string RSA_Helpers::decrypt(const string& base64_cipher, const CryptoPP::RSA::PrivateKey privateKey) const
{
    using namespace CryptoPP;
    string cipher = "";
    StringSource decoder(base64_cipher, true, new Base64Decoder(new StringSink(cipher)));

    string cleartext = "";
    AutoSeededRandomPool RNG;
    RSAES_OAEP_SHA_Decryptor decryptor(privateKey);
    StringSource cleartext_str_src(cipher, true, 
        new PK_DecryptorFilter(RNG, decryptor, new StringSink(cleartext))
    );

    return cleartext;
}

Any help as to how the cipher can be properly decrypted is appreciated.

0

There are 0 best solutions below