教学服务系统

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

信息计算2019级1班4号吴春滟

[复制链接]

9

主题

20

帖子

85

积分

注册会员

Rank: 2

积分
85
发表于 2022-4-19 11:41:13 | 显示全部楼层 |阅读模式

本帖子中包含更多资源

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

x
回复

使用道具 举报

9

主题

20

帖子

85

积分

注册会员

Rank: 2

积分
85
 楼主| 发表于 2022-4-29 22:17:24 | 显示全部楼层

本帖子中包含更多资源

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

x
回复

使用道具 举报

9

主题

20

帖子

85

积分

注册会员

Rank: 2

积分
85
 楼主| 发表于 2022-5-3 09:09:38 | 显示全部楼层
本帖最后由 吴春滟 于 2022-6-2 21:58 编辑
  1. <b style="font-size: x-large; background-color: rgb(255, 255, 255);">大数RSA算法的实现</b>
复制代码
  1. package aa;

  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. }
复制代码
  1. package aa;

  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. for (int i = 3; i <= (int) Math.sqrt(n); i += 2) {
  14. if (n % i == 0) {
  15. isPrime = false;
  16. break;
  17. }
  18. }
  19. }

  20. }
  21. return isPrime;
  22. }

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

  27. public BigInteger nextPrime(BigInteger start) {

  28. if (isEven(start))
  29. start = start.add(ONE);
  30. else
  31. start = start.add(TWO);
  32. if (start.isProbablePrime(ERR_VAL))
  33. return (start);
  34. else

  35. return (nextPrime(start));
  36. }

  37. private static boolean isEven(BigInteger n) {

  38. return (n.mod(TWO).equals(ZERO));
  39. }

  40. public BigInteger bigRandom(int numDigits) {

  41. StringBuffer s = new StringBuffer("");
  42. for (int i = 0; i < numDigits; i++)
  43. if (i == 0)
  44. s.append(randomDigit(false));
  45. else
  46. s.append(randomDigit(true));
  47. return (new BigInteger(s.toString()));
  48. }

  49. private StringBuffer randomDigit(boolean isZeroOK) {

  50. int index;
  51. if (isZeroOK)
  52. index = (int) Math.floor(Math.random() * 10);
  53. else
  54. index = 1 + (int) Math.floor(Math.random() * 9);
  55. return (digits[index]);
  56. }

  57. private static StringBuffer[] digits = { new StringBuffer("0"),
  58. new StringBuffer("1"), new StringBuffer("2"),
  59. new StringBuffer("3"), new StringBuffer("4"),
  60. new StringBuffer("5"), new StringBuffer("6"),
  61. new StringBuffer("7"), new StringBuffer("8"), new StringBuffer("9") };

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

  63. }
  64. }
复制代码
  1. package aa;
  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. System.out.println("密文的字符串表示形式:" + new String(c.toByteArray()));
  128. m = c.modPow(d, n);
  129. System.out.println("解密得到明文的大整数表示形式:" + c);
  130. String str = new String(m.toByteArray());// 把返回的结果还原成一个字串
  131. System.out.println("解密得到明文为:" + str);
  132. }
  133. }
  134. }
复制代码




本帖子中包含更多资源

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

x
回复

使用道具 举报

9

主题

20

帖子

85

积分

注册会员

Rank: 2

积分
85
 楼主| 发表于 2022-6-2 21:53:33 | 显示全部楼层

本帖子中包含更多资源

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

x
回复

使用道具 举报

9

主题

20

帖子

85

积分

注册会员

Rank: 2

积分
85
 楼主| 发表于 2022-6-2 21:57:21 | 显示全部楼层
大数RSA算法的实现

