crypto7.0移植筆記

Crypto移植筆記

2018.06.12 許靜

 

1.     環境: vs2017, crypto7.0 

2.     修改代碼,使編譯通過。測試crylib項目。

無法打開源文件stdio.h, stddef.h,stdlib.h等文件, 在包含目錄中添加:

C:\Program Files(x86)\Windows Kits\10\Include\10.0.16299.0\ucrt

注意10.0.16299.0版本是否存在本機目錄下。

“找不到Windows SDK版本8.1. 請安裝所需的版本的Windows SDK或者在項目屬性頁中或通過右鍵單擊解決方案並選擇“重定解決方案目標”來更改SDK版本”。更改一下,不出現錯誤。

 

3.     添加cryptlib.lib和所有.h文件。

4.     屬性->c/c++->代碼生成->運行庫 多線程調試。

出錯。

#error指令:Please use the /MD switch for _AFXDLL builds.

原因:將MFC設置成在共享dll中使用MFC.

解決方案:屬性->常規->MFC的使用->在靜態庫中使用MFC.

 

5.     源文件

  1. #ifndef __RSA_MAIN_H__  
  2. #define __RSA_MAIN_H__  
  3.   
  4. #include <iostream>  
  5. #include <string>  
  6.   
  7.   
  8.   
  9. #define _GEN_RSA        1  
  10. #if !_GEN_RSA  
  11. #define _SPE_RSA    1  
  12. #endif  
  13.   
  14. class RSA_C {  
  15. public:  
  16.     RSA_C(void);  
  17.     ~RSA_C(void);  
  18.     std::string PubEn(const char* message);  
  19.     std::string PriDe(const char* chiphtertext);  
  20.     void GenerateRSAKey(unsigned int keyLength, const char *privFilename, const char *pubFilename, const char *seed);  
  21.   
  22. private:  
  23.     unsigned int keyLen;  
  24.     char *privFilename;  
  25.     char *pubFilename;  
  26.     char *seed;  
  27.     /* 
  28. #if _SPE_RSA 
  29.     RSA::PrivateKey k1; 
  30.     RSA::PublicKey k2; 
  31.     AutoSeededRandomPool rnd; 
  32. #endif 
  33. */  
  34. };  
  35.   
  36. extern RSA_C g_RSA_App;  
  37.   
  38.   
  39.   
  40.   
  41.   
  42.   
  43.   
  44.   
  45. #endif  
  1. #include "RSA_main.h"  
  2.   
  3. #include "rsa.h"  
  4. #include "randpool.h"  
  5. #include "hex.h"  
  6. #include "files.h"  
  7. #include "cryptlib.h"  
  8. #include "queue.h"  
  9. #include "filters.h"  
  10.   
  11. #include "osrng.h"    
  12.   
  13. #include <stdexcept>  
  14. using std::runtime_error;  
  15.   
  16. using namespace CryptoPP;  
  17. #pragma comment(lib, "cryptlib.lib")  
  18.   
  19.   
  20. RSA_C g_RSA_App;  
  21.   
  22. #if _GEN_RSA  
  23. static std::string RSADecryptString(const char *privFilename, const char *ciphertext);  
  24. static std::string RSAEncryptString(const char *pubFilename, const char *seed, const char *message);  
  25. static RandomPool& GlobalRNG(void);  
  26.   
  27. void Encode(const std::string& filename, const BufferedTransformation& bt);  
  28. #endif  
  29.   
  30. #if _SPE_RSA  
  31. void Encode(const std::string& filename, const BufferedTransformation& bt);  
  32. void Decode(const std::string& filename, BufferedTransformation& bt);  
  33. #endif  
  34.   
  35.   
  36. #if _SPE_RSA  
  37. RSA::PrivateKey k1;  
  38. RSA::PublicKey k2;  
  39. AutoSeededRandomPool rnd;  
  40. #endif  
  41.   
  42. RSA_C::RSA_C(void)  
  43. {  
  44.     privFilename = new char[1024];  
  45.     pubFilename = new char[1024];  
  46.     seed = new char[1024];  
  47.   
  48. }  
  49.   
  50. RSA_C::~RSA_C()  
  51. {  
  52.     delete privFilename;  
  53.     delete pubFilename;  
  54.     delete seed;  
  55. }  
  56.   
  57. std::string RSA_C::PubEn(const char* message)  
  58. {  
  59.     std::string enText = "";  
  60.   
  61. #if _GEN_RSA  
  62.     std::cout << "origin text:" << message << '\n';  
  63.     enText = RSAEncryptString(pubFilename, seed, message);  
  64.     std::cout << "Encrypted Text:" << enText << '\n';  
  65. #endif  
  66.   
  67. #if _SPE_RSA  
  68.     RSAES_OAEP_SHA_Encryptor e(k2);  
  69.     StringSource(message, truenew PK_EncryptorFilter(rnd,e, new StringSink(enText)));  
  70.   
  71. #endif  
  72.   
  73.     return enText;  
  74. }  
  75.   
  76.   
  77. std::string RSA_C::PriDe(const char* chiphtertext)  
  78. {  
  79.     std::string deText = "";  
  80. #if _GEN_RSA  
  81.     std::cout << "Encryped text:" << chiphtertext << '\n';  
  82.     deText = RSADecryptString(privFilename, chiphtertext);  
  83.     std::cout << "Decrypted Text:" << deText << '\n';  
  84. #endif  
  85.   
  86. #if _SPE_RSA  
  87.     RSAES_OAEP_SHA_Decryptor d(k1);  
  88.     StringSource(chiphtertext, truenew PK_DecryptorFilter(rnd, d, new StringSink(deText)));  
  89.   
  90.   
  91. #endif  
  92.   
  93.     return deText;  
  94. }  
  95.   
  96.   
  97. #if _GEN_RSA  
  98. static RandomPool& GlobalRNG(void)  
  99. {  
  100.     static RandomPool randomPool;  
  101.   
  102.     return randomPool;  
  103. }  
  104. #endif  
  105.   
  106.   
  107. #if _SPE_RSA  
  108. void EncodePrivateKey(const std::string& filename, const RSA::PrivateKey& key)  
  109. {  
  110.     // http://www.cryptopp.com/docs/ref/class_byte_queue.html    
  111.     ByteQueue queue;  
  112.     key.DEREncodePrivateKey(queue);  
  113.   
  114.     Encode(filename, queue);  
  115. }  
  116.   
  117. void EncodePublicKey(const std::string& filename, const RSA::PublicKey& key)  
  118. {  
  119.     // http://www.cryptopp.com/docs/ref/class_byte_queue.html    
  120.     ByteQueue queue;  
  121.     key.DEREncodePublicKey(queue);  
  122.   
  123.     Encode(filename, queue);  
  124. }  
  125.   
  126. void Encode(const std::string& filename, const BufferedTransformation& bt)  
  127. {  
  128.     // http://www.cryptopp.com/docs/ref/class_file_sink.html    
  129.     FileSink file(filename.c_str());  
  130.   
  131.     bt.CopyTo(file);  
  132.     file.MessageEnd();  
  133. }  
  134.   
  135. void DecodePrivatekey(const std::string& filename, RSA::PrivateKey& key)  
  136. {  
  137.     ByteQueue queue;  
  138.   
  139.     Decode(filename, queue);  
  140.     key.BERDecodePrivateKey(queue, false, queue.MaxRetrievable());  
  141.   
  142. }  
  143.   
  144. void DecodePublickey(const std::string& filename, RSA::PublicKey& key)  
  145. {  
  146.     ByteQueue queue;  
  147.   
  148.     Decode(filename, queue);  
  149.     key.BERDecodePublicKey(queue, false, queue.MaxRetrievable());  
  150.   
  151. }  
  152.   
  153.   
  154. void Decode(const std::string& filename, BufferedTransformation& bt)  
  155. {  
  156.     FileSource file(filename.c_str(), true);  
  157.   
  158.     file.TransferTo(bt);  
  159.     bt.MessageEnd();  
  160. }  
  161.   
  162. #endif  
  163.   
  164. void RSA_C::GenerateRSAKey(unsigned int ikeyLength, const char *iprivFilename, const char *ipubFilename, const char *iseed)  
  165. {  
  166. #if _GEN_RSA  
  167.     keyLen = ikeyLength;  
  168.     strcpy(privFilename, iprivFilename);  
  169.     strcpy(pubFilename, ipubFilename);  
  170.     strcpy(seed, iseed);  
  171.   
  172.     // DEREncode() changed to Save() at Issue 569.  
  173.     RandomPool randPool;  
  174.     randPool.IncorporateEntropy((byte *)iseed, strlen(seed));  
  175.   
  176.     RSAES_OAEP_SHA_Decryptor priv(randPool, ikeyLength);  
  177.     HexEncoder privFile(new FileSink(iprivFilename));  
  178.     priv.AccessMaterial().Save(privFile);  
  179.     privFile.MessageEnd();  
  180.   
  181.     RSAES_OAEP_SHA_Encryptor pub(priv);  
  182.     HexEncoder pubFile(new FileSink(ipubFilename));  
  183.     pub.AccessMaterial().Save(pubFile);  
  184.     pubFile.MessageEnd();  
  185. #endif  
  186.   
  187. #if _SPE_RSA  
  188.     AutoSeededRandomPool rnd;  
  189.   
  190.     RSA::PrivateKey rsaPrivate;  
  191.     rsaPrivate.GenerateRandomWithKeySize(rnd, 2048);  
  192.   
  193.     RSA::PublicKey rsaPublic(rsaPrivate);  
  194.     // generate  
  195.     EncodePrivateKey("C:\\rsa05\\rsa-private.key", rsaPrivate);  //將密鑰放在C:\\rsa05目錄下    
  196.     EncodePublicKey("C:\\rsa05\\rsa-public.key", rsaPublic);     //必須預先在C盤建立rsa05文件夾,否則出錯   
  197.     std::cout << "Successfully generated and saved RSA keys" << '\n';  
  198.     //load  
  199.     DecodePrivatekey("C:\\rsa05\\rsa-private.key", k1);  
  200.     DecodePublickey("C:\\rsa05\\rsa-public.key", k2);  
  201.   
  202.     //check  
  203.     if (!k1.Validate(rnd, 3))  
  204.         throw runtime_error("Rsa private key validation failed");  
  205.     if (!k2.Validate(rnd, 3))  
  206.         throw runtime_error("Rsa public key validation failed");  
  207.     /* 
  208.     if (rsaPrivate.GetModulus() != k1.GetModulus() || rsaPrivate.GetPublicExponent() != k1.GetPublicExponent() || rsaPrivate.GetPrivateExponent() != k1.GetPrivateExponent) 
  209.     { 
  210.         throw runtime_error("private key data did not round trip"); 
  211.     } 
  212.     if (rsaPublic.GetModulus() != k2.GetModulus() || rsaPublic.GetPublicExponent != k2.GetPublicExponent()) 
  213.     { 
  214.         throw runtime_error("public key data did not round trip"); 
  215.     } 
  216.         //successfully! 
  217. */  
  218.   
  219. #endif    
  220.   
  221. }  
  222.   
  223. #if _GEN_RSA  
  224. static std::string RSAEncryptString(const char *pubFilename, const char *seed, const char *message)  
  225. {  
  226.     FileSource pubFile(pubFilename, truenew HexDecoder);  
  227.     RSAES_OAEP_SHA_Encryptor pub(pubFile);  
  228.   
  229.     RandomPool randPool;  
  230.     randPool.IncorporateEntropy((byte *)seed, strlen(seed));  
  231.   
  232.     std::string result;  
  233.     StringSource(message, truenew PK_EncryptorFilter(randPool, pub, new HexEncoder(new StringSink(result))));  
  234.     return result;  
  235. }  
  236. #endif  
  237.   
  238. #if _GEN_RSA  
  239. static std::string RSADecryptString(const char *privFilename, const char *ciphertext)  
  240. {  
  241.     FileSource privFile(privFilename, truenew HexDecoder);  
  242.     RSAES_OAEP_SHA_Decryptor priv(privFile);  
  243.   
  244.     std::string result;  
  245.     StringSource(ciphertext, truenew HexDecoder(new PK_DecryptorFilter(GlobalRNG(), priv, new StringSink(result))));  
  246.     return result;  
  247. }  
  248. #endif  

外加一個lib庫。

/**********************************************************************************************************************/ 

生成cryptest工程

官方文檔

 

https://www.cryptopp.com/wiki/Keys_and_Formats#Generating.2C_Validating.2C_Saving.2C_and_Loading_Keys

 

在工程目錄下添加CryptoPP文件夾,將編譯好的lib,以及頭文件放入(頭文件放在include文件夾下) 

流程

 

 

同時,也可以根據項目不同,生成不同的key。將項目名稱作爲seed替換。


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章