教学服务系统

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

信息计算2019级2班15号陈周易

[复制链接]

9

主题

21

帖子

99

积分

注册会员

Rank: 2

积分
99
发表于 2022-5-3 16:35:29 | 显示全部楼层 |阅读模式
慕课截图

本帖子中包含更多资源

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

x
回复

使用道具 举报

9

主题

21

帖子

99

积分

注册会员

Rank: 2

积分
99
 楼主| 发表于 2022-5-6 16:05:54 | 显示全部楼层
  1. package secret;

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

使用道具 举报

9

主题

21

帖子

99

积分

注册会员

Rank: 2

积分
99
 楼主| 发表于 2022-5-6 16:07:00 | 显示全部楼层
  1. package secret;

  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.                 // 产生一个比给定大整数 start 大的素数
  29.                 if (isEven(start))
  30.                         start = start.add(ONE);
  31.                 else
  32.                         start = start.add(TWO);
  33.                 if (start.isProbablePrime(ERR_VAL))
  34.                         return (start);
  35.                 else
  36.                         return (nextPrime(start));
  37.         }

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

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

  52.         private StringBuffer randomDigit(boolean isZeroOK) {
  53.                 // 产生一个随机的数字(字符串形式的)
  54.                 int index;
  55.                 if (isZeroOK)
  56.                         index = (int) Math.floor(Math.random() * 10);
  57.                 else
  58.                         index = 1 + (int) Math.floor(Math.random() * 9);
  59.                 return (digits[index]);
  60.         }

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

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

  67.         }
  68. }
复制代码
回复

使用道具 举报

9

主题

21

帖子

99

积分

注册会员

Rank: 2

积分
99
 楼主| 发表于 2022-5-6 16:07:29 | 显示全部楼层
  1. package secret;

  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.                         c = m.modPow(e, n);// JAVA中的方法
  123.                         System.out.println("密文的整数表示形式:" + c);
  124.                         System.out.println("密文的字符串表示形式:" + new String(c.toByteArray()));
  125.                         m = c.modPow(d, n);
  126.                         String str = new String(m.toByteArray());// 把返回的结果还原成一个字串
  127.                         System.out.println("解密得到明文为:" + str);
  128.                 }
  129.         }
  130. }
复制代码
回复

使用道具 举报

9

主题

21

帖子

99

积分

注册会员

Rank: 2

积分
99
 楼主| 发表于 2022-5-6 16:08:34 | 显示全部楼层
运行截图

本帖子中包含更多资源

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

x
回复

使用道具 举报

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

本版积分规则

教学服务系统

GMT+8, 2025-4-30 14:33 , Processed in 0.015031 second(s), 19 queries .

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

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