教学服务系统

 找回密码
 立即注册
搜索
查看: 754|回复: 6

信息计算2019级1班10号张夏楠

[复制链接]

8

主题

20

帖子

98

积分

注册会员

Rank: 2

积分
98
发表于 2022-4-22 15:29:00 | 显示全部楼层 |阅读模式
本帖最后由 张夏楠 于 2022-4-22 15:34 编辑




本帖子中包含更多资源

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

x
回复

使用道具 举报

8

主题

20

帖子

98

积分

注册会员

Rank: 2

积分
98
 楼主| 发表于 2022-4-22 15:32:54 | 显示全部楼层

本帖子中包含更多资源

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

x
回复

使用道具 举报

8

主题

20

帖子

98

积分

注册会员

Rank: 2

积分
98
 楼主| 发表于 2022-4-29 19:45:53 | 显示全部楼层

本帖子中包含更多资源

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

x
回复

使用道具 举报

8

主题

20

帖子

98

积分

注册会员

Rank: 2

积分
98
 楼主| 发表于 2022-5-3 15:24:17 | 显示全部楼层
本帖最后由 张夏楠 于 2022-5-3 15:32 编辑

RSA.java:
  1. package club;
  2. import java.math.BigInteger;
  3. import java.util.ArrayList;
  4. import java.util.Scanner;

  5. public class RSA {
  6.         static BigInteger x, y;

  7.         public static boolean fun(BigInteger x, BigInteger y) {
  8.                 BigInteger t;
  9.                 while (!(y.equals(new BigInteger("0")))) {
  10.                         t = x;
  11.                         x = y;
  12.                         y = t.mod(y);
  13.                 }
  14.                 if (x.equals(new BigInteger("1")))
  15.                         return false;
  16.                 else
  17.                         return true;
  18.         }

  19.         public static BigInteger siyue(BigInteger a, BigInteger b) {
  20.                 BigInteger temp;
  21.                 if (b.equals(new BigInteger("0"))) {
  22.                         x = new BigInteger("1");
  23.                         y = new BigInteger("0");
  24.                         return x;
  25.                 }
  26.                 siyue(b, a.mod(b));
  27.                 temp = x;
  28.                 x = y;
  29.                 y = temp.subtract((a.divide(b).multiply(y)));
  30.                 return x;
  31.         }

  32.         public static double entropy(String mess) {
  33.                 ArrayList<Node> jieguo = new ArrayList<Node>();
  34.                 jieguo.clear();
  35.                 double num = mess.length();
  36.                 for (int i = 0; i < num; i++) {
  37.                         boolean flag_exit = true;
  38.                         for (int j = 0; j < jieguo.size(); j++) {
  39.                                 if (jieguo.get(j).getalpha() == mess.charAt(i)) {
  40.                                         flag_exit = false;
  41.                                         jieguo.get(j).setp(jieguo.get(j).getp() + 1 / num);
  42.                                 }
  43.                         }
  44.                         if (flag_exit)
  45.                                 jieguo.add(new Node(1 / num, mess.charAt(i)));
  46.                 }
  47.                 /** 计算熵 */
  48.                 double entropy = 0;
  49.                 for (int i = 0; i < jieguo.size(); i++) {
  50.                         double p1 = jieguo.get(i).getp();
  51.                         entropy += (-p1 * (Math.log(p1) / Math.log(2)));
  52.                 }
  53.                 return entropy;
  54.         }

  55.         public static void main(String[] args) {
  56.                 BigInteger p = null;
  57.                 BigInteger e, d, n, t, c;
  58.                 BigInteger q = null;
  59.                 for (int i = 0; i < 2; i++) {
  60.                         int numDigits;
  61.                         Prime pri = new Prime();
  62.                         try {
  63.                                 numDigits = Integer.parseInt("15");
  64.                         } catch (Exception e1) {
  65.                                 numDigits = 128;
  66.                         }
  67.                         BigInteger start = pri.bigRandom(numDigits);
  68.                         BigInteger big = pri.nextPrime(start);
  69.                         if (i == 0)
  70.                                 p = big;
  71.                         else
  72.                                 q = big;
  73.                 }
  74.                 Scanner input = new Scanner(System.in);
  75.                 n = p.multiply(q);
  76.                 t = p.subtract(new BigInteger("1")).multiply(
  77.                                 q.subtract(new BigInteger("1")));
  78.                 int numDigits;
  79.                 Prime pri = new Prime();
  80.                 try {
  81.                         numDigits = Integer.parseInt("15");
  82.                 } catch (Exception e1) {
  83.                         numDigits = 128;
  84.                 }
  85.                 BigInteger start = pri.bigRandom(numDigits);
  86.                 BigInteger big = pri.nextPrime(start);
  87.                 e = big;
  88.                 while (e.compareTo(t) == 1 || fun(e, t)) {
  89.                         System.out.println("e不合要求,请重新产生" + (e.compareTo(t) == 1)
  90.                                         + fun(e, t));
  91.                         pri = new Prime();
  92.                         try {
  93.                                 numDigits = Integer.parseInt("15");
  94.                         } catch (Exception e1) {
  95.                                 numDigits = 128;
  96.                         }
  97.                         start = pri.bigRandom(numDigits);
  98.                         big = pri.nextPrime(start);
  99.                         e = big;
  100.                 }
  101.                 // 求出私钥d
  102.                 d = siyue(e, t);
  103.                 System.out.println("由计算机随机产生2个素数p,q,分别如下:");
  104.                 System.out.println(p);
  105.                 System.out.println(q);
  106.                 System.out.println("计算得到的n:");
  107.                 System.out.println(n);
  108.                 System.out.println("计算得到的t:");
  109.                 System.out.println(t);
  110.                 System.out.println("随机产生的公钥:");
  111.                 System.out.println(e);
  112.                 System.out.println("由扩展的欧几里德算法求得私钥:");
  113.                 System.out.println(d);
  114.                 while (true) {
  115.                         System.out.println("请输入明文:");
  116.                         String mess = input.nextLine();
  117.                         //
  118.                         BigInteger m = new BigInteger(mess.getBytes());// 把字串转成一个BigInteger对象
  119.                         System.out.println("明文的大整数表示形式:" + m);
  120.                         /** 调用函数计算信息,统计信息 */
  121.                         mess = m.toString();
  122.                         System.out.println("计算得明文熵:" + entropy(mess));
  123.                         // 修改第一位,+1
  124.                         c = m.modPow(e, n);// JAVA中的方法
  125.                         System.out.println("密文的大整数表示形式:" + c);
  126.                         System.out.println("计算得密文熵:" + entropy(c.toString()));
  127.                         m = c.modPow(d, n);
  128.                         System.out.println("解密得到明文的大整数表示形式:" + c);
  129.                         String str = new String(m.toByteArray());// 把返回的结果还原成一个字串
  130.                         System.out.println("解密得到明文为:" + str);
  131.                 }
  132.         }
  133.        
  134. }
