Save PublicKey to string and load from it

41 views
Skip to first unread message

Наталья

unread,
Jun 2, 2019, 5:53:03 AM6/2/19
to Crypto++ Users
Hi all!

I have an ordinary, but easy, problem. Need to save public key into string and load from this string.
Saving and loading are successed in the test code, but when I try to create decryptor from mey new public key (which are loaded wrom string) - i had an exception "Missing required parameter Primel1".
Wich parameter i need to add? What is the problem with this code?

In debug I see correct value inside all strings...

  1. //for generate IV and Key
  2. #include "D:/Crypto++/cryptopp820/osrng.h"
  3. using CryptoPP::AutoSeededRandomPool;
  4.  
  5. #include <string>
  6. using std::string;
  7.  
  8. //for exceptions
  9. #include "D:/Crypto++/cryptopp820/cryptlib.h"
  10. using CryptoPP::Exception;
  11.  
  12. //for hex viewing
  13. #include "D:/Crypto++/cryptopp820/hex.h"
  14. using CryptoPP::HexDecoder;
  15. using CryptoPP::HexEncoder;
  16.  
  17. //converter input into cipher
  18. #include "D:/Crypto++/cryptopp820/filters.h"
  19. using CryptoPP::StringSink;
  20. using CryptoPP::StreamTransformation;
  21. using CryptoPP::StreamTransformationFilter;
  22.  
  23. //add AES
  24. #include "D:/Crypto++/cryptopp820/aes.h"
  25. using CryptoPP::AES;
  26.  
  27. #include "D:/Crypto++/cryptopp820/modes.h"
  28. using CryptoPP::CBC_Mode;
  29.  
  30. #include "D:/Crypto++/cryptopp820/md5.h"
  31. using CryptoPP::MD5;
  32.  
  33. #include "D:/Crypto++/cryptopp820/blowfish.h"
  34. #include "D:/Crypto++/cryptopp820/rsa.h"
  35. #include "D:/Crypto++/cryptopp820/eccrypto.h"
  36. #include "D:/Crypto++/cryptopp820/base64.h"
  37. using CryptoPP::Blowfish;
  38. #include <iostream>
  39. using namespace std;
  40. using namespace CryptoPP;
  41.  
  42.  
  43. int main()
  44. {
  45.     //create public+private keys
  46.     //encrypt some text
  47.     //pack public key into the string or vector char
  48.     //unpack public key and decrypt text
  49.  
  50.  
  51.     std::string testText = "Text text";
  52.     std::string encryptText, decryptText, for_save_key, packedKey, unpackedKey;
  53.  
  54.     AutoSeededRandomPool rng(true);
  55.     InvertibleRSAFunction params;
  56.     params.GenerateRandomWithKeySize(rng, 3072);
  57.  
  58.     RSA::PrivateKey privateKey(params);
  59.     RSA::PublicKey publicKey(params);
  60.  
  61.     RSAES_OAEP_SHA_Encryptor encryptor(publicKey);
  62.     StringSource(testText, truenew PK_EncryptorFilter(rng, encryptor, new StringSink(encryptText)));
  63.  
  64.     //try to pack public key
  65.     RSA::PublicKey newPublicKey;
  66.     try
  67.     {
  68.         StringSink tmp_key(for_save_key);
  69.         publicKey.Save(tmp_key);
  70.         newPublicKey.Load(CryptoPP::StringStore((const byte*)for_save_key.data(), for_save_key.size()).Ref());
  71.     }
  72.     catch (const BERDecodeErr& ex)
  73.     {
  74.         cerr << ex.what() << endl;
  75.     }
  76.  
  77.     try
  78.     {
  79.         RSAES_OAEP_SHA_Decryptor decryptor(newPublicKey);
  80.         StringSource(encryptText, truenew PK_DecryptorFilter(rng, decryptor, new StringSink(decryptText)));
  81.     }
  82.     catch (InvalidArgument& ex)
  83.     {
  84.         cerr << ex.what() << endl;
  85.     }  
  86.  
  87.     std::cout << "Decrypt text: " << decryptText << endl;
  88.  
  89.     system("pause");
  90.     return 0;
  91. }

Jeffrey Walton

unread,
Jun 2, 2019, 6:11:29 AM6/2/19
to Наталья, Crypto++ Users
The decryptor should use the private key, not the public key. This is incorrect:

RSAES_OAEP_SHA_Decryptor decryptor(newPublicKey);

Jeff

Jeffrey Walton

unread,
Jun 2, 2019, 6:14:26 AM6/2/19
to Наталья, Crypto++ Users
On Sun, Jun 2, 2019 at 5:53 AM Наталья <lire...@gmail.com> wrote:
>
> I have an ordinary, but easy, problem. Need to save public key into string and load from this string.
> Saving and loading are successed in the test code, but when I try to create decryptor from mey new public key (which are loaded wrom string) - i had an exception "Missing required parameter Primel1".
> Wich parameter i need to add? What is the problem with this code?

You might also be interested in
https://cryptopp.com/wiki/RSA_Cryptography and
https://www.cryptopp.com/wiki/Keys_and_Formats on the wiki.

Jeff

Nataliia

unread,
Jun 2, 2019, 6:37:06 AM6/2/19
to Crypto++ Users
U are my hero!
How I could be so inattentive?!
All works properly.

Thanks a lot!

воскресенье, 2 июня 2019 г., 13:11:29 UTC+3 пользователь Jeffrey Walton написал:
Reply all
Reply to author
Forward
0 new messages