Use crypto ++ 5.5.2 to complete RSA encryption and decryption, and put the public key in the string instead of in the file.

Source: Internet
Author: User
This article from the small building to listen to spring rain blog overnight: http://hi.baidu.com/magic475/blog/item/e8b82139020ae622b8998f96.html Use crypto ++ 5.5.2 to complete RSA encryption and decryption
I have been busy for a week and basically completed the RSA encryption and decryption API encapsulation in the crypto ++ library! This week, I checked a lot of crypto ++ related information. I feel that this content is not good enough. It is either too simple or too general! This article hopes to bring some help to friends who use crypto ++, mainly in the source code. There is no time code! During the test, it seems that crypto ++ is very efficient. We hope more people will use crypto ++. In addition, the source code of crypto ++ is of great research value and is a classic material for in-depth study of C ++! You love C ++. Never miss it ~

The C ++ source code provided in this article has been compiled on Redhat Enterprise Server 5.0 and Windows XP platform, the compilers used are g ++ 3.4.6 and Visual C ++ 6.0 (SP 6 ). Because the code of the two platforms is not very different, only the code of the Linux platform is provided! If you need code for the Windows platform, you can ask me for it and leave your email address! If you encounter any problems during use, you are welcome to leave a message for me!

The source program mainly includes five files: myrsa. h. myrsa. CPP, Main. h, Main. CPP and makefile. The lib directory contains libcryptopp. a. The include directory contains all files in crypto ++ 5.5.2. h header file. By the way, the above five files are in the same directory as the lib and include directories! It should be noted that makefile borrowed a brother and forgot the name. Sorry :-)

Ha, paste the code!