复制代码


Node.java:
  1. package club;

  2. public class Node {
  3.         private double p;// 记录概率
  4.         private char alpha;// 记录对应的字母

  5.         public Node(double p, char alpha) {
  6.                 this.p = p;
  7.                 this.alpha = alpha;
  8.         }

  9.         public void setp(double p) {
  10.                 this.p = p;
  11.         }

  12.         public void setalpha(char a) {
  13.                 this.alpha = a;
  14.         }

  15.         public double getp() {
  16.                 return p;
  17.         }

  18.         public char getalpha() {
  19.                 return alpha;
  20.         }
  21. }
复制代码


Prime.java:
  1. package club;
  2. import java.math.BigInteger;

  3. public class Prime {
  4.         public boolean sushu(double n) {
  5.                 // 判断素数
  6.                 boolean isPrime = true;
  7.                 // 如果n大于2 继续判断 否则 isPrime的值不变 2素数
  8.                 if (n > 2) {
  9.                         // 如果n是大于2的偶数 认定不是素数 修改变量值为false
  10.                         if (n % 2 == 0) {
  11.                                 isPrime = false;
  12.                         } else {
  13.                                 // 循环判断如果找到一个可以整除的数 则判定不是素数跳出循环 因为是判断奇数 因此 2 4 6 ...?
  14.                                 // 不用考虑 循环递增2 即?3 5 7 ...
  15.                                 for (int i = 3; i <= (int) Math.sqrt(n); i += 2) {
  16.                                         if (n % i == 0) {
  17.                                                 isPrime = false;
  18.                                                 break;
  19.                                         }
  20.                                 }
  21.                         }

  22.                 }
  23.                 return isPrime;
  24.         }

  25.         private static final BigInteger ZERO = BigInteger.ZERO;
  26.         private static final BigInteger ONE = BigInteger.ONE;
  27.         private static final BigInteger TWO = new BigInteger("2");
  28.         private static final int ERR_VAL = 100;

  29.         public BigInteger nextPrime(BigInteger start) {
  30.                 // 产生一个比给定大整数 start 大的素数,错误率低于 1/2 的 ERR_VAL 次方
  31.                 if (isEven(start))
  32.                         start = start.add(ONE);
  33.                 else
  34.                         start = start.add(TWO);
  35.                 if (start.isProbablePrime(ERR_VAL))
  36.                         return (start);
  37.                 else
  38.                         // 采用递归方式(递归的层数会是个天文数字吗?)
  39.                         return (nextPrime(start));
  40.         }

  41.         private static boolean isEven(BigInteger n) {
  42.                 // 测试一个大整数是否为偶数
  43.                 return (n.mod(TWO).equals(ZERO));
  44.         }

  45.         public BigInteger bigRandom(int numDigits) {
  46.                 // 产生一个随机大整数,各位上的数字都是随机产生的,首位不为 0
  47.                 StringBuffer s = new StringBuffer("");
  48.                 for (int i = 0; i < numDigits; i++)
  49.                         if (i == 0)
  50.                                 s.append(randomDigit(false));
  51.                         else
  52.                                 s.append(randomDigit(true));
  53.                 return (new BigInteger(s.toString()));
  54.         }

  55.         private StringBuffer randomDigit(boolean isZeroOK) {
  56.                 // 产生一个随机的数字(字符串形式的),isZeroOK 决定这个数字是否可以为 0
  57.                 int index;
  58.                 if (isZeroOK)
  59.                         index = (int) Math.floor(Math.random() * 10);
  60.                 else
  61.                         index = 1 + (int) Math.floor(Math.random() * 9);
  62.                 return (digits[index]);
  63.         }

  64.         private static StringBuffer[] digits = { new StringBuffer("0"),
  65.                         new StringBuffer("1"), new StringBuffer("2"),
  66.                         new StringBuffer("3"), new StringBuffer("4"),
  67.                         new StringBuffer("5"), new StringBuffer("6"),
  68.                         new StringBuffer("7"), new StringBuffer("8"), new StringBuffer("9") };

  69.         public static void main(String[] args) {

  70.         }
  71. }
