On Tuesday, October 18, 2016 at 11:17:09 PM UTC-4, Hoàng Hiệp wrote: > > 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 > publicKey.Save(CryptoPP::Base64Encoder( > new CryptoPP::StringSink(keyPair.publicKey)).Ref()); > privateKey.Save(CryptoPP::Base64Encoder( > new CryptoPP::StringSink(keyPair.privateKey)).Ref()); > > return keyPair; > } > > > This is Public key: > MIGdMA0GCSqGSIb3DQEBAQUAA4GLADCBhwKBgQDs648aASMAR9VprkzNVS7b > 36N1hiYvbBG0cdE0QkS3H/sc3+Ej92lGBQErpBu9LVhwN/beBX4QnbCn1eNS > rKoOzS4yqWlwOaCe0WLmFDHCn1cMTkX89cT4A0pcjBbY+0W7htxWcqHxEQH9x/AjQ9/ > 4blerh1i6/lLIo6hn2hB8kQIB > > Next I want to encrypt a string by public key (saved in string) > Code: > > 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()); > decoder.MessageEnd(); > > > RSA::PublicKey rsaPublick; > rsaPublick.BERDecodePublicKey(queue, false, (size_t) > queue.MaxRetrievable()); > > // BERDecodePrivateKey is a void function. Here's the only check > // we have regarding the DER bytes consumed. > CRYPTOPP_ASSERT(queue.IsEmpty()); > > > 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 > BERDecodePublicKey > to Load() but it really does not work.). Please just help me is wrong > somewhere. Learn well if you can guide me to fix. >
The key is malformed. For the analysis, see http://stackoverflow.com/a/40121565/608639 . Jeff -- -- 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 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 cryptopp-users+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.