1, Asymmetric encryption RSA:
(1) Party B generates two keys (public and private). The public key is public and can be obtained by anyone, and the private key is confidential.
(2) Party A obtains the public key of party B, then uses it to encrypt the information.
(3) Party B obtains the encrypted information, decrypts with the private key.
2, use Cryptopp to implement RSA:
Cryptopp is a very complete set of encryption and decryption open source solutions, how to use this is not much to say, please google yourself.
#include ". \cryptopp562\randpool.h "#include ". \cryptopp562\osrng.h "#include ". \cryptopp562\rsa.h "//Automatically generate random data byte seed[1024] = " "; Autoseededrandompool rnd;rnd. Generateblock (seed, sizeof (Seed));p rintf ("seed = %s\n", (char *) Seed, strlen (( char *));//Generate encrypted high-quality pseudo-random bytes The entropy randompool randpool;randpool.put (seed, sizeof (Seed)) after the seeding pool is integrated;// The string to be encrypted string message = "Http://my.oschina.net/xlplbo/blog";p rintf ("message = %s, length = %d\n ", message.c_str (), strlen (Message.c_str ()));//use OAEP mode Rsaes_oaep_sha_ Decryptor pri (randpool, sizeof (Seed)); Rsaes_oaep_sha_encryptor pub (PRI);p rintf ("max plaintext length = %d,%d\n", pri. Fixedmaxplaintextlength (), pub. Fixedmaxplaintextlength ());//The text to be encrypted cannot be greater than the maximum encryption length if (pub. Fixedmaxplaintextlength () > message.length ()) {string chilper; Stringsource (Message, true, new&nbsP Pk_encryptorfilter (Randpool, pub, new stringsink (chilper)));p rintf ("chilper = %s, length = %d\n ", chilper.c_str (), strlen (Chilper.c_str ()));string txt; Stringsource (Chilper, true, new pk_decryptorfilter (randpool, pri, new stringsink (TXT))); printf ("txt = %s, length = %d\n", txt.c_str (), strlen (Txt.c_str ()));} Use pkcs1v15 mode Rsaes_pkcs1v15_decryptor pri1 (randpool, sizeof (Seed)); Rsaes_pkcs1v15_encryptor pub1 (PRI1);p rintf ("max plaintext length = %d,%d\n", Pri1. Fixedmaxplaintextlength (), pub1. Fixedmaxplaintextlength ());//The text to be encrypted cannot be greater than the maximum encryption length if (pub1. Fixedmaxplaintextlength () > message.length ()) {string chilper; Stringsource (Message, true, new pk_encryptorfilter (Randpool, pub1,new stringsink ( Chilper)));p rintf ("chilper = %s, length = %d\n", chilper.c_str (), strlen (Chilper.c_str ()));string txt; Stringsource (Chilper, true, new pk_decryptorfilter (randpool, pri1, new Stringsink (TXT)));p rintf ("txt = %s, length = %d\n", txt.c_str (), strlen ( Txt.c_str ()));}
Cryptopp provides two kinds of RSA padding mode, respectively OAEP and pk1v15,padding mode and security is actually closely linked, interested friends can go to understand.
Note that the size of the seed determines the length of the text that can be encrypted, and it is possible to run the view results by modifying the size of the seed. The larger the seed, the longer the encryption and decryption time, more than 20,481 will be able to clearly feel the time is very long, generally use 1024 is already enough security, refer to the following table:
Of course Cryptopp not only provides encryption and decryption algorithms, but also provides many easy-to-use tools such as Autoseededrandompool, Randompool, Stringsource,stringsink,socketsource, Socketsink,filesource,filesink such as class, Rsaes_oaep_sha_decryptor, rsaes_oaep_sha_encryptor and other macro definitions, the specific use of the method please read the source code.
Reference Links:
http://www.cryptopp.com/
Http://www.ruanyifeng.com/blog/2013/06/rsa_algorithm_part_one.html
Http://www.ruanyifeng.com/blog/2013/07/rsa_algorithm_part_two.html
crypto++ Application: Asymmetric encryption RSA