/************************** Myrsa. h *******************************/<br/> # ifndef _ myrsa_h__< br/> # DEFINE _ myrsa_h__</P> <p> # include <string> </P> <p> # include "files. H "<br/> # include" filters. H "<br/> # include" hex. H "<br/> # include" randpool. H "<br/> # include" RSA. H "</P> <p> using namespace STD; <br/> using namespace cryptopp; </P> <p> class cmyrsa <br/>{< br/> Public: <br/> cmyrsa (void); <br/> virtual ~ Cmyrsa (void); </P> <p> // you must set the keylength 512,102 4, 2048... <br/> void generatekey (const unsigned int keylength, const char * seed, rsaes_oaep_sha_decryptor & priv, Region & pub); <br/> void generatekey (const unsigned int keylength, const char * seed, string & strpriv, string & strpub); </P> <p> // use public key to encrypt <br/> void encryptstring (const rsaes_oaep_sha_encryptor & Pub, const char * seed, const string & plaintext, string & ciphertext); <br/> void encryptstring (const string & strpub, const char * seed, const string & plaintext, string & ciphertext); </P> <p> // use private key to decrypt <br/> void decryptstring (const rsaes_oaep_sha_decryptor & priv, const string & ciphertext, string & plaintext); <br/> void decryptstring (const string & strpriv, const string & ciphertext, String & plaintext); </P> <p> PRIVATE: <br/> static randompool & RNG (void); </P> <p> PRIVATE: <br/> static randompool m_srandpool; <br/> }; </P> <p> # endif/* end of _ myrsa_h _ */</P> <p> /************ * ************* myrsa. CPP *******************************/<br/> # include "myrsa. H "</P> <p> cmyrsa: cmyrsa () <br/>{</P> <p >}</P> <p> cmyrsa ::~ Cmyrsa (void) <br/>{</P> <p >}</P> <p> void cmyrsa: generatekey (const unsigned int keylength, const char * seed, rsaes_oaep_sha_decryptor & priv, rsaes_oaep_sha_encryptor & pub) <br/>{< br/> randompool randpool; <br/> randpool. incorporateentropy (byte *) seed, strlen (SEED); </P> <p> // generate private key <br/> priv = rsaes_oaep_sha_decryptor (randpool, keylength ); </P> <p> // generate public key using private K Ey <br/> pub = rsaes_oaep_sha_encryptor (priv); <br/>}</P> <p> void cmyrsa: generatekey (const unsigned int keylength, const char * seed, string & strpriv, string & strpub) <br/>{< br/> randompool randpool; <br/> randpool. incorporateentropy (byte *) seed, strlen (SEED); </P> <p> // generate private key <br/> rsaes_oaep_sha_decryptor priv (randpool, keylength ); <br/> hexencoder privateencoder (New stringsink (St Rpriv); // added by the author of this blog: I found this code for a whole day! <Br/> priv. derencode (privateencoder); <br/> privateencoder. messageend (); </P> <p> // generate public key using private key <br/> rsaes_oaep_sha_encryptor pub (priv ); <br/> hexencoder publicencoder (New stringsink (strpub); <br/> pub. derencode (publicencoder); <br/> publicencoder. messageend (); <br/>}</P> <p> void cmyrsa: encryptstring (const rsaes_oaep_sha_encryptor & pub, const char * seed, const string & plaintext, string & ciphertext) <br/>{< br/> randompool randpool; <br/> randpool. incorporateentropy (byte *) seed, strlen (SEED); </P> <p> int maxmsglength = pub. fixedmaxplaintextlength (); <br/> for (INT I = plaintext. size (), j = 0; I> 0; I-= maxmsglength, J + = maxmsglength) <br/>{< br/> string partplaintext = plaintext. substr (J, maxmsglength); <br/> string partciphertext; <br/> stringsource (partplaintext, true, new pk_encryptorfilter (randpool, pub, new hexencoder (New stringsink (partciphertext); <br/> ciphertext + = partciphertext; <br/>}</P> <p> void cmyrsa: encryptstring (const string & strpub, const char * seed, const string & plaintext, string & ciphertext) <br/>{< br/> stringsource publickey (strpub, true, new hexdecoder); <br/> rsaes_oaep_sha_encryptor pub (publickey ); </P> <p> randompool randpool; <br/> randpool. incorporateentropy (byte *) seed, strlen (SEED); </P> <p> int maxmsglength = pub. fixedmaxplaintextlength (); <br/> for (INT I = plaintext. size (), j = 0; I> 0; I-= maxmsglength, J + = maxmsglength) <br/>{< br/> string partplaintext = plaintext. substr (J, maxmsglength); <br/> string partciphertext; <br/> stringsource (partplaintext, true, new pk_encryptorfilter (randpool, pub, new hexencoder (New stringsink (partciphertext); <br/> ciphertext + = partciphertext; <br/>}</P> <p> void cmyrsa: decryptstring (const rsaes_oaep_sha_decryptor & priv, const string & ciphertext, string & plaintext) <br/>{< br/> // indicate the ciphertext in hexcode <br/> int ciphertextlength = priv. fixedciphertextlength () * 2; <br/> for (INT I = ciphertext. size (), j = 0; I> 0; I-= ciphertextlength, J + = ciphertextlength) <br/>{< br/> string partciphertext = ciphertext. substr (J, ciphertextlength); <br/> string partplaintext; <br/> stringsource (partciphertext, true, new hexdecoder (New pk_decryptorfilter (RNG (), priv, new stringsink (partplaintext); <br/> plaintext + = partplaintext; <br/>}</P> <p> void cmyrsa :: decryptstring (const string & strpriv, const string & ciphertext, string & plaintext) <br/>{< br/> stringsource privkey (strpriv, true, new hexdecoder ); <br/> rsaes_oaep_sha_decryptor priv (privkey); </P> <p> // indicate the ciphertext in hexcode <br/> int ciphertextlength = priv. fixedciphertextlength () * 2; <br/> for (INT I = ciphertext. size (), j = 0; I> 0; I-= ciphertextlength, J + = ciphertextlength) <br/>{< br/> string partciphertext = ciphertext. substr (J, ciphertextlength); <br/> string partplaintext; <br/> stringsource (partciphertext, true, new hexdecoder (New pk_decryptorfilter (RNG (), priv, new stringsink (partplaintext); <br/> plaintext + = partplaintext; <br/>}</P> <p> randompool & cmyrsa :: RNG (void) <br/>{< br/> return m_srandpool; <br/>}</P> <p> randompool cmyrsa: m_srandpool; </P> <p>/**************************** main. h *******************************/<br/> # ifndef _ main_h __< br/> # DEFINE _ main_h __</P> <p> # endif/* end of _ main_h _ */</P> <p >/*************************** main. CPP *******************************/<br/> # include <unistd. h> </P> <p> # include <iostream> </P> <p> # include "Main. H "<br/> # include" myrsa. H "</P> <p>/***** static variables *****/<br/> static rsaes_oaep_sha_encryptor g_pub; <br/> static rsaes_oaep_sha_decryptor g_priv; <br/> static string g_strpub; <br/> static string g_strpriv; </P> <p> int main (INT argc, char * argv []) <br/>{< br/> try <br/> {<br/> char seed [1024], message [1024], messageseed [1024]; <br/> unsigned int keylength; <br/> cmyrsa myrsa; </P> <p> cout <"Key Length in bits :"; <br/> CIN> keylength; </P> <p> cout <"/nrandom seed:"; <br/> WS (CIN ); <br/> cin. getline (seed, 1024); </P> <p> cout <"/nmessage:"; <br/> WS (CIN); <br/> cin. getline (message, 1024); </P> <p> cout <"/nrandom message seed:"; <br/> WS (CIN); <br/> cin. getline (messageseed, 1024); </P> <p> myrsa. generatekey (keylength, seed, g_priv, g_pub); <br/> // myrsa. generatekey (keylength, seed, g_strpriv, g_strpub); </P> <p> // If generate key in rsaes_oaep_sha_encryptor and rsaes_oaep_sha_decryptor, please note four lines below <br/>/* <br/> cout <"g_strpub =" <g_strpub <Endl; <br/> cout <Endl; <br/> cout <"g_strpriv =" <g_strpriv <Endl; <br/> cout <Endl; <br/> */</P> <p> string plaintext (Message); <br/> string ciphertext; <br/> myrsa. encryptstring (g_pub, messageseed, plaintext, ciphertext); <br/> // myrsa. encryptstring (g_strpub, messageseed, plaintext, ciphertext); <br/> cout <"/nciphertext:" <ciphertext <Endl; <br/> cout <Endl; </P> <p> string decrypted; <br/> myrsa. decryptstring (g_priv, ciphertext, decrypted); <br/> // myrsa. decryptstring (g_strpriv, ciphertext, decrypted); <br/> cout <"/ndecrypted:" <decrypted <Endl; <br/> return 0; <br/>}< br/> catch (cryptopp: exception const & E) <br/>{< br/> cout <"/ncryptopp: exception caught: "<E. what () <Endl; <br/> return-1; <br/>}< br/> catch (STD: exception const & E) <br/>{ <br/> cout <"/nstd: exception caught:" <E. what () <Endl; <br/> return-2; <br/>}< br/> return-3; <br/>}</P> <p>/*************************** makefile * *****************************/<br/> # the executable file Name. <br/> program: = myrsa </P> <p> # the directories in which source files reside. <br/> srcdirs: =. # current directory </P> <p> # The source file types (headers excluded ). <br/> srcexts: =. CPP </P> <p> # The flags used by the CPP (man CPP for more ). <br/> cppflags: = </P> <p> # The compiling flags used only for C. <br/> # if it is a C ++ program, no need to set these flags. <br/> # if it is a C and C ++ merging program, set these flags for the c parts. </P> <p> cflags: = </P> <p> cflags + = </P> <p> # The compiling flags used only for C ++. <br/> # if it is a C program, no need to set these flags. <br/> # if it is a C and C ++ merging program, set these flags for the c ++ parts. </P> <p> cxxflags: =-g-O2-I. /include </P> <p> cxxflags + = </P> <p> # The Library and the link options (C and C ++ common ). </P> <p> ldflags: =-L. /lib-lcryptopp </P> <p> ldflags + = </P> <p> # implict section: Change the following only when necessary. </P> <p >##================================== ========================================================== ========</P> <p> # The C program compiler. uncomment it to specify yours explicitly. </P> <p> # cc = GCC </P> <p> # The C ++ program compiler. uncomment it to specify yours explicitly. </P> <p> cxx = g ++ </P> <p> # uncomment the 2 lines to compile C programs as C ++ ones. </P> <p> CC =$ (cxx) </P> <p> cflags =$ (cxxflags) </P> <p> # The command used to delete file. </P> <p> Rm = Rm-F </P> <p> # stable section: Usually no need to be changed. but you can add more. </P> <p >##================================== ========================================================== =====</P> <p> shell =/bin/sh </P> <p> sources = $ (foreach D, $ (srcdirs), $ (wildcard $ (addprefix $ (d)/*, $ (srcexts) </P> <p> objs = $ (foreach X, $ (srcexts),/<br/> $ (patsubst % $ (x), %. o, $ (filter % $ (x), $ (sources) </P> <p> deps = $ (patsubst %. o, %. d, $ (objs) </P> <p>. phony: All objs clean cleanall rebuild </P> <p> All: $ (Program) </P> <p> # rules for creating the dependency files (. d ). </P> <p> # --------------------------------------------------- </P> <p> %. d: %. c </P> <p >@ (CC)-Mm-MD $ (cflags) $ </P> <p> %. d: %. c </P> <p >@ (CC)-Mm-MD $ (cxxflags) $ </P> <p> %. d: %. CC </P> <p >@ (CC)-Mm-MD $ (cxxflags) $ </P> <p> %. d: %. CPP </P> <p >@ (CC)-Mm-MD $ (cxxflags) $ </P> <p> %. d: %. CPP </P> <p >@ (CC)-Mm-MD $ (cxxflags) $ </P> <p> %. d: %. c ++ </P> <p >@ (CC)-Mm-MD $ (cxxflags) $ </P> <p> %. d: %. CP </P> <p >@ (CC)-Mm-MD $ (cxxflags) $ </P> <p> %. d: %. cxx </P> <p >@ (CC)-Mm-MD $ (cxxflags) $ </P> <p> # rules for producing the objects. </P> <p> # ------------------------------------------------- </P> <p> objs: $ (objs) </P> <p> %. o: %. c </P> <p> $ (CC)-C $ (cppflags) $ (cflags) $ </P> <p> %. o: %. c </P> <p> $ (cxx)-C $ (cppflags) $ (cxxflags) $ </P> <p> %. o: %. CC </P> <p> $ (cxx)-C $ (cppflags) $ (cxxflags) $ </P> <p> %. o: %. CPP </P> <p> $ (cxx)-C $ (cppflags) $ (cxxflags) $ </P> <p> %. o: %. CPP </P> <p> $ (cxx)-C $ (cppflags) $ (cxxflags) $ </P> <p> %. o: %. c ++ </P> <p> $ (cxx-C $ (cppflags) $ (cxxflags) $ </P> <p> %. o: %. CP </P> <p> $ (cxx)-C $ (cppflags) $ (cxxflags) $ </P> <p> %. o: %. cxx </P> <p> $ (cxx)-C $ (cppflags) $ (cxxflags) $ </P> <p> # rules for producing the executable. </P> <p> # -------------------------------------------- </P> <p >$ (Program): $ (objs) </P> <p> ifeq ($ (Strip $ (srcexts )),. c) # C file </P> <p> $ (CC)-o $ (Program) $ (objs) $ (ldflags) </P> <p> else # C ++ file </P> <p> $ (cxx)-o $ (Program) $ (objs) $ (ldflags) </P> <p> endif </P> <p>-include $ (deps) </P> <p> Rebuild: clean all </P> <p> clean: <br/> @ $ (RM )*. O *. d <br/> cleanall: Clean <br/> @ $ (RM) $ (Program) program (programpolic.exe

 

If you are still writing the dynamic link library of this encryption class library, I suggest you do not waste time! Already available and very easy to use, web site: http://download.csdn.net/source/1931336

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.