1. 程式人生 > >crypto7.0移植筆記

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, 

    相關推薦

    crypto7.0移植筆記

    Crypto移植筆記 2018.06.12 許靜   1.     環境: vs2017, crypto7.0  2.     修改程式碼,使編譯通過。測試crylib專案

    [RK3288][Android6.0] 移植筆記 --- 13.3寸eDP顯示屏新增

    Platform: RK3288 OS: Android 6.0 Kernel: 3.10.92 不得不說從 RGB -> LVDS -> MIPI -> eDP 一路過來,現在的

    [RK3288][Android6.0] 移植筆記 --- 韌體無法下載到eMMC除錯

    Platform: ROCKCHIP OS: Android 6.0 Kernel: 3.10.92 現象: 按照參考設計畫的空板子使用AndroidTool下載images提示準備IDB失敗. An

    ECMAScript 6.0 學習筆記

    企業 編寫 lan 語言 http targe 程序 blank es6 ECMAScript 6入門 1、ECMAScript 6.0(也就是ES2015 以下簡稱 ES6)是 JavaScript 語言的下一代標準,已經在2015年6月正式發布了。它的目標,是使得 J

    thphp5.0學習筆記(一)

    mic tel 序號 app clas world char p s 庫類 1.目錄結構: 其中thinkphp子目錄是框架核心目錄 thinkphp結構: 2.入口文件 默認自帶的入口文件位於public/index.php 應用目錄為application,其結構

    thinkphp5.0學習筆記(二)API後臺處理與命名空間

    mac code 輸入 -1 pub 基礎 select() color 第一個 命名空間 先來看命名空間吧; 命名空間是學習TP的基礎, <?php namespace app\lian\c1; class yi{ public $obj = "這是第一個

    thinkphp5.0學習筆記(三)獲取信息,變量,綁定參數

    名稱 自動識別 參數順序 query images 報錯 oca nds arc 1.構造函數: 控制器類必須繼承了\think\Controller類,才能使用: 方法_initialize 代碼: <?php namespace app\lian\control

    磁盤陣列raid0,raid1,raid5,raid0-1,raid1-0學習筆記

    raid0 raid5 raid1 磁盤陣列 磁盤陣列RAID ,REDUNDANTARRAYS OD INDEPENSIVE DISKS ,容錯廉價磁盤陣列,可以通過一些技術將多個較小的磁盤整合為一個較大的磁盤設備,而這個較大的磁盤功能不只是存儲,還具有數據保護的功能。整個RAID的等級不同

    vue2.0學習筆記之webpack-simple模板中的路由簡單配置案例

    nbsp 主頁 default code vue 兩個文件 new ebp 命名 以下是完成後的目錄結構 案例基礎是已經用 webpack+vue-cli 創建了一個名為 vue_router的工程 , 並安裝了vue-rout

    vue2.0學習筆記之自定義組件

    2.0 sco ron 自定義組件 定義 temp use 使用 imp step one: 推薦結構 step two: Loading.vue <template> <h3>{{msg}}<

    Enterprise Library 5.0 學習筆記

    數據 dsm tar ng- one pop releases 緩存 最新版 近期了解了微軟提供的企業開發框架Enterprise Library,眼下最新版本號是6.0,可是不支持FW3.5。所以就學習了5.0的版本號,EL5.0支持FW3.5和4

    .net core 2.0學習筆記(四):遷移.net framework 工程到.net core

    編譯 its evel hashtable ref 學習筆記 inline null 創建 在遷移.net core的過程中,第一步就是要把.net framework 工程的目標框架改為.net core2.0,但是官網卻沒有提供轉換工具,需要我們自己動手完成了

    [dotnetCore2.0]學習筆記之一: ASP.NET Core 升級到2.0

    玩耍 後來 razor ons 引用 net ins install 查找 需要升級: 1、SDK2.0 ,需要單獨安裝;https://www.microsoft.com/net/core#windowscmd   VS2017 不包含這個SDK;而這個SDK包含了run

    一起學ASP.NET Core 2.0學習筆記(一): CentOS下 .net core2 sdk nginx、supervisor、mysql環境搭建

    image dev 預覽 def star fig brush rest aspnet 作為.neter,看到.net core 2.0的正式發布,心裏是有點小激動的,迫不及待的體驗了一把,發現速度確實是快了很多,其中也遇到一些小問題,所以整理了一些學習筆記: 閱讀目

    .net core 2.0學習筆記(六):Remoting核心類庫RealProxy遷移

    ride dispatch 包含 void reflect 既然 splay creat (六) 在學習.net core的過程中,我們已經明確被告知,Remoting將不會被支持。官方的解釋是,.net framework 類型包含了太多的Runtime的內容,是

    一起學ASP.NET Core 2.0學習筆記(二): ef core2.0 及mysql provider 、Fluent API相關配置及遷移

    upd order rac option 包管理 rtl code create .net core 不得不說微軟的技術叠代還是很快的,上了微軟的船就得跟著她走下去,前文一起學ASP.NET Core 2.0學習筆記(一): CentOS下 .net core2 s

    c# 7.0 學習筆記

    ole action zed val struct amp ret null int out 可以寫在裏面了 // int result = 0; 不需要寫在外面了 if (!int.TryParse(input, out int result)) { r

    react-router 4.0 學習筆記

    com alt image div ima cnblogs link 圖片 imp 1、安裝react-router-dom 2、頁面上要使用的時候要引入  import { BrowserRouter as Router, Route, Link } fr

    python基本數據類型(二)-python3.0學習筆記

    tin 基本數據 abcde 返回 屬性方法 mat sizeof 不可變 map python基本數據類型 序列類型的自帶方法 1.列表的常用方法 2.元祖的常用方法 3.字符串的常用方法 1.列表常用的方法 L.append(obj) #在列表末尾添加新的對

    python基本數據類型(一)-python3.0學習筆記

    中括號 str 只有一個 most 浮點型 基本 數字類型 shel convert python基本數據類型 1.python課程簡介 2.數據類型 3.序列類型 1.python簡介 1.python是一門編程語言,是一門完全面向對象的編程語言 2.如果對語言進行分類,