教学服务系统

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

信息计算2019级1班2号孙志慧

[复制链接]

8

主题

19

帖子

114

积分

注册会员

Rank: 2

积分
114
发表于 2022-4-23 11:57:48 | 显示全部楼层 |阅读模式
mooc截图

本帖子中包含更多资源

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

x
回复

使用道具 举报

8

主题

19

帖子

114

积分

注册会员

Rank: 2

积分
114
 楼主| 发表于 2022-4-23 11:59:04 | 显示全部楼层
欧拉定理

本帖子中包含更多资源

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

x
回复

使用道具 举报

8

主题

19

帖子

114

积分

注册会员

Rank: 2

积分
114
 楼主| 发表于 2022-4-30 23:22:57 | 显示全部楼层
4.28日视频截图

本帖子中包含更多资源

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

x
回复

使用道具 举报

8

主题

19

帖子

114

积分

注册会员

Rank: 2

积分
114
 楼主| 发表于 2022-5-2 23:54:28 | 显示全部楼层
本帖最后由 孙志慧 于 2022-5-3 00:06 编辑
  1. public class Node {
  2.         private double p;
复制代码
  1. import java.math.BigInteger;
  2. public class Prime {
  3.         public boolean sushu(double n) {
  4.                 boolean isPrime = true;              
  5.                 if (n > 2) {               
  6.                         if (n % 2 == 0) {
  7.                                 isPrime = false;
  8.                         } else {                        
  9.                                 for (int i = 3; i <= (int) Math.sqrt(n); i += 2) {
  10.                                         if (n % i == 0) {
  11.                                                 isPrime = false;
  12.                                                 break;
  13.                                         }
  14.                                 }
  15.                         }
  16.                 }
  17.                 return isPrime;
  18.         }
  19.         private static final BigInteger ZERO = BigInteger.ZERO;
  20.         private static final BigInteger ONE = BigInteger.ONE;
  21.         private static final BigInteger TWO = new BigInteger("2");
  22.         private static final int ERR_VAL = 100;
  23.         public BigInteger nextPrime(BigInteger start) {
  24.                 // 产生一个比给定大整数 start 大的素数,错误率低于 1/2 的 ERR_VAL 次方
  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.                         // 采用递归方式(递归的层数会是个天文数字吗?)
  33.                         return (nextPrime(start));
  34.         }
  35.         private static boolean isEven(BigInteger n) {
  36.                 // 测试一个大整数是否为偶数
  37.                 return (n.mod(TWO).equals(ZERO));
  38.         }
  39.         public BigInteger bigRandom(int numDigits) {
  40.                 // 产生一个随机大整数,各位上的数字都是随机产生的,首位不为 0
  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.                 // 产生一个随机的数字(字符串形式的)
  51.                 int index;
  52.                 if (isZeroOK)
  53.                         index = (int) Math.floor(Math.random() * 10);
  54.                 else
  55.                         index = 1 + (int) Math.floor(Math.random() * 9);
  56.                 return (digits[index]);
  57.         }
  58.         private static StringBuffer[] digits = { new StringBuffer("0"),
  59.                         new StringBuffer("1"), new StringBuffer("2"),
  60.                         new StringBuffer("3"), new StringBuffer("4"),
  61.                         new StringBuffer("5"), new StringBuffer("6"),
  62.                         new StringBuffer("7"), new StringBuffer("8"), new StringBuffer("9") };
  63.         public static void main(String[] args) {
  64.         }
  65. }
复制代码
  1. import java.math.BigInteger;
  2. import java.util.ArrayList;
  3. import java.util.Scanner;
  4. public class RSA {
  5.         static BigInteger x, 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 private(BigInteger a, BigInteger b) {
  19.                 BigInteger temp;
  20.                 if (b.equals(new BigInteger("0"))) {
  21.                         x = new BigInteger("1");
  22.                         y = new BigInteger("0");
  23.                         return x;
  24.                 }
  25.                 private(b, a.mod(b));
  26.                 temp = x;
  27.                 x = y;
  28.                 y = temp.subtract((a.divide(b).multiply(y)));
  29.                 return x;
  30.         }
  31.         public static double entropy(String mess) {
  32.                 ArrayList<Node> result = new ArrayList<Node>();
  33.                 result.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 < result.size(); j++) {
  38.                                 if (result.get(j).getalpha() == mess.charAt(i)) {
  39.                                         flag_exit = false;
  40.                                         result.get(j).setp(result.get(j).getp() + 1 / num);
  41.                                 }
  42.                         }
  43.                         if (flag_exit)
  44.                                result.add(new Node(1 / num, mess.charAt(i)));
  45.                 }
  46.                 //计算熵
  47.                 double entropy = 0;
  48.                 for (int i = 0; i < result.size(); i++) {
  49.                         double p1 = result.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
  101.                 d = private(e, t);
  102.                 System.out.println("随机生成2个素数p,q,如下:");
  103.                 System.out.println(p);
  104.                 System.out.println(q);
  105.                 System.out.println("计算得到的n:");
  106.                 System.out.println(n);
  107.                 System.out.println("计算得到的t:");
  108.                 System.out.println(t);
  109.                 System.out.println("随机产生的公钥:");
  110.                 System.out.println(e);
  111.                 System.out.println("由扩展的欧几里德算法求得私钥:");
  112.                 System.out.println(d);
  113.                 while (true) {
  114.                         System.out.println("请输入明文:");
  115.                         String mess = input.nextLine();         
  116.                         BigInteger m = new BigInteger(mess.getBytes());
  117.                         System.out.println("明文的大整数表示形式:" + m);
  118.                         //调用函数计算信息,统计信息
  119.                         mess = m.toString();
  120.                         System.out.println("计算得明文熵:" + entropy(mess));
  121.                         // 修改第一位,+1
  122.                         c = m.modPow(e, n);
复制代码

本帖子中包含更多资源

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

x
回复

使用道具 举报

8

主题

19

帖子

114

积分

注册会员

Rank: 2

积分
114
 楼主| 发表于 2022-5-6 14:41:25 | 显示全部楼层
mooc截图

本帖子中包含更多资源

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

x
回复

使用道具 举报

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

本版积分规则

教学服务系统

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

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

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