• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>
            流量統(tǒng)計(jì):
            Rixu Blog (日需博客)
            日需博客,每日必需來踩踩哦..
            posts - 108,comments - 54,trackbacks - 0
                  從官方網(wǎng)下載的Crypto++庫是開源的,只有源文件和幾個可以生成lib、dll的工程,以及一個使用的例子工程,因此希望生成自己建的工程能使用的SDK。 
              
            1.編譯鏈接生成cryptlib.lib 
            打開cryptest.sln,分別在Debug模式和Release模式下編譯鏈接cryptlib工程,成功后會在cryptopp54\\Win32\\output\\debug和cryptopp54\\Win32\\output\\release下生成cryptlib.lib文件。作者當(dāng)時用的是Crypto++ 5.4版本。 
            Build時方法是,右擊Solution Explorer中的cryptlib工程,單擊build。第一次時它會報(bào)錯說“d:\\cryptopp54\\adler32.cpp(3) : fatal error C1033: cannot open program database ’d:\\cryptopp54\\win32\\cryptlib\\debug\\vc80.idb’”,沒關(guān)系,按這樣再build一次,就可以build成功了。 
              
            2.建立Crypto++ SDK 
            在C:\\Program Files\\中新建文件夾,取名“CryptoPP”,里面新建文件夾“include”、“lib”,在“lib”中新建文件夾“debug”、“release”。將Crypto++庫中的所有頭文件復(fù)制到“include”文件夾中,再將上面生成的兩個cryptlib.lib分別復(fù)制到“debug”和“release”中。 
              
            三.RSA加解密 
              
            1.在VS 2005中新建Win32 Console Application工程,建立空的工程。完成后新建文件main.cpp,里面源碼如下: 
              
            #include \"randpool.h\" 
            #include \"rsa.h\" 
            #include \"hex.h\" 
            #include \"files.h\" 
            #include <iostream> 
              
            using namespace std; 
            using namespace CryptoPP; 
              
            #pragma comment(lib, \"cryptlib.lib\") 
              
            //------------------------ 
            // 函數(shù)聲明 
            //------------------------ 
            void GenerateRSAKey(unsigned int keyLength, const char *privFilename, const char *pubFilename, const char *seed); 
            string RSAEncryptString(const char *pubFilename, const char *seed, const char *message); 
            string RSADecryptString(const char *privFilename, const char *ciphertext); 
            RandomPool & GlobalRNG(); 
              
            //------------------------ 
            // 主程序 
            //------------------------ 
            void main() 

                char priKey[128] = {0}; 
                char pubKey[128] = {0}; 
                char seed[1024] = {0}; 
              
                // 生成 RSA 密鑰對 
                strcpy(priKey, \"pri\"); // 生成的私鑰文件 
                strcpy(pubKey, \"pub\"); // 生成的公鑰文件 
                strcpy(seed, \"seed\"); 
                GenerateRSAKey(1024, priKey, pubKey, seed); 
              
                // RSA 加解密 
                char message[1024] = {0}; 
                cout<<\"Origin Text:\\t\"<<\"Hello World!\"<<endl<<endl; 
                strcpy(message, \"Hello World!\"); 
                string encryptedText = RSAEncryptString(pubKey, seed, message); // RSA 加密 [Page]
                cout<<\"Encrypted Text:\\t\"<<encryptedText<<endl<<endl; 
                string decryptedText = RSADecryptString(priKey, encryptedText.c_str()); // RSA 解密 
                cout<<\"Decrypted Text:\\t\"<<decryptedText<<endl<<endl; 

              
            //------------------------ 
            // 生成RSA密鑰對 
            //------------------------ 
            void GenerateRSAKey(unsigned int keyLength, const char *privFilename, const char *pubFilename, const char *seed) 

                   RandomPool randPool; 
                   randPool.Put((byte *)seed, strlen(seed)); 
              
                   RSAES_OAEP_SHA_Decryptor priv(randPool, keyLength); 
                   HexEncoder privFile(new FileSink(privFilename)); 
                   priv.DEREncode(privFile); 
                   privFile.MessageEnd(); 
              
                   RSAES_OAEP_SHA_Encryptor pub(priv); 
                   HexEncoder pubFile(new FileSink(pubFilename)); 
                   pub.DEREncode(pubFile); 
                   pubFile.MessageEnd(); 

              
            //------------------------ 
            // RSA加密 
            //------------------------ 
            string RSAEncryptString(const char *pubFilename, const char *seed, const char *message) 

                   FileSource pubFile(pubFilename, true, new HexDecoder); 
                   RSAES_OAEP_SHA_Encryptor pub(pubFile); 
              
                   RandomPool randPool; 
                   randPool.Put((byte *)seed, strlen(seed)); 
              
                   string result; 
                   StringSource(message, true, new PK_EncryptorFilter(randPool, pub, new HexEncoder(new StringSink(result)))); 
                   return result; 

              
            //------------------------ 
            // RSA解密 
            //------------------------ 
            string RSADecryptString(const char *privFilename, const char *ciphertext) 

                   FileSource privFile(privFilename, true, new HexDecoder);

               RSAES_OAEP_SHA_Decryptor priv(privFile); 
              
                   string result; 
                   StringSource(ciphertext, true, new HexDecoder(new PK_DecryptorFilter(GlobalRNG(), priv, new StringSink(result)))); 
                   return result; [Page]

              
            //------------------------ 
            // 定義全局的隨機(jī)數(shù)池 
            //------------------------ 
            RandomPool & GlobalRNG() 

                   static RandomPool randomPool; 
                   return randomPool; 

              
            2.設(shè)置工程屬性 
            選擇工程屬性(Alt + F7): 
            (1)“Configuration Properties”→“C/C++” →“General”,右邊的“Additional Include Directories”設(shè)置為上面建好的Crypto++ SDK的Include文件夾,“C:\\Program Files\\CyptoPP\\include”; 
            (2) “Configuration Properties”→“Linker” →“General”,右邊的“Additional Library Directories”設(shè)置為上面建好的Crypto++ SDK的Lib\\Debug文件夾,“C:\\Program Files\\CyptoPP\\lib\\debug”(Release模式下對應(yīng)著Release文件夾); 
            (3) “Configuration Properties”→“C/C++” →“Code Generation”,右邊的“Runtime Library”設(shè)置為“Multi-threaded Debug (/MTd)”(Release模式下對應(yīng)著“Multi-threaded (/MT)”) 
              
            3.運(yùn)行程序(Ctrl + F5) 
            正常運(yùn)行的輸出結(jié)果為:


            Origin Text:    Hello World!

            Encrypted Text: 79C72A482482EF45111F961772456310792AB735ECF72329ECB26292D2B26374 
            824E0E35D24A63CB03B867DD2C70B001FD4B2B33FBC984BD229A5226F284B889901817976A680322
            9E8351372C5E28E8BEBA2A94E7CF61A8A162F0BA2F3E0C35D26842D92EC4866D25E6BF878743E481
            84D9F6FF9BA690F953568D017C02D540

            Decrypted Text: Hello World! 


            如果上面的第(3)步?jīng)]有設(shè)置則會出現(xiàn)以下鏈接錯誤: 
            cryptlib.lib(randpool.obj) : error LNK2005: \"public: __thiscall std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >(char const *)\" (??0?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@QAE@PBD@Z) already defined in msvcprtd.lib(MSVCP80D.dll) 
            說在msvcprtd.lib和MSVCRTD.lib中已經(jīng)定義過。

            Logo
            作者:Gezidan
            出處:http://www.rixu.net    
            本文版權(quán)歸作者和博客園共有,歡迎轉(zhuǎn)載,但未經(jīng)作者同意必須保留此段聲明,且在文章頁面明顯位置給出原文連接,否則保留追究法律責(zé)任的權(quán)利。
            posted on 2011-08-05 16:24 日需博客 閱讀(5308) 評論(0)  編輯 收藏 引用 所屬分類: C C++技術(shù)文章轉(zhuǎn)載
            国产精品青草久久久久福利99 | 久久久久国产| 国产精品99久久99久久久| 欧美日韩精品久久久免费观看| 色综合久久天天综线观看| 久久艹国产| 久久伊人中文无码| 亚洲国产精品成人久久蜜臀 | 精品一久久香蕉国产线看播放| 成人免费网站久久久| 91亚洲国产成人久久精品网址| 国产国产成人久久精品| 久久99国产精品成人欧美| 久久高清一级毛片| 99久久免费国产精品特黄| 久久99久国产麻精品66| 2021久久国自产拍精品| 久久噜噜电影你懂的| 久久综合狠狠综合久久97色| 99久久免费国产精品特黄| 精品久久久久久亚洲精品 | 久久播电影网| 久久精品国产亚洲AV久| 97精品久久天干天天天按摩| 久久久久这里只有精品| 久久久午夜精品| 国产精品久久一区二区三区 | 亚洲精品99久久久久中文字幕 | 久久久久久久亚洲Av无码| 久久99热精品| 久久亚洲精品国产亚洲老地址 | 婷婷久久综合九色综合绿巨人| 国产成人无码精品久久久性色| 精品久久8x国产免费观看| 欧美激情精品久久久久久久九九九| 久久久久久精品无码人妻| 日韩欧美亚洲综合久久影院d3| 97精品国产97久久久久久免费| 久久久久18| 欧美亚洲国产精品久久蜜芽| 狠狠色噜噜色狠狠狠综合久久|