教学服务系统

 找回密码
 立即注册
搜索
查看: 589|回复: 1

信息计算2019级1班18号冯雨

[复制链接]

11

主题

17

帖子

83

积分

注册会员

Rank: 2

积分
83
发表于 2022-5-2 23:01:31 | 显示全部楼层 |阅读模式
本帖最后由 冯雨 于 2022-5-2 23:04 编辑
  1. import java.math.BigInteger;
  2. import java.util.ArrayList;
  3. import java.util.Scanner;

  4. public class Rsamim01 {
  5.         static BigInteger z, y;

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

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

  31.         public static double entropy(String mess) {
  32.                 ArrayList<Node> jieguo = new ArrayList<Node>();
  33.                 jieguo.clear();
  34.                 double num = mess.length();
  35.                 for (int i = 0; i < num; i++) {
  36.                         boolean flag_exit = true;
  37.                         for (int j = 0; j < jieguo.size(); j++) {
  38.                                 if (jieguo.get(j).getalpha() == mess.charAt(i)) {
  39.                                         flag_exit = false;
  40.                                         jieguo.get(j).setp(jieguo.get(j).getp() + 1 / num);
  41.                                 }
  42.                         }
  43.                         if (flag_exit)
  44.                                 jieguo.add(new Node(1 / num, mess.charAt(i)));
  45.                 }
  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.         
  55.         
  56.         public static void main(String[] args) {
  57.                 BigInteger p = null;
  58.                 BigInteger e, d, n, t, c;
  59.                 BigInteger q = null;
  60.                 for (int i = 0; i < 2; i++) {
  61.                         int numDigits;
  62.                         Prime pri = new Prime();
  63.                         try {
  64.                                 numDigits = Integer.parseInt("15");
  65.                         } catch (Exception e1) {
  66.                                 numDigits = 128;
  67.                         }
  68.                         BigInteger start = pri.bigRandom(numDigits);
  69.                         BigInteger big = pri.nextPrime(start);
  70.                         if (i == 0)
  71.                                 p = big;
  72.                         else
  73.                                 q = big;
  74.                 }
  75.                 Scanner input = new Scanner(System.in);
  76.                 n = p.multiply(q);
  77.                 t = p.subtract(new BigInteger("1")).multiply(
  78.                                 q.subtract(new BigInteger("1")));
  79.                 int numDigits;
  80.                 Prime pri = new Prime();
  81.                 try {
  82.                         numDigits = Integer.parseInt("15");
  83.                 } catch (Exception e1) {
  84.                         numDigits = 128;
  85.                 }
  86.                 BigInteger start = pri.bigRandom(numDigits);
  87.                 BigInteger big = pri.nextPrime(start);
  88.                 e = big;
  89.                 while (e.compareTo(t) == 1 || fun(e, t)) {
  90.                         System.out.println("e不合要求,请重新产生" + (e.compareTo(t) == 1)
  91.                                         + fun(e, t));
  92.                         pri = new Prime();
  93.                         try {
  94.                                 numDigits = Integer.parseInt("15");
  95.                         } catch (Exception e1) {
  96.                                 numDigits = 128;
  97.                         }
  98.                         start = pri.bigRandom(numDigits);
  99.                         big = pri.nextPrime(start);
  100.                         e = big;
  101.                 }
  102.                 d = siyue(e, t);
  103.                 System.out.println("由计算机随机产生2个素数p,q:");
  104.                 System.out.println("素数p:"+p);
  105.                 System.out.println("素数q:"+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.                 while (true) {
  113.                         System.out.println("请输入明文:");
  114.                         String mess = input.nextLine();
  115.                         BigInteger m = new BigInteger(mess.getBytes());// 把字串转成一个BigInteger对象
  116.                         System.out.println("明文的大整数表示形式:" + m);
  117.                         /** 调用函数计算信息,统计信息 */
  118.                         mess = m.toString();
  119.                         c = m.modPow(e, n);// JAVA中的方法
  120.                         System.out.println("密文的大整数表示形式:" + c);
  121.                         System.out.println("密文的字符串表示形式:" + new String(c.toByteArray()));
  122.                         m = c.modPow(d, n);
  123.                         System.out.println("解密得到明文的大整数表示形式:" + c);
  124.                         String str = new String(m.toByteArray());// 把返回的结果还原成一个字串
  125.                         System.out.println("解密得到:" + str);
  126.                 }
  127.         }
  128. }
  129. class Prime {
  130.         public boolean sushu(double n) {
  131.                 // 判断素数
  132.                 boolean isPrime = true;
  133.                 // 如果n大于2 继续判断 否则 isPrime的值不变 2素数
  134.                 if (n > 2) {
  135.                         // 如果n是大于2的偶数 认定不是素数 修改变量值为false
  136.                         if (n % 2 == 0) {
  137.                                 isPrime = false;
  138.                         } else {
  139.                                 // 循环判断如果找到一个可以整除的数 则判定不是素数跳出循环 因为是判断奇数 因此 2 4 6
  140.                                 // 不用考虑 循环递增
  141.                                 for (int i = 3; i <= (int) Math.sqrt(n); i += 2) {
  142.                                         if (n % i == 0) {
  143.                                                 isPrime = false;
  144.                                                 break;
  145.                                         }
  146.                                 }
  147.                         }

  148.                 }
  149.                 return isPrime;
  150.         }

  151.         private static final BigInteger ZERO = BigInteger.ZERO;
  152.         private static final BigInteger ONE = BigInteger.ONE;
  153.         private static final BigInteger TWO = new BigInteger("2");
  154.         private static final int ERR_VAL = 100;

  155.         public BigInteger nextPrime(BigInteger start) {
  156.                 if (isEven(start))
  157.                         start = start.add(ONE);
  158.                 else
  159.                         start = start.add(TWO);
  160.                 if (start.isProbablePrime(ERR_VAL))
  161.                         return (start);
  162.                 else
  163.                         // 采用递归方式(递归的层数会是个天文数字吗?)
  164.                         return (nextPrime(start));
  165.         }

  166.         private static boolean isEven(BigInteger n) {
  167.                 // 测试一个大整数是否为偶数
  168.                 return (n.mod(TWO).equals(ZERO));
  169.         }

  170.         public BigInteger bigRandom(int numDigits) {
  171.                 // 产生一个随机大整数,各位上的数字都是随机产生的,首位不为 0
  172.                 StringBuffer s = new StringBuffer("");
  173.                 for (int t= 0; t< numDigits; t++)
复制代码

本帖子中包含更多资源

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

x
回复

使用道具 举报

11

主题

17

帖子

83

积分

注册会员

Rank: 2

积分
83
 楼主| 发表于 2022-5-3 15:56:51 | 显示全部楼层

本帖子中包含更多资源

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

x
回复

使用道具 举报

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

本版积分规则

教学服务系统

GMT+8, 2025-4-30 16:37 , Processed in 0.014531 second(s), 19 queries .

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

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