Implementing RSA encryption and decryption binary data with crypto++

Source: Internet
Author: User

On the internet a large talk about how to add the decryption string, looking for a big half-day also did not find to speak plus decryption binary data, so I studied the next, share to everyone.

Add decryption function:

#include <rsa.h> #include <randpool.h> #include <filters.h>void generatersakey (byte seed[], size_t Seedlen, size_t keylen,byte privatekey[], size_t &privatekeylen,byte publickey[], size_t &publicKeyLen) { Cryptopp::randompool randompool;randompool.put (seed, Seedlen); Cryptopp::rsaes_oaep_sha_decryptor decryptor (Randompool, 1024); Cryptopp::arraysink Decarr (Privatekey, Privatekeylen);d ecryptor. Derencode (Decarr);d ecarr.messageend ();p Rivatekeylen = Decarr.totalputlength (); Cryptopp::rsaes_oaep_sha_encryptor encryptor (decryptor); Cryptopp::arraysink Encarr (PublicKey, Publickeylen); encryptor. Derencode (Encarr); Encarr.messageend ();p Ublickeylen = Encarr.totalputlength ();} void Rsaencryptdata (Byte seed[], size_t Seedlen, byte publickey[], size_t publickeylen,byte plaindata[], size_t plainData Len, Byte cipherdata[], size_t &cipherdatalen) {Cryptopp::arraysource Keyarr (PublicKey, Publickeylen, true); Cryptopp::rsaes_oaep_sha_encryptor Enc;enc. AccessKey (). Load (Keyarr); Cryptopp::Randompool randompool;randompool.put (seed, Seedlen); size_t Putlen = 0;size_t Fixedlen = Enc. Fixedmaxplaintextlength (); for (size_t i = 0; i < plaindatalen; i + = Fixedlen) {size_t len = Fixedlen < (plaindatalen -i)? Fixedlen: (plaindatalen-i); Cryptopp::arraysink *dstarr = new Cryptopp::arraysink (CipherData + Putlen, Cipherdatalen-putlen); Cryptopp::arraysource Source (Plaindata + i, Len, True, new Cryptopp::P k_encryptorfilter (Randompool, ENC, Dstarr)); Putlen + = Dstarr->totalputlength ();} Cipherdatalen = Putlen;} void Rsadecryptdata (Byte seed[], size_t Seedlen, byte privatekey[], size_t privatekeylen,byte cipherdata[], size_t cipher Datalen, Byte plaindata[], size_t &plaindatalen) {Cryptopp::arraysource Keyarr (Privatekey, PrivateKeyLen, true); Cryptopp::rsaes_oaep_sha_decryptor Dec;dec. AccessKey (). Load (Keyarr); Cryptopp::randompool randompool;randompool.put (seed, Seedlen); size_t Putlen = 0;size_t Fixedlen = Dec. Fixedciphertextlength (); for (size_t i = 0; i < Cipherdatalen; I+ = Fixedlen) {size_t len = Fixedlen < (cipherdatalen-i)? Fixedlen: (Cipherdatalen-i); Cryptopp::arraysink *dstarr = new Cryptopp::arraysink (Plaindata + Putlen, Plaindatalen-putlen); Cryptopp::arraysource Source (CipherData + i, Len, True, new Cryptopp::P k_decryptorfilter (Randompool, Dec, Dstarr)); Putlen + = Dstarr->totalputlength ();} Plaindatalen = Putlen;}

  

Test function:

#include <iostream> #include <string> #include "CryptRsa.h" using namespace std;using namespace Cryptopp;int Main (int argc, char *argv) {try{string plaintext = "{0fa152b4-d176-4d95-8363-ba9dfc787651}"; cout << PlainText < < endl;cout << plaintext.length () << endl;byte seed[] = {0xCA, 0xDA, 0x63, 0xEC, 0x9b, 0x89, 0x40, 0xDE, 0 x8e, 0x64, 0x94, 0xe8, 0x79, 0xEA, 0x32, 0x9E};size_t privatekeylen = 2048;byte *privatekey = new Byte[privatekeylen];siz e_t Publickeylen = 2048;byte *publickey = new Byte[publickeylen]; Generatersakey (seed, sizeof (seed), 1024x768, Privatekey, Privatekeylen, PublicKey, Publickeylen); size_t Cipherdatalen = 2048;byte *cipherdata = new Byte[cipherdatalen]; Rsaencryptdata (seed, sizeof (seed), PublicKey, Publickeylen, (byte*) plaintext.c_str (), Plaintext.length (), CipherData , Cipherdatalen); size_t Plaindatalen = Plaintext.length () * 2;byte *plaindata = new Byte[plaindatalen]; Rsadecryptdata (seed, sizeof (seed), Privatekey, Privatekeylen,cipherdata, Cipherdatalen, Plaindata, Plaindatalen);p Laindata[plaindatalen] = ' n '; cout << (char*) plaindata << Endl; cout << plaindatalen << endl;delete[] plaindata;delete[] cipherdata;delete[] publickey;delete[] PrivateKey ;} catch (Std::exception &e) {cout << e.what () << Endl;} Cin.get (); return 0;}

  

Implementing RSA encryption and decryption binary data with crypto++

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.