Advertisement
Kyojin96

Crypto++ side

Feb 11th, 2017
535
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.27 KB | None | 0 0
  1. void CryptLib::GenerateRandomRSAKeyPair(string& publicKey, string& privateKey, int keySize)
  2.     {
  3.         //Generate params
  4.         AutoSeededRandomPool rng;
  5.         InvertibleRSAFunction params;
  6.         //256 - 4096
  7.         params.Initialize(rng, 512);
  8.  
  9.         //Generate Keys
  10.         RSA::PrivateKey privKey(params);
  11.         RSA::PublicKey pubKey(params);
  12.  
  13.         //Encode keys to Base64
  14.         string encodedPriv, encodedPub;
  15.  
  16.         Base64Encoder privKeySink(new StringSink(encodedPriv));
  17.         privKey.DEREncode(privKeySink);
  18.         privKeySink.MessageEnd();
  19.  
  20.         Base64Encoder pubKeySink(new StringSink(encodedPub));
  21.         pubKey.DEREncode(pubKeySink);
  22.         pubKeySink.MessageEnd();
  23.  
  24.         publicKey = encodedPub;
  25.         privateKey = encodedPriv;
  26.     }
  27.  
  28.     const string CryptLib::EncryptRSA(const string& plaintext, const string& publicKey)
  29.     {
  30.         //Decode public key
  31.         RSA::PublicKey pbKeyDecoded;
  32.         StringSource ss2(publicKey, true, new Base64Decoder);
  33.         pbKeyDecoded.BERDecode(ss2);
  34.  
  35.         //Encrypt message
  36.         //Encrypt string
  37.         Integer e = Integer((const byte*)plaintext.data(), plaintext.size());
  38.  
  39.         Integer crypted = pbKeyDecoded.ApplyFunction(e);
  40.  
  41.         //Encode it as base64
  42.         size_t len = crypted.MinEncodedSize();
  43.         string crpt;
  44.         crpt.resize(len);
  45.         crypted.Encode((byte*)crpt.data(), crpt.size(), Integer::UNSIGNED);
  46.  
  47.         string encodedCrypt;
  48.         StringSource ss((byte*)crpt.c_str(), crpt.length(), true,
  49.             new Base64Encoder(
  50.                 new StringSink(encodedCrypt), false
  51.             )
  52.         );
  53.  
  54.         //return it
  55.         return encodedCrypt;
  56.     }
  57.  
  58.     const string CryptLib::DecryptRSA(const string& cipher, const string& privateKey)
  59.     {
  60.         //decode private key
  61.         RSA::PrivateKey pvKeyDecoded;
  62.         StringSource ss3(privateKey, true, new Base64Decoder);
  63.         pvKeyDecoded.BERDecode(ss3);
  64.  
  65.         //decrypt the cipher
  66.         string decodedCipher;
  67.         StringSource sb(cipher, true,
  68.             new Base64Decoder(
  69.                 new StringSink(decodedCipher)
  70.             )
  71.         );
  72.  
  73.         //Convert binary string to Integer
  74.         Integer normalCipher((byte*)decodedCipher.c_str(), decodedCipher.length());
  75.  
  76.         //feed the recovered cipher
  77.         AutoSeededRandomPool rng;
  78.         Integer d = pvKeyDecoded.CalculateInverse(rng, normalCipher);
  79.  
  80.         //decode it to string
  81.         string recovered;
  82.         size_t req = d.MinEncodedSize();
  83.         recovered.resize(req);
  84.         d.Encode((byte*)recovered.data(), recovered.size());
  85.  
  86.         //return it
  87.         return recovered;
  88.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement