教学服务系统

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

信息计算2019级1班13号郝怀宇

[复制链接]

9

主题

19

帖子

117

积分

注册会员

Rank: 2

积分
117
发表于 2022-4-22 15:49:53 | 显示全部楼层 |阅读模式
慕课截图

本帖子中包含更多资源

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

x
回复

使用道具 举报

9

主题

19

帖子

117

积分

注册会员

Rank: 2

积分
117
 楼主| 发表于 2022-4-29 22:42:38 | 显示全部楼层
慕课截图

本帖子中包含更多资源

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

x
回复

使用道具 举报

9

主题

19

帖子

117

积分

注册会员

Rank: 2

积分
117
 楼主| 发表于 2022-5-4 18:38:05 | 显示全部楼层
慕课截图

本帖子中包含更多资源

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

x
回复

使用道具 举报

9

主题

19

帖子

117

积分

注册会员

Rank: 2

积分
117
 楼主| 发表于 2022-5-4 18:40:38 | 显示全部楼层
本帖最后由 郝怀宇 于 2022-5-4 18:47 编辑
  1. #include<stdio.h>
  2. #include<stdlib.h>

  3. int long_n(int n);
  4. int shuru(char* arr, int k, char* wei, int is_first);
  5. void jiami(char* arr, int k, int e, int n);

  6. int shuru(char* arr, int k, char* wei, int is_first)
  7. {
  8.         int i;
  9.         char ch;
  10.      
  11.         if (is_first == 1)
  12.                 ch = getchar();
  13.         else
  14.                 ch = *wei;
  15.         for (i = 0; (i < k) && (ch != '\n'); i++)
  16.         {
  17.                 arr[i] = ch;
  18.                 ch = getchar();
  19.         }
  20.         *wei = ch;
  21.         for (i = i; i < k; i++)
  22.                 arr[i] = 'a';  
  23.         if (ch == '\n')  
  24.                 return 0;
  25.         else
  26.                 return 1;
  27. }


  28. void jiami(char* arr, int k, int e, int n)
  29. {
  30.         int m = 0, c = 1, i, j, t = 0, shu, temp, num = 0;
  31.         int* array;
  32.       
  33.         for (i = 0; i < k; i++)
  34.         {
  35.                 temp = 1;
  36.                 for (j = 0; j < (k - i - 1) * 2; j++)
  37.                         temp = temp * 10;
  38.                 shu = (int)arr[i] - 97;
  39.                 m = m + temp * shu;
  40.         }
  41.         temp = e;
  42.      
  43.         do {
  44.                 temp = temp / 2;
  45.                 num++;
  46.         } while (temp != 0);
  47.         array = (int*)malloc(sizeof(int) * k);
  48.         temp = e;
  49.       
  50.         for (i = 0; i < num; i++)
  51.         {
  52.                 array[i] = temp % 2;
  53.                 temp = temp / 2;
  54.         }
  55.       
  56.         for (i = num - 1; i >= 0; i--)
  57.         {
  58.                 t = t * 2;
  59.                 temp = c * c;
  60.                 if (temp > n)
  61.                 {
  62.                         for (j = 0; temp - n * j >= 0; j++);
  63.                         j--;
  64.                         c = temp - n * j;
  65.                 }
  66.                 else
  67.                         c = temp;
  68.                 if (array[i] == 1)
  69.                 {
  70.                         t = t + 1;
  71.                         temp = c * m;
  72.                         if (temp > n)
  73.                         {
  74.                                 for (j = 0; temp - n * j >= 0; j++);
  75.                                 j--;
  76.                                 c = temp - n * j;
  77.                         }
  78.                         else
  79.                                 c = temp;
  80.                 }

  81.                 e = e / 2;
  82.         }
  83.         temp = c;
  84.         i = 0;
  85.       
  86.         do {
  87.                 temp = temp / 10;
  88.                 i++;
  89.         } while (temp != 0);
  90.         for (i; i < num; i++)
  91.                 printf("0");
  92.         printf("%d", c);
  93. }

  94. int long_n(int n)
  95. {
  96.         int temp, i, j, k, shi, comp = 0;
  97.         temp = n;

  98.         for (i = 1; temp / 10 != 0; i++)
  99.         {
  100.                 temp = temp / 10;
  101.         }
  102.         temp = i;
  103.    
  104.         if (i % 2 != 0)
  105.         {
  106.                 i = i - 1;
  107.                 return i;
  108.         }
  109.    
  110.         else
  111.         {
  112.                 for (j = 0; j < i / 2; j++)
  113.                 {
  114.                         shi = 1;
  115.                         for (k = 0; k < temp - 2; k++)
  116.                                 shi = shi * 10;
  117.                         comp = comp + shi * 25;
  118.                         temp = temp - 2;
  119.                 }
  120.                 if (comp <= n)
  121.                         return i;
  122.                 else
  123.                 {
  124.                         i = i - 2;
  125.                         return i;
  126.                 }
  127.         }
  128. }


  129. int main()
  130. {
  131.         int p, q, e, d, n, fai_n, k, i, is_first = 1;
  132.         char ch, * arr, wei = 'a';
  133.         printf("请输入p、q、e值,用空格间隔开\n");
  134.         scanf_s("%d%d%d", &p, &q, &e);
  135.         n = p * q;
  136.         fai_n = (p - 1) * (q - 1);   //Φ(n)
  137.         for (k = 0; (k * n + 1) % e != 0; k++);
  138.         if ((k * n + 1) % e == 0)
  139.                 d = (k * n + 1) / e;  //d * e ≡ 1 (mod Φ(n))
  140.         k = long_n(n);
  141.         k = k / 2;  
  142.         ch = getchar();
  143.         arr = (char*)malloc(sizeof(char) * k);  
  144.         printf("请输入明文\n");
  145.         while (1)
  146.         {
  147.                 i = shuru(arr, k, &wei, is_first);  
  148.                 is_first = 0;  
  149.                 jiami(arr, k, e, n);  
  150.                 if (i == 0)  
  151.                         break;
  152.         }
  153.         printf("\n");
  154.         return 0;
  155. }