一.原理:
1. 随意选择两个大的质数p和q,p不等于q,计算N = pq.
2. 根据欧拉函数,求得r=φ(N)=φ(p)φ(q)=(p-1)(q-1)。
3. 选择一个小于r的整数e,是e与r互质。并求得e关于r的模反元素,命名为d。(求d令ed≡1(mod r))。(模反元素存在,当且仅当e与r互质)
4.公开整数n和e,秘密保存d;
5.将明文m(m<n,是一个整数)加密成密文c,加密算法为c=E(m)=memodn;
6.将密文c解密为明文m,解密算法为m=D(c)=cdmodn。



  1. package mm;
  2. import java.util.Random;
  3. import java.util.Scanner;
  4. import java.math.BigInteger;
  5. import java.security.SecureRandom;

  6. public class Rsa1 {
  7.         private static final int ORDER = 100000;// 随机数的数量级
  8.         private static final int MIN = 10000; // 选择的随机数的最小值

  9.         public static long getPrime() { // 获取一个随机数,判断是否为素数,并用MillerRabin方法检验
  10.                 long x = 0;
  11.                 while (x % 2 == 0 || !MillerRabin(x, 5)) {
  12.                         x = getRandom();
  13.                 }
  14.                 return x;
  15.         }

  16.         public static long getRandom() {// 随机生成一个奇数
  17.                 long x = 3;
  18.                 Random rd = new Random();
  19.                 do {
  20.                         x = rd.nextInt(ORDER);
  21.                 } while (x < MIN || x % 2 == 0);
  22.                 return x;
  23.         }

  24.         public static boolean MillerRabin(long n, int t) {
  25.                 for (int i = 0; i < t; i++)
  26.                         if (!isPrime(n))
  27.                                 return false;
  28.                 return true;
  29.         }

  30.         public static boolean isPrime(long n) {
  31.                 long k, q;
  32.                 SecureRandom random = new SecureRandom();
  33.                 for (k = 0; (((n - 1) >> k) & 1) == 0; k++)
  34.                         ;
  35.                 q = (n - 1) >> k;
  36.                 long a = random.nextLong(n);
  37.                 if (squareMultiply(a, q, n) == 1)
  38.                         return true;
  39.                 for (int j = 0; j < k; j++)
  40.                         if (squareMultiply(a, (int) Math.pow(2, j) * q, n) == n - 1)
  41.                                 return true;
  42.                 return false;
  43.         }

  44.         public static long squareMultiply(long a, long b, long p) {
  45.                 long x = 1, y = a;
  46.                 long len = (long) Math.ceil((Math.log(b) / Math.log(2)));
  47.                 for (int i = 0; i < len; i++) {
  48.                         if (((b >> i) & 1) == 1) {
  49.                                 x = (x * y) % p;
  50.                         }
  51.                         y = (y * y) % p;
  52.                 }
  53.                 return x;
  54.         }

  55.         public static long powMod(long x, long y, long z) {//模幂运算

  56.                 if (y == 0)
  57.                         return 1 % z;

  58.                 long half = powMod(x, y >> 1, z);

  59.                 half = (half * half) % z;

  60.                 if ((y & 1) == 0) { // y是偶数

  61.                         return half;

  62.                 } else { // y是奇数

  63.                         return (half * (x % z)) % z;

  64.                 }

  65.         }

  66.         public static long Inverse(long a, long b) { // 模逆运算
  67.                 long[] m = { 1, 0, a };
  68.                 long[] n = { 0, 1, b };
  69.                 long[] temp = new long[3];
  70.                 long q = 0; // 初始化
  71.                 boolean flag = true;
  72.                 while (flag) {
  73.                         q = m[2] / n[2];
  74.                         for (int i = 0; i < 3; i++) {
  75.                                 temp[i] = m[i] - q * n[i];
  76.                                 m[i] = n[i];
  77.                                 n[i] = temp[i];
  78.                         }
  79.                         if (n[2] == 1) {
  80.                                 if (n[1] < 0) {
  81.                                         n[1] = n[1] + a;
  82.                                 }
  83.                                 return n[1];
  84.                         }
  85.                         if (n[2] == 0) {
  86.                                 flag = false;
  87.                         }
  88.                 }
  89.                 return 0;
  90.         }

  91.         public static void main(String[] args) {
  92.                 long p = getPrime(); // 生成两个素数
  93.                 long q = getPrime();
  94.                 while (p == q) {
  95.                         q = getPrime();
  96.                 }
  97.                 long n = p * q;
  98.                 long φ = (p - 1) * (q - 1);
  99.                 long e = getPrime();
  100.                 long d = Inverse(φ, e);
  101.                 System.out.println("p:"+p);
  102.                 System.out.println("q:"+q);
  103.                 System.out.println("n:"+n);
  104.                 System.out.println("φ:"+φ);
  105.                 System.out.println("e:"+e);
  106.                 System.out.println("d:"+d);

  107.                 System.out.print("请输入明文:");
  108.                 Scanner sc = new Scanner(System.in);
  109.                 long m=sc.nextLong();
  110.                 long c=powMod(m, e, n);
  111.                 System.out.println("密文:" + c);
  112.                 m = powMod(c, d, n);
  113.                 System.out.println("解密得到明文:" + m);
  114.                 sc.close();
  115.         }
  116. }
复制代码


本帖子中包含更多资源

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

x
回复

使用道具 举报

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

本版积分规则

教学服务系统

GMT+8, 2025-4-30 07:40 , Processed in 0.020741 second(s), 19 queries .

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

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