复制代码


运行截图:

本帖子中包含更多资源

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

x
回复

使用道具 举报

8

主题

20

帖子

98

积分

注册会员

Rank: 2

积分
98
 楼主| 发表于 2022-5-9 16:26:12 | 显示全部楼层

本帖子中包含更多资源

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

x
回复

使用道具 举报

8

主题

20

帖子

98

积分

注册会员

Rank: 2

积分
98
 楼主| 发表于 2022-5-9 16:27:17 | 显示全部楼层

本帖子中包含更多资源

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

x
回复

使用道具 举报

8

主题

20

帖子

98

积分

注册会员

Rank: 2

积分
98
 楼主| 发表于 2022-5-13 15:23:57 | 显示全部楼层
  1. #include <iostream>
  2. #include <cmath>
  3. #include <cstring>
  4. #include <ctime>
  5. #include <cstdlib>
  6. using namespace std;


  7. int Plaintext[100];//明文
  8. long long Ciphertext[100];//密文
  9. int n, e = 0, d;

  10. //二进制转换
  11. int BianaryTransform(int num, int bin_num[])
  12. {

  13.     int i = 0, mod = 0;

  14.     //转换为二进制,逆向暂存temp[]数组中
  15.     while (num != 0)
  16.     {
  17.         mod = num % 2;
  18.         bin_num[i] = mod;
  19.         num = num / 2;
  20.         i++;
  21.     }

  22.     //返回二进制数的位数
  23.     return i;
  24. }

  25. //反复平方求幂
  26. long long Modular_Exonentiation(long long a, int b, int n)
  27. {
  28.     int c = 0, bin_num[1000];
  29.     long long d = 1;
  30.     int k = BianaryTransform(b, bin_num) - 1;

  31.     for (int i = k; i >= 0; i--)
  32.     {
  33.         c = 2 * c;
  34.         d = (d * d) % n;
  35.         if (bin_num[i] == 1)
  36.         {
  37.             c = c + 1;
  38.             d = (d * a) % n;
  39.         }
  40.     }
  41.     return d;
  42. }

  43. //生成1000以内素数
  44. int ProducePrimeNumber(int prime[])
  45. {
  46.     int c = 0, vis[1001];
  47.     memset(vis, 0, sizeof(vis));
  48.     for (int i = 2; i <= 1000; i++)if (!vis[i])
  49.     {
  50.         prime[c++] = i;
  51.         for (int j = i * i; j <= 1000; j += i)
  52.             vis[j] = 1;
  53.     }

  54.     return c;
  55. }


  56. //欧几里得扩展算法
  57. int Exgcd(int m, int n, int& x)
  58. {
  59.     int x1, y1, x0, y0, y;
  60.     x0 = 1; y0 = 0;
  61.     x1 = 0; y1 = 1;
  62.     x = 0; y = 1;
  63.     int r = m % n;
  64.     int q = (m - r) / n;
  65.     while (r)
  66.     {
  67.         x = x0 - q * x1; y = y0 - q * y1;
  68.         x0 = x1; y0 = y1;
  69.         x1 = x; y1 = y;
  70.         m = n; n = r; r = m % n;
  71.         q = (m - r) / n;
  72.     }
  73.     return n;
  74. }

  75. //RSA初始化
  76. void RSA_Initialize()
  77. {
  78.     //取出1000内素数保存在prime[]数组中
  79.     int prime[5000];
  80.     int count_Prime = ProducePrimeNumber(prime);

  81.     //随机取两个素数p,q
  82.     srand((unsigned)time(NULL));
  83.     int ranNum1 = rand() % count_Prime;
  84.     int ranNum2 = rand() % count_Prime;
  85.     int p = prime[ranNum1], q = prime[ranNum2];

  86.     n = p * q;

  87.     int On = (p - 1) * (q - 1);


  88.     //用欧几里德扩展算法求e,d
  89.     for (int j = 3; j < On; j += 1331)
  90.     {
  91.         int gcd = Exgcd(j, On, d);
  92.         if (gcd == 1 && d > 0)
  93.         {
  94.             e = j;
  95.             break;
  96.         }

  97.     }

  98. }

  99. //RSA加密
  100. void RSA_Encrypt()
  101. {
  102.     cout << "Public Key (e, n) : e = " << e << " n = " << n << '\n';
  103.     cout << "Private Key (d, n) : d = " << d << " n = " << n << '\n' << '\n';

  104.     int i = 0;
  105.     for (i = 0; i < 100; i++)
  106.         Ciphertext[i] = Modular_Exonentiation(Plaintext[i], e, n);

  107.     cout << "Use the public key (e, n) to encrypt:" << '\n';
  108.     for (i = 0; i < 100; i++)
  109.         cout << Ciphertext[i] << " ";
  110.     cout << '\n' << '\n';
  111. }

  112. //RSA解密
  113. void RSA_Decrypt()
  114. {
  115.     int i = 0;
  116.     for (i = 0; i < 100; i++)
  117.         Ciphertext[i] = Modular_Exonentiation(Ciphertext[i], d, n);

  118.     cout << "Use private key (d, n) to decrypt:" << '\n';
  119.     for (i = 0; i < 100; i++)
  120.         cout << Ciphertext[i] << " ";
  121.     cout << '\n' << '\n';
  122. }


  123. //算法初始化
  124. void Initialize()
  125. {
  126.     int i;
  127.     srand((unsigned)time(NULL));
  128.     for (i = 0; i < 100; i++)
  129.         Plaintext[i] = rand() % 1000;

  130.     cout << "Generate 100 random numbers:" << '\n';
  131.     for (i = 0; i < 100; i++)
  132.         cout << Plaintext[i] << " ";
  133.     cout << '\n' << '\n';
  134. }

  135. int main()
  136. {
  137.     Initialize();

  138.     while (!e)
  139.         RSA_Initialize();

  140.     RSA_Encrypt();

  141.     RSA_Decrypt();

  142.     return 0;
  143. }
复制代码


运行截图:

本帖子中包含更多资源

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

x
回复

使用道具 举报

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

本版积分规则

教学服务系统

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

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

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