Okay so I'm messing around with the RSA section of the CryptoPP library and 
I have a working (demonstration only) function I have written which will 
load the local RSA pubkey, validate it, encrypt a string and then output 
the string into a filesink. I know that validating RSA keys is incredibly 
important as a malformed key could lead to recoverable data, so I am using 
the "Validate(rng, level)" function to validate the key. However, the way I 
am doing it "loads" the key two times into memory, ands seems to me like a 
inefficient way of doing things which usually means that I have done 
something wrong. I've attached the function and hopefully someone can tell 
me how to validate the public key and also use it to encrypt without 
effectively loading it two times. Either that or I am misunderstanding 
something! Thank you ahead of time ~Patrick

std::string Account::encryptData(const char *pubKeyPath, std::string data)
{
    CryptoPP::FileSource pubFile(pubKeyPath, true, new CryptoPP::HexDecoder
);
    CryptoPP::RSAES_OAEP_SHA_Encryptor pubKey(pubFile);

    if (data.size() > pubKey.FixedMaxPlaintextLength()) {
        std::cout << "[ANDataStore] Cannot encrypt string, it is too long\n"
;
        return "";
    }

    CryptoPP::AutoSeededRandomPool randPool;

    // Check that the public key is valid -- !!!!!!!THIS IS WHERE I LOAD 
THE KEY TWO TIMES!!!!!!!!!
    CryptoPP::RSA::PublicKey publicKey;
    publicKey.Load(pubFile);
    publicKey.Validate(randPool, 3);

    std::string result;
    CryptoPP::StringSource(data, true, new CryptoPP::PK_EncryptorFilter(
randPool, pubKey, new CryptoPP::HexEncoder(new CryptoPP::StringSink(result
))));

    std::cout << "Result:\n" << result << std::endl;
    return result;
}


-- 
-- 
You received this message because you are subscribed to the "Crypto++ Users" 
Google Group.
To unsubscribe, send an email to [email protected].
More information about Crypto++ and this group is available at 
http://www.cryptopp.com.
--- 
You received this message because you are subscribed to the Google Groups 
"Crypto++ Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/d/optout.

Reply via email to