复制代码

本帖子中包含更多资源

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

x
回复

使用道具 举报

9

主题

19

帖子

117

积分

注册会员

Rank: 2

积分
117
 楼主| 发表于 2022-5-30 23:55:03 | 显示全部楼层
大数改进
  1. import java.math.BigInteger;
  2. import java.util.Random;
  3. import java.util.Scanner;

  4. public class RSA大数版本 {

  5.         public static BigInteger Mod_ni(BigInteger a, BigInteger b) {
  6.                 BigInteger zero = new BigInteger("0");
  7.                 a = a.subtract(b.multiply(a.divide(b))); // a = a - b * (a / b)
  8.                 int i = 0;
  9.                 BigInteger bb = b;
  10.                 BigInteger s[] = new BigInteger[1024];
  11.                 while (a.compareTo(zero) != 0) { // a != 0
  12.                         s[i] = b.divide(a); // s[i] = b / a
  13.                         BigInteger t = b;
  14.                         b = a;
  15.                         a = t.mod(a); // t % a
  16.                         i++;
  17.                 }
  18.                 BigInteger y[] = new BigInteger[i];
  19.                 y[0] = new BigInteger("1");
  20.                 y[1] = s[0];
  21.                 for (int j = 2; j < i; j++)
  22.                         y[j] = (y[j - 1].multiply(s[j - 1])).add(y[j - 2]); // y[j] = y[j - 1] * s[j - 1] + y[j - 2]
  23.                 if ((i - 1) % 2 == 1)
  24.                         y[i - 1] = bb.subtract(y[i - 1]); // y[i - 1] = bb - y[i - 1]
  25.                 return y[i - 1];
  26.         }

  27.         public static void main(String args[]) {
  28.                 BigInteger one = new BigInteger("1");
  29.                 Random rnd = new Random();
  30.                 BigInteger p = new BigInteger("1");
  31.                 BigInteger q = new BigInteger("1");
  32.                 p = BigInteger.probablePrime(1024, rnd);
  33.                 q = BigInteger.probablePrime(1024, rnd);
  34.                 BigInteger n = p.multiply(q); // n = p * q
  35.                 BigInteger f = (p.subtract(one).multiply(q.subtract(one))); // f = (p - 1) * (q - 1)
  36.                 BigInteger e = new BigInteger("65537");
  37.                 while (true) {
  38.                         e = BigInteger.probablePrime(1024, rnd);
  39.                         if (e.compareTo(f) <= 1)
  40.                                 break;
  41.                 }
  42.                 BigInteger d = Mod_ni(e, f);
  43.                 System.out.println("n = " + n);
  44.                 System.out.println("公钥:" + e);
  45.                 System.out.println("私钥:" + d);

  46.                 BigInteger xx, yy;
  47.                 while (true) {
  48.                         System.out.println("请选择功能: \n0.退出 \n1.加密\n2.解密");
  49.                         Scanner scan1 = new Scanner(System.in);
  50.                         int ch = scan1.nextInt();
  51.                         if (ch == 1) {
  52.                                 System.out.println("请输入明文:");
  53.                                 String mingwen = new String();
  54.                                 Scanner scan2 = new Scanner(System.in);
  55.                                 mingwen = scan2.nextLine();
  56.                                 byte x[] = new byte[mingwen.length()];
  57.                                 for (int i = 0; i < mingwen.length(); i++)
  58.                                         x[i] = Byte.valueOf((byte) mingwen.charAt(i));                        
  59.                                 xx = new BigInteger(x);  // 字节数组转大整型
  60.                                 xx = xx.modPow(e, n);  // xx^e mod n
  61.                                 System.out.println("密文为:" + xx);
  62.                         } else if (ch == 2) {
  63.                                 System.out.println("请输入密文:");
  64.                                 String miwen = new String();
  65.                                 Scanner scan3 = new Scanner(System.in);
  66.                                 miwen = scan3.nextLine();
  67.                                 yy = new BigInteger(miwen);
  68.                                 yy = yy.modPow(d, n);   // yy^d mod n
  69.                                 byte[] y = yy.toByteArray(); // 大整型转字节数组
  70.                                 System.out.print("明文为:");
  71.                                 for (int i = 0; i < y.length; ++i)
  72.                                         System.out.print((char) y[i]);
  73.                                 System.out.print('\n');
  74.                         } else
  75.                                 break;
  76.                 }

  77.         }
  78. }
复制代码


回复

使用道具 举报

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

本版积分规则

教学服务系统

GMT+8, 2025-4-30 07:26 , Processed in 0.017118 second(s), 18 queries .

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

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