I have problems using Crypto++ to save a RSA public key in string. When 
decoding the key, I always get a BERDecodeErr exception.

Code get public key and private key and is save to string

KeyPairBase64 GenerateKeyPair::generateKeyPair(unsigned int aKeySize) {
    KeyPairBase64 keyPair = KeyPairBase64();
    // Generate keys
    AutoSeededRandomPool rng;

    InvertibleRSAFunction parameters;
    parameters.GenerateRandomWithKeySize(rng, aKeySize);

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

    // save keys
            new CryptoPP::StringSink(keyPair.publicKey)).Ref());
            new CryptoPP::StringSink(keyPair.privateKey)).Ref());

    return keyPair;

This is Public key: 

Next I want to encrypt a string by public key (saved in string)

string RsaEncryptor::encryptor(string plaintext, string publicKey) {
    std::string cipher;
    AutoSeededRandomPool prng;
    try {
        ByteQueue queue;
        Base64Decoder decoder(new Redirector(queue));
        decoder.Put((const byte *) publicKey.data(), publicKey.size());

        RSA::PublicKey rsaPublick;
        rsaPublick.BERDecodePublicKey(queue, false, (size_t) 

        // BERDecodePrivateKey is a void function. Here's the only check
        // we have regarding the DER bytes consumed.

        bool valid = rsaPublick.Validate(prng, 3);
        if (!valid)
            cipher = "RSA private key is not valid";

        RSAES_OAEP_SHA_Encryptor e(rsaPublick);

        StringSource(plaintext, true,
                     new PK_EncryptorFilter(prng, e,
                                            new StringSink(cipher)
                     ) // PK_EncryptorFilter
        ); // StringSource
    catch (CryptoPP::Exception &e) {
        cipher = e.what();
    return cipher;

But  I always get a BERDecodeErr exception. (I've been tried change 
to Load() but it really does not work.). Please just help me is wrong 
somewhere. Learn well if you can guide me to fix.


You received this message because you are subscribed to the "Crypto++ Users" 
Google Group.
To unsubscribe, send an email to cryptopp-users-unsubscr...@googlegroups.com.
More information about Crypto++ and this group is available at 
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 cryptopp-users+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to