教学服务系统

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

信息计算2019级2班张柯冕

[复制链接]

8

主题

19

帖子

90

积分

注册会员

Rank: 2

积分
90
发表于 2022-4-22 14:18:44 | 显示全部楼层 |阅读模式
信息计算2019级2班张柯冕

本帖子中包含更多资源

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

x
回复

使用道具 举报

8

主题

19

帖子

90

积分

注册会员

Rank: 2

积分
90
 楼主| 发表于 2022-4-22 21:45:15 | 显示全部楼层
4.22日网课记录

本帖子中包含更多资源

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

x
回复

使用道具 举报

8

主题

19

帖子

90

积分

注册会员

Rank: 2

积分
90
 楼主| 发表于 2022-4-30 00:55:30 | 显示全部楼层
2022.4.29

本帖子中包含更多资源

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

x
回复

使用道具 举报

8

主题

19

帖子

90

积分

注册会员

Rank: 2

积分
90
 楼主| 发表于 2022-5-3 22:15:50 | 显示全部楼层
2022.5.3日慕课记录

本帖子中包含更多资源

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

x
回复

使用道具 举报

8

主题

19

帖子

90

积分

注册会员

Rank: 2

积分
90
 楼主| 发表于 2022-5-3 22:32:39 | 显示全部楼层
  1. package RSA;

  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.                                 // 循环判断如果找到一个可以整除的数 则判定不是素数跳出循环 因为是判断奇数 因此 2 4 6 ...?
  14.                                 // 不用考虑 循环递增2 即?3 5 7 ...
  15.                                 for (int i = 3; i <= (int) Math.sqrt(n); i += 2) {
  16.                                         if (n % i == 0) {
  17.                                                 isPrime = false;
  18.                                                 break;
  19.                                         }
  20.                                 }
  21.                         }

  22.                 }
  23.                 return isPrime;
  24.         }

  25.         private static final BigInteger ZERO = BigInteger.ZERO;
  26.         private static final BigInteger ONE = BigInteger.ONE;
  27.         private static final BigInteger TWO = new BigInteger("2");
  28.         private static final int ERR_VAL = 100;

  29.         public BigInteger nextPrime(BigInteger start) {
  30.                 // 产生一个比给定大整数 start 大的素数,错误率低于 1/2 的 ERR_VAL 次方
  31.                 if (isEven(start))
  32.                         start = start.add(ONE);
  33.                 else
  34.                         start = start.add(TWO);
  35.                 if (start.isProbablePrime(ERR_VAL))  //判断start有没有可能是素数
  36.                         return (start);
  37.                 else
  38.                         // 采用递归方式
  39.                         return (nextPrime(start));
  40.         }

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

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

  55.         private StringBuffer randomDigit(boolean isZeroOK) {
  56.                 // 产生一个随机的数字(字符串形式的),isZeroOK 决定这个数字是否可以为 0
  57.                 int index;
  58.                 if (isZeroOK)
  59.                         index = (int) Math.floor(Math.random() * 10);
  60.                 else
  61.                         index = 1 + (int) Math.floor(Math.random() * 9);
  62.                 return (digits[index]);
  63.         }

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

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

  70.         }
  71. }
  72. package RSA;

  73. import java.math.BigInteger;
  74. import java.util.Scanner;

  75. public class RSA {
  76.         static BigInteger x, y;

  77.         public static boolean fun(BigInteger x, BigInteger y) {
  78.                 BigInteger t;
  79.                 while (!(y.equals(new BigInteger("0")))) {
  80.                         t = x;
  81.                         x = y;
  82.                         y = t.mod(y);
  83.                 }
  84.                 if (x.equals(new BigInteger("1")))
  85.                         return false;
  86.                 else
  87.                         return true;
  88.         }

  89.         public static BigInteger siyao(BigInteger a, BigInteger b) {
  90.                 BigInteger temp;
  91.                 if (b.equals(new BigInteger("0"))) {
  92.                         x = new BigInteger("1");
  93.                         y = new BigInteger("0");
  94.                         return x;
  95.                 }
  96.                 siyao(b, a.mod(b));
  97.                 temp = x;
  98.                 x = y;
  99.                 y = temp.subtract((a.divide(b).multiply(y)));
  100.                 return x;
  101.         }

  102.         public static void main(String[] args) {
  103.                 BigInteger p = null;
  104.                 BigInteger e, d, n, t, c;
  105.                 BigInteger q = null;
  106.                 for (int i = 0; i < 2; i++) {// 生成p,q
  107.                         int numDigits;
  108.                         Prime pri = new Prime();
  109.                         numDigits = Integer.parseInt("10");
  110.                         BigInteger start = pri.bigRandom(numDigits);// 生成一个十位的大数
  111.                         BigInteger big = pri.nextPrime(start);// 求出比start大的素数
  112.                         if (i == 0)
  113.                                 p = big; // 把第一次生成的数赋值给p
  114.                         else
  115.                                 q = big; // 把第二次生成的数赋值给q
  116.                 }
  117.                
  118.                 Scanner sc = new Scanner(System.in); // 键盘输入明文
  119.                 n = p.multiply(q); // 求出φ(n)
  120.                 t = p.subtract(new BigInteger("1")).multiply( // 求出φ(n)=t=(p-1)*(q-1)
  121.                                 q.subtract(new BigInteger("1")));
  122.                 int numDigits;
  123.                 Prime pri = new Prime();
  124.                 numDigits = Integer.parseInt("10");
  125.                 BigInteger start = pri.bigRandom(numDigits);
  126.                 BigInteger big = pri.nextPrime(start); // 找出一个质数e
  127.                 e = big;
  128.                 // 求出私钥d
  129.                 d = siyao(e, t);
  130.                 System.out.println("产生2个素数");
  131.                 System.out.println("p:"+p);
  132.                 System.out.println("q:"+q);
  133.                 System.out.println("n=p*q:"+n);
  134.                 System.out.println("e:"+e);
  135.                 System.out.println("φ(n)=(p-1)*(q-1):"+t);
  136.                 System.out.println("公钥:"+e);
  137.                 System.out.println("私钥:"+d);
  138.                 System.out.println("请输入明文:");
  139.                 String mess = sc.nextLine();
  140.                 BigInteger m = new BigInteger(mess.getBytes());
  141.                 System.out.println("明文的大整数:" + m);
  142.                 c = m.modPow(e, n);
  143.                 System.out.println("密文的大整数:" + c);
  144.                 m = c.modPow(d, n);
  145.                 System.out.println("解密得到明文的大整数:" + c);
  146.                 String str = new String(m.toByteArray());
  147.                 System.out.println("解密得到明文为:" + str);
  148.         }

  149. }
复制代码

本帖子中包含更多资源

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

x
回复

使用道具 举报

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

本版积分规则

教学服务系统

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

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

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