教学服务系统

 找回密码
 立即注册
搜索
查看: 653|回复: 4

信息计算2019级1班5号唐乐

[复制链接]

9

主题

22

帖子

98

积分

注册会员

Rank: 2

积分
98
发表于 2022-4-19 12:49:32 | 显示全部楼层 |阅读模式
2022年4月19日 mooc截图











本帖子中包含更多资源

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

x
回复

使用道具 举报

9

主题

22

帖子

98

积分

注册会员

Rank: 2

积分
98
 楼主| 发表于 2022-4-24 14:21:00 | 显示全部楼层
2022年4月22日 mooc截图

本帖子中包含更多资源

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

x
回复

使用道具 举报

9

主题

22

帖子

98

积分

注册会员

Rank: 2

积分
98
 楼主| 发表于 2022-4-29 21:36:08 | 显示全部楼层
2022年4月29日 mooc截图

本帖子中包含更多资源

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

x
回复

使用道具 举报

9

主题

22

帖子

98

积分

注册会员

Rank: 2

积分
98
 楼主| 发表于 2022-5-3 10:37:44 | 显示全部楼层
2022年5月3日 mooc截图


本帖子中包含更多资源

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

x
回复

使用道具 举报

9

主题

22

帖子

98

积分

注册会员

Rank: 2

积分
98
 楼主| 发表于 2022-5-3 10:58:31 | 显示全部楼层
RSA加解密----大数加密这里添加了外部依赖库boost库

一、源代码
1、RAS.h(结构体的定义和功能函数的声明)
  1. #pragma once
  2. #include <string>
  3. #include <vector>
  4. #include <boost/multiprecision/cpp_int.hpp>
  5. #include <boost/multiprecision/random.hpp>
  6. #include <boost/multiprecision/miller_rabin.hpp>
  7. namespace bm = boost::multiprecision;

  8. struct Key
  9. {
  10.         bm::int1024_t pkey;
  11.         //公钥(ekey, pkey): (e,n)
  12.         bm::int1024_t ekey;
  13.         //私钥(dkey, pkey): (d, n)
  14.         bm::int1024_t dkey;
  15. };

  16. class RSA
  17. {
  18. public:
  19.         RSA();

  20.         Key getKey()
  21.         {
  22.                 return _key;
  23.         }

  24.         void ecrept(const char* plain_file_in, const char* ecrept_file_out,
  25.                 bm::int1024_t ekey, bm::int1024_t pkey);//加密
  26.         void decrept(const char* ecrept_file_in, const char* plain_file_out,
  27.                 bm::int1024_t dkey, bm::int1024_t pkey);//解密

  28.         std::vector<bm::int1024_t> ecrept(std::string& str_in, bm::int1024_t ekey, bm::int1024_t pkey);//字符串加密
  29.         std::string decrept(std::vector<bm::int1024_t>& ecrept_str, bm::int1024_t dkey, bm::int1024_t pkey);//解密为字符串

  30.         void printInfo(std::vector<bm::int1024_t>& ecrept_str);//打印信息
  31. private:
  32.         bm::int1024_t ecrept(bm::int1024_t msg, bm::int1024_t key, bm::int1024_t pkey);//加密单个信息
  33.         bm::int1024_t produce_prime();//产生素数
  34.         bool is_prime(bm::int1024_t prime);//是否为素数
  35.         bool is_prime_bigInt(bm::int1024_t prime);
  36.         void produce_keys();
  37.         bm::int1024_t produce_pkey(bm::int1024_t prime1, bm::int1024_t prime2);//计算pq乘积n
  38.         bm::int1024_t produce_orla(bm::int1024_t prime1, bm::int1024_t prime2);//计算欧拉函数φ(n)
  39.         bm::int1024_t produce_ekey(bm::int1024_t orla);//产生e
  40.         bm::int1024_t produce_gcd(bm::int1024_t ekey, bm::int1024_t orla);//产生最大公约数
  41.         bm::int1024_t produce_dkey(bm::int1024_t ekey, bm::int1024_t orla);//产生d
  42.         bm::int1024_t exgcd(bm::int1024_t ekey, bm::int1024_t orla,
  43.                 bm::int1024_t& x, bm::int1024_t& y);
  44. private:
  45.         Key _key;
  46. };

  47. //        1. 随机选择两个不相等的质数p和q(实际应用中,这两个质数越大,就越难破解)。
  48. //        2. 计算p和q的乘积n,n = pq。
  49. //        3. 计算n的欧拉函数φ(n)。
  50. //        4. 随机选择一个整数e,条件是1 < e < φ(n),且e与φ(n) 互质。
  51. //        5. 计算e对于φ(n)的模反元素d,使得de≡1 mod φ(n),即:
  52. //                                                                                        (de)modφ(n) = 1
  53. //        6. 产生公钥(e, n),私钥(d, n)。
