教学服务系统

 找回密码
 立即注册
搜索
查看: 639|回复: 2

信息计算2019级2班2号胡莘梅

[复制链接]

10

主题

25

帖子

118

积分

注册会员

Rank: 2

积分
118
发表于 2022-5-3 23:32:17 | 显示全部楼层 |阅读模式

本帖子中包含更多资源

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

x
回复

使用道具 举报

10

主题

25

帖子

118

积分

注册会员

Rank: 2

积分
118
 楼主| 发表于 2022-5-3 23:34:50 | 显示全部楼层
本帖最后由 信计19-2胡莘梅 于 2022-5-30 20:41 编辑

大数版本RSA

一、RSA介绍
  RSA是被研究得最广泛的公钥算法,也是第一个能同时用于加密和数字签名的算法。
  RSA算法是一种非对称密码算法,所谓非对称,就是指该算法需要一对密钥,使用其中一个加密,则需要用另一个才能解密。
  RSA的算法涉及三个参数,n、e1、 e2。

  其中,n是两个大质数p、q的积,n的二进制表示时所占用的位数,就是所谓的密钥长度。
  e1和e2是一对相关的值,e1可以任意取,但要求el与(p-1)*(q-1)互质;
  再选择e2,要求(e2*e 1)mod((p-1)*(q-1))=1。满足上述条件后,(n及e1),(n及e2)就是密钥对。

二、RSA算法实现步骤
产生非对称密钥的算法流程

加解密算法过程

三、运行截图

本帖子中包含更多资源

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

x
回复

使用道具 举报

10

主题

25

帖子

118

积分

注册会员

Rank: 2

积分
118
 楼主| 发表于 2022-5-30 20:45:53 | 显示全部楼层
四、源代码
(1)Node.java
  1. package rsa;

  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. }
复制代码

(2)Prime.java
  1. package rsa;

  2. import java.math.BigInteger;

  3. public class Prime {
  4. public boolean sushu(double n) {
  5. boolean isPrime = true;
  6. if (n > 2) {
  7. if (n % 2 == 0) {
  8. isPrime = false;
  9. } else {
  10. for (int i = 3; i <= (int) Math.sqrt(n); i += 2) {
  11. if (n % i == 0) {
  12. isPrime = false;
  13. break;
  14. }
  15. }
  16. }

  17. }
  18. return isPrime;
  19. }

  20. private static final BigInteger ZERO = BigInteger.ZERO;
  21. private static final BigInteger ONE = BigInteger.ONE;
  22. private static final BigInteger TWO = new BigInteger("2");
  23. private static final int ERR_VAL = 100;

  24. public BigInteger nextPrime(BigInteger start) {
  25. if (isEven(start))
  26. start = start.add(ONE);
  27. else
  28. start = start.add(TWO);
  29. if (start.isProbablePrime(ERR_VAL))
  30. return (start);
  31. else
  32. return (nextPrime(start));
  33. }

  34. private static boolean isEven(BigInteger n) {
  35. return (n.mod(TWO).equals(ZERO));
  36. }

  37. public BigInteger bigRandom(int numDigits) {
  38. StringBuffer s = new StringBuffer("");
  39. for (int i = 0; i < numDigits; i++)
  40. if (i == 0)
  41. s.append(randomDigit(false));
  42. else
  43. s.append(randomDigit(true));
  44. return (new BigInteger(s.toString()));
  45. }

  46. private StringBuffer randomDigit(boolean isZeroOK) {
  47. int index;
  48. if (isZeroOK)
  49. index = (int) Math.floor(Math.random() * 10);
  50. else
  51. index = 1 + (int) Math.floor(Math.random() * 9);
  52. return (digits[index]);
  53. }

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

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

  60. }
  61. }

复制代码


(3)RSA.java
  1. package rsa;

  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. double entropy = 0;
  48. for (int i = 0; i < jieguo.size(); i++) {
  49. double p1 = jieguo.get(i).getp();
  50. entropy += (-p1 * (Math.log(p1) / Math.log(2)));
  51. }
  52. return entropy;
  53. }

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

回复

使用道具 举报

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

本版积分规则

教学服务系统

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

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

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