教学服务系统

 找回密码
 立即注册
搜索
查看: 555|回复: 3

信息计算2019级1班22号李浩栋

[复制链接]

8

主题

16

帖子

72

积分

注册会员

Rank: 2

积分
72
发表于 2022-4-22 18:15:29 | 显示全部楼层 |阅读模式

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?立即注册

x
回复

使用道具 举报

8

主题

16

帖子

72

积分

注册会员

Rank: 2

积分
72
 楼主| 发表于 2022-4-29 21:38:29 | 显示全部楼层

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?立即注册

x
回复

使用道具 举报

8

主题

16

帖子

72

积分

注册会员

Rank: 2

积分
72
 楼主| 发表于 2022-5-3 19:21:50 | 显示全部楼层
1、RSA.h
  1. #pragma once
  2. #pragma once
  3. #include <string>
  4. #include <vector>
  5. struct Key
  6. {
  7.         long pkey;
  8.         //私钥(ekey, pkey): (e,n)
  9.         long ekey;
  10.         //公钥(dkey, pkey): (d, n)
  11.         long dkey;
  12. };

  13. class RSA
  14. {
  15. public:
  16.         RSA();

  17.         Key getKey()
  18.         {
  19.                 return _key;
  20.         }

  21.         void ecrept(const char* plain_file_in, const char* ecrept_file_out,
  22.                 long ekey, long pkey);//加密
  23.         void decrept(const char* ecrept_file_in, const char* plain_file_out,
  24.                 long dkey, long pkey);//解密

  25.         std::vector<long> ecrept(std::string& str_in, long ekey, long pkey);//字符串加密
  26.         std::string decrept(std::vector<long>& ecrept_str, long dkey, long pkey);//解密为字符串

  27.         void printInfo(std::vector<long>& ecrept_str);//打印信息
  28. private:
  29.         long ecrept(long msg, long key, long pkey);//加密单个信息
  30.         long produce_prime();//产生素数
  31.         bool is_prime(long prime);//是否为素数
  32.         void produce_keys();
  33.         long produce_pkey(long prime1, long prime2);//计算pq乘积n
  34.         long produce_orla(long prime1, long prime2);//计算欧拉函数φ(n)
  35.         long produce_ekey(long orla);//产生e
  36.         long produce_gcd(long ekey, long orla);//产生最大公约数
  37.         long produce_dkey(long ekey, long orla);//产生d

  38. private:
  39.         Key _key;
  40. };