复制代码
2、RAS.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.         bm::int1024_t ekey, bm::int1024_t pkey)//加密
  12. {
  13.         std::ifstream fin(plain_file_in, std::ifstream::binary);
  14.         std::ofstream fout(ecrept_file_out, std::ofstream::binary);
  15.         if (!fin.is_open())
  16.         {
  17.                 std::cout << "open file failed" << std::endl;
  18.                 return;
  19.         }
  20.         const int NUM = 128;
  21.         char buffer[NUM];
  22.         bm::int1024_t 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(bm::int1024_t));
  33.         }
  34.         fin.close();
  35.         fout.close();
  36. }
  37. void RSA::decrept(const char* ecrept_file_in, const char* plain_file_out,
  38.         bm::int1024_t dkey, bm::int1024_t pkey)//解密
  39. {
  40.         std::ifstream fin(ecrept_file_in, std::ifstream::binary);
  41.         std::ofstream fout(plain_file_out, std::ofstream::binary);
  42.         if (!fin.is_open())
  43.         {
  44.                 std::cout << "open file failed" << std::endl;
  45.                 return;
  46.         }
  47.         const int NUM = 256;
  48.         bm::int1024_t buffer[NUM];
  49.         char buffer_out[NUM];
  50.         int curNum;
  51.         while (!fin.eof())
  52.         {
  53.                 fin.read((char*)buffer, NUM * sizeof(bm::int1024_t));
  54.                 curNum = fin.gcount() / sizeof(bm::int1024_t);
  55.                 for (int i = 0; i < curNum; ++i)
  56.                 {
  57.                         buffer_out[i] = (char)ecrept(buffer[i], dkey, pkey);
  58.                 }
  59.                 fout.write(buffer_out, curNum);
  60.         }
  61.         fin.close();
  62.         fout.close();
  63. }

  64. std::vector<bm::int1024_t> RSA::ecrept(std::string& str_in, bm::int1024_t ekey, bm::int1024_t pkey)//字符串加密
  65. {
  66.         std::vector<bm::int1024_t> 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<bm::int1024_t>& ecrept_str, bm::int1024_t dkey, bm::int1024_t pkey)//解密为字符串
  75. {
  76.         std::string strout;
  77.         for (const auto& e : ecrept_str)
  78.         {
  79.                 strout.push_back((char)ecrept(e, dkey, pkey));
  80.         }
  81.         return strout;
  82. }

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


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

  110. //随机产生一个素数
  111. bm::int1024_t RSA::produce_prime()
  112. {
  113.         //srand(time(nullptr));
  114.         bm::int1024_t prime = 0;

  115.         //mt19937:一种随机数产生器
  116.         boost::random::mt19937 gen(time(nullptr));

  117.         //指定随机数的范围 2 ~ (1<<128)
  118.         boost::random::uniform_int_distribution<bm::int1024_t> dist(2, bm::int1024_t(1) << 128);

  119.         while (1)
  120.         {
  121.                 prime = dist(gen);
  122.                 if (is_prime_bigInt(prime))
  123.                         break;
  124.         }
  125.         return prime;
  126. }

  127. bool RSA::is_prime(bm::int1024_t prime)
  128. {
  129.         if (prime < 2)
  130.                 return false;
  131.         for (bm::int1024_t i = 2; i < sqrt(prime); ++i)
  132.         {
  133.                 if (prime % i == 0)
  134.                         return false;
  135.         }
  136.         return true;
  137. }

  138. bool RSA::is_prime_bigInt(const bm::int1024_t digit)
  139. {
  140.         boost::random::mt11213b gen(time(nullptr));
  141.         if (miller_rabin_test(digit, 25, gen)) //素数测试算法miller_rabin_test()
  142.         {
  143.                 if (miller_rabin_test((digit - 1) / 2, 25, gen))
  144.                 {
  145.                         return true;
  146.                 }
  147.         }
  148.         return false;

  149. }

  150. void RSA::produce_keys()
  151. {
  152.         bm::int1024_t prime1 = produce_prime();
  153.         bm::int1024_t prime2 = produce_prime();
  154.         while (prime1 == prime2)
  155.                 prime2 = produce_prime();
  156.         _key.pkey = produce_pkey(prime1, prime2);
  157.         bm::int1024_t orla = produce_orla(prime1, prime2);
  158.         _key.ekey = produce_ekey(orla);
  159.         _key.dkey = produce_dkey(_key.ekey, orla);
  160. }

  161. bm::int1024_t RSA::produce_pkey(bm::int1024_t prime1, bm::int1024_t prime2)
  162. {
  163.         return prime1 * prime2;
  164. }

  165. bm::int1024_t RSA::produce_orla(bm::int1024_t prime1, bm::int1024_t prime2)
  166. {
  167.         return (prime1 - 1) * (prime2 - 1);
  168. }

  169. //随机选择一个整数e,条件是1 < e < φ(n),且e与φ(n) 互质
  170. bm::int1024_t RSA::produce_ekey(bm::int1024_t orla)
  171. {
  172.         bm::int1024_t ekey;
  173.         srand(time(nullptr));
  174.         while (1)
  175.         {
  176.                 ekey = rand() % orla;
  177.                 if (ekey > 1 && produce_gcd(ekey, orla) == 1)
  178.                         break;
  179.         }
  180.         return ekey;
  181. }

  182. //求最大公约数
  183. bm::int1024_t RSA::produce_gcd(bm::int1024_t ekey, bm::int1024_t orla)
  184. {
  185.         //gcd(a, b)------gcd(b ,a%b)
  186.         bm::int1024_t ret;
  187.         while (ret = ekey % orla)
  188.         {
  189.                 ekey = orla;
  190.                 orla = ret;
  191.         }
  192.         return orla;
  193. }

  194. bm::int1024_t RSA::produce_dkey(bm::int1024_t ekey, bm::int1024_t orla)
  195. {
  196.         bm::int1024_t x, y;
  197.         exgcd(ekey, orla, x, y);
  198.         return (x % orla + orla) % orla;
  199. }



  200. /*
  201. 扩展的欧几里得算法
  202. x = y1; y = x1 - [a/b]*y1
  203. */

  204. bm::int1024_t RSA::exgcd(bm::int1024_t ekey, bm::int1024_t orla,
  205.         bm::int1024_t& x, bm::int1024_t& y)
  206. {
  207.         if (orla == 0)
  208.         {
  209.                 x = 1;
  210.                 y = 0;
  211.                 return ekey;
  212.         }
  213.         bm::int1024_t ret = exgcd(orla, ekey % orla, x, y);
  214.         bm::int1024_t x1 = x, y1 = y;
  215.         x = y1;
  216.         y = x1 - (ekey / orla) * y1;
  217.         return ret;
  218. }
复制代码
3、test.cpp(主函数及测试函数)
  1. #include"RSA.h"
  2. #include<iostream>


  3. void teststring()
  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<bm::int1024_t>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.         teststring();
  24.         system("pause");
  25.         return 0;
  26. }
复制代码


二、运行截图


本帖子中包含更多资源

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

x
回复

使用道具 举报

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

本版积分规则

教学服务系统

GMT+8, 2025-4-30 11:57 , Processed in 0.018616 second(s), 19 queries .

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

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