|

楼主 |
发表于 2022-5-6 16:07:29
|
显示全部楼层
- package secret;
- import java.math.BigInteger;
- import java.util.ArrayList;
- import java.util.Scanner;
-
- public class RSA {
- static BigInteger x, y;
-
- public static boolean fun(BigInteger x, BigInteger y) {
- BigInteger t;
- while (!(y.equals(new BigInteger("0")))) {
- t = x;
- x = y;
- y = t.mod(y);
- }
- if (x.equals(new BigInteger("1")))
- return false;
- else
- return true;
- }
-
- public static BigInteger siyue(BigInteger a, BigInteger b) {
- BigInteger temp;
- if (b.equals(new BigInteger("0"))) {
- x = new BigInteger("1");
- y = new BigInteger("0");
- return x;
- }
- siyue(b, a.mod(b));
- temp = x;
- x = y;
- y = temp.subtract((a.divide(b).multiply(y)));
- return x;
- }
-
- public static double entropy(String mess) {
- ArrayList<node> jieguo = new ArrayList<node>();
- jieguo.clear();
- double num = mess.length();
- for (int i = 0; i < num; i++) {
- boolean flag_exit = true;
- for (int j = 0; j < jieguo.size(); j++) {
- if (jieguo.get(j).getalpha() == mess.charAt(i)) {
- flag_exit = false;
- jieguo.get(j).setp(jieguo.get(j).getp() + 1 / num);
- }
- }
- if (flag_exit)
- jieguo.add(new node(1 / num, mess.charAt(i)));
- }
- /** 计算熵 */
- double entropy = 0;
- for (int i = 0; i < jieguo.size(); i++) {
- double p1 = jieguo.get(i).getp();
- entropy += (-p1 * (Math.log(p1) / Math.log(2)));
- }
- return entropy;
- }
-
- public static void main(String[] args) {
- BigInteger p = null;
- BigInteger e, d, n, t, c;
- BigInteger q = null;
- for (int i = 0; i < 2; i++) {
- int numDigits;
- Prime pri = new Prime();
- try {
- numDigits = Integer.parseInt("15");
- } catch (Exception e1) {
- numDigits = 128;
- }
- BigInteger start = pri.bigRandom(numDigits);
- BigInteger big = pri.nextPrime(start);
- if (i == 0)
- p = big;
- else
- q = big;
- }
- Scanner input = new Scanner(System.in);
- n = p.multiply(q);
- t = p.subtract(new BigInteger("1")).multiply(
- q.subtract(new BigInteger("1")));
- int numDigits;
- Prime pri = new Prime();
- try {
- numDigits = Integer.parseInt("15");
- } catch (Exception e1) {
- numDigits = 128;
- }
- BigInteger start = pri.bigRandom(numDigits);
- BigInteger big = pri.nextPrime(start);
- e = big;
- while (e.compareTo(t) == 1 || fun(e, t)) {
- System.out.println("e不合要求,请重新产生" + (e.compareTo(t) == 1)
- + fun(e, t));
- pri = new Prime();
- try {
- numDigits = Integer.parseInt("15");
- } catch (Exception e1) {
- numDigits = 128;
- }
- start = pri.bigRandom(numDigits);
- big = pri.nextPrime(start);
- e = big;
- }
- // 求出私钥d
- d = siyue(e, t);
- System.out.println("由计算机随机产生2个素数p,q,分别如下:");
- System.out.println(p);
- System.out.println(q);
- System.out.println("计算得到的n:");
- System.out.println(n);
- System.out.println("计算得到的t:");
- System.out.println(t);
- System.out.println("随机产生的公钥:");
- System.out.println(e);
- System.out.println("由扩展的欧几里德算法求得私钥:");
- System.out.println(d);
- while (true) {
- System.out.println("请输入明文:");
- String mess = input.nextLine();
- //
- BigInteger m = new BigInteger(mess.getBytes());// 把字串转成一个BigInteger对象
- System.out.println("明文的整数表示形式:" + m);
- /** 调用函数计算信息,统计信息 */
- mess = m.toString();
- c = m.modPow(e, n);// JAVA中的方法
- System.out.println("密文的整数表示形式:" + c);
- System.out.println("密文的字符串表示形式:" + new String(c.toByteArray()));
- m = c.modPow(d, n);
- String str = new String(m.toByteArray());// 把返回的结果还原成一个字串
- System.out.println("解密得到明文为:" + str);
- }
- }
- }
复制代码 |
|