复制代码
2、RSA.cpp
  1. #include"RSA.h"
  2. #include<time.h>
  3. #include<math.h>
  4. #include<iostream>
  5. #include<fstream>

  6. RSA::RSA()
  7. {
  8.         produce_keys();
  9. }

  10. void RSA::ecrept(const char* plain_file_in, const char* ecrept_file_out,
  11.         long ekey, long pkey)//加密
  12. {
  13.         std::ifstream fin(plain_file_in);
  14.         std::ofstream fout(ecrept_file_out, std::ofstream::app);
  15.         if (!fin.is_open())
  16.         {
  17.                 std::cout << "open file failed" << std::endl;
  18.                 return;
  19.         }
  20.         const int NUM = 256;
  21.         char buffer[NUM];
  22.         long buffer_out[NUM];
  23.         int curNum;
  24.         while (!fin.eof())
  25.         {
  26.                 fin.read(buffer, NUM);
  27.                 curNum = fin.gcount();
  28.                 for (int i = 0; i < curNum; ++i)
  29.                 {
  30.                         buffer_out[i] = ecrept(buffer[i], ekey, pkey);
  31.                 }
  32.                 fout.write((char*)buffer_out, curNum * sizeof(long));
  33.         }
  34.         fin.close();
  35.         fout.close();
  36. }
  37. void RSA::decrept(const char* ecrept_file_in, const char* plain_file_out,
  38.         long dkey, long pkey)//解密
  39. {
  40.         std::ifstream fin(ecrept_file_in);
  41.         std::ofstream fout(plain_file_out, std::ofstream::app);
  42.         if (!fin.is_open())
  43.         {
  44.                 std::cout << "open file failed" << std::endl;
  45.                 return;
  46.         }
  47.         const int NUM = 256;
  48.         long buffer[NUM];
  49.         char buffer_out[NUM];
  50.         int curNum;
  51.         while (!fin.eof())
  52.         {
  53.                 fin.read((char*)buffer, NUM * sizeof(long));
  54.                 curNum = fin.gcount() / sizeof(long);
  55.                 for (int i = 0; i < curNum; ++i)
  56.                 {
  57.                         buffer_out[i] = ecrept(buffer[i], dkey, pkey);
  58.                 }
  59.                 fout.write(buffer_out, curNum);
  60.         }
  61.         fin.close();
  62.         fout.close();
  63. }

  64. std::vector<long> RSA::ecrept(std::string& str_in, long ekey, long pkey)//字符串加密
  65. {
  66.         std::vector<long> vecout;
  67.         size_t sz = str_in.size();
  68.         for (const auto& e : str_in)
  69.         {
  70.                 vecout.push_back(ecrept(e, ekey, pkey));
  71.         }
  72.         return vecout;
  73. }
  74. std::string RSA::decrept(std::vector<long>& ecrept_str, long dkey, long pkey)//解密为字符串
  75. {
  76.         std::string strout;
  77.         for (const auto& e : ecrept_str)
  78.         {
  79.                 strout.push_back(ecrept(e, dkey, pkey));
  80.         }
  81.         return strout;
  82. }

  83. void RSA::printInfo(std::vector<long>& ecrept_str)//打印信息
  84. {
  85.         for (const auto& e : ecrept_str)
  86.         {
  87.                 std::cout << e << ' ';
  88.         }
  89.         std::cout << std::endl;
  90. }

  91. //加密解密算法
  92. //模幂运算(a^b)%c   
  93. long RSA::ecrept(long msg, long key, long pkey)
  94. {
  95.         long msg_out = 1;
  96.         //A0:a^(2^0) % c = a^1 = a%c
  97.         long a = msg;
  98.         long b = key;
  99.         int c = pkey;
  100.         while (b)
  101.         {
  102.                 if (b & 1)
  103.                         //msg_out = (A0 * A1 ...Ai ... An) % c
  104.                         msg_out = (msg_out * a) % c;
  105.                 b >>= 1;
  106.                 //Ai = (A(i - 1) * A(i - 1)) % c
  107.                 a = (a * a) % c;
  108.         }
  109.         return msg_out;
  110. }

  111. long RSA::produce_prime()
  112. {
  113.         srand(time(nullptr));
  114.         long prime = 0;
  115.         while (1)
  116.         {
  117.                 prime = rand() % 50 + 2;
  118.                 if (is_prime(prime))
  119.                         break;
  120.         }
  121.         return prime;
  122. }

  123. bool RSA::is_prime(long prime)
  124. {
  125.         if (prime < 2)
  126.                 return false;
  127.         for (int i = 2; i < sqrt(prime); ++i)
  128.         {
  129.                 if (prime % i == 0)
  130.                         return false;
  131.         }
  132.         return true;
  133. }

  134. void RSA::produce_keys()
  135. {
  136.         long prime1 = produce_prime();
  137.         long prime2 = produce_prime();
  138.         while (prime1 == prime2)
  139.                 prime2 = produce_prime();
  140.         _key.pkey = produce_pkey(prime1, prime2);
  141.         long orla = produce_orla(prime1, prime2);
  142.         _key.ekey = produce_ekey(orla);
  143.         _key.dkey = produce_dkey(_key.ekey, orla);
  144. }

  145. long RSA::produce_pkey(long prime1, long prime2)
  146. {
  147.         return prime1 * prime2;
  148. }

  149. long RSA::produce_orla(long prime1, long prime2)
  150. {
  151.         return (prime1 - 1) * (prime2 - 1);
  152. }

  153. //随机选择一个整数e,条件是1 < e < φ(n),且e与φ(n) 互质
  154. long RSA::produce_ekey(long orla)
  155. {
  156.         long ekey;
  157.         srand(time(nullptr));
  158.         while (1)
  159.         {
  160.                 ekey = rand() % orla;
  161.                 if (ekey > 1 && produce_gcd(ekey, orla) == 1)
  162.                         break;
  163.         }
  164.         return ekey;
  165. }

  166. //求最大公约数
  167. long RSA::produce_gcd(long ekey, long orla)
  168. {
  169.         //gcd(a, b)------gcd(b ,a%b)
  170.         long ret;
  171.         while (ret = ekey % orla)
  172.         {
  173.                 ekey = orla;
  174.                 orla = ret;
  175.         }
  176.         return orla;
  177. }

  178. long RSA::produce_dkey(long ekey, long orla)
  179. {
  180.         //(dkey * ekey) % orla == 1

  181.         long dkey = orla / ekey;//dkey最小为orla / ekey,才能保证dkey * ekey与orla相等
  182.                                                         //只有dkey * ekey比orla大才能有余数为1
  183.         while (1)
  184.         {
  185.                 if ((dkey * ekey) % orla == 1)
  186.                         break;
  187.                 ++dkey;
  188.         }
  189.         return dkey;
  190. }
复制代码
3、Test.cpp
  1. #include"RSA.h"
  2. #include<iostream>

  3. void test()
  4. {
  5.         RSA rsa;
  6.         Key key = rsa.getKey();
  7.         std::string strin;
  8.         while (1)
  9.         {
  10.                 std::cout << "输入要加密的信息:" << std::endl;
  11.                 std::cin >> strin;
  12.                 std::vector<long>strecrept = rsa.ecrept(strin, key.ekey, key.pkey);
  13.                 std::string strout = rsa.decrept(strecrept, key.dkey, key.pkey);
  14.                 std::cout << "加密后的信息:" << std::endl;
  15.                 rsa.printInfo(strecrept);
  16.                 std::cout << "解密后的信息:" << std::endl;
  17.                 std::cout << strout << std::endl;
  18.                 std::cout << std::endl;

  19.         }
  20. }

  21. int main()
  22. {
  23.         test();
  24.         system("pause");
  25.         return 0;
  26. }
复制代码


回复

使用道具 举报

8

主题

16

帖子

72

积分

注册会员

Rank: 2

积分
72
 楼主| 发表于 2022-5-3 19:23:41 | 显示全部楼层

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?立即注册

x
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

教学服务系统

GMT+8, 2025-4-30 14:01 , Processed in 0.021945 second(s), 19 queries .

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表