教学服务系统

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

信息计算2019级2班3号徐柳婧

[复制链接]

8

主题

24

帖子

100

积分

注册会员

Rank: 2

积分
100
发表于 2022-4-19 13:10:35 | 显示全部楼层 |阅读模式
本帖最后由 徐柳婧 于 2022-4-19 13:11 编辑

mooc 4月19日学习记录

本帖子中包含更多资源

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

x
回复

使用道具 举报

8

主题

24

帖子

100

积分

注册会员

Rank: 2

积分
100
 楼主| 发表于 2022-4-22 15:51:21 | 显示全部楼层
4月22日mooc学习记录


本帖子中包含更多资源

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

x
回复

使用道具 举报

8

主题

24

帖子

100

积分

注册会员

Rank: 2

积分
100
 楼主| 发表于 2022-4-30 19:36:07 | 显示全部楼层

信息计算2019级2班3号徐柳婧

4月29日mooc学习记录


本帖子中包含更多资源

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

x
回复

使用道具 举报

8

主题

24

帖子

100

积分

注册会员

Rank: 2

积分
100
 楼主| 发表于 2022-5-3 11:28:59 | 显示全部楼层

信息计算2019级2班3号徐柳婧

5月3日mooc学习截图


本帖子中包含更多资源

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

x
回复

使用道具 举报

8

主题

24

帖子

100

积分

注册会员

Rank: 2

积分
100
 楼主| 发表于 2022-5-3 23:34:05 | 显示全部楼层
本帖最后由 徐柳婧 于 2022-5-30 19:09 编辑

一、RSA算法描述
(1)任意选取两个不同的大素数p和q计算乘积

(2)任意选取一个大整数e,满足,整数e用做加密钥 ;
(3)确定的解密钥d,满足,即 是一个任意的整数;所以,若知道e和,则很容易计算出d
(4)公开整数n和e,秘密保存d ;
(5)将明文m(m<n是一个整数)加密成密文c,加密算法为

(6)将密文c解密为明文m,解密算法为

二、源代码
node.java
  1. public class node {
  2.         private double p;
  3.         private char alpha;
  4.         public node(double p, char alpha) {
  5.                 this.p = p;
  6.                 this.alpha = alpha;
  7.         }
  8.         public void setp(double p) {
  9.                 this.p = p;
  10.         }
  11.         public void setalpha(char a) {
  12.                 this.alpha = a;
  13.         }
  14.         public double getp() {
  15.                 return p;
  16.         }
  17.         public char getalpha() {
  18.                 return alpha;
  19.         }
  20. }
复制代码
prime.java
  1. public class Prime {
  2.         public boolean sushu(double n) {
  3.                 boolean isPrime = true;
  4.                 if (n > 2) {
  5.                         if (n % 2 == 0) {
  6.                                 isPrime = false;
  7.                         } else {
  8.                                 for (int i = 3; i <= (int) Math.sqrt(n); i += 2) {
  9.                       if (n % i == 0) {
  10.                      isPrime = false;
  11.                     break;
  12.                   }
  13.                 }
  14.             }
  15.         }
  16.         return isPrime;
  17.         }
  18.         private static final BigInteger ZERO = BigInteger.ZERO;
  19.         private static final BigInteger ONE = BigInteger.ONE;
  20.         private static final BigInteger TWO = new BigInteger("2");
  21.         private static final int ERR_VAL = 100;
  22.         public BigInteger nextPrime(BigInteger start) {
  23.                 if (isEven(start))
  24.                         start = start.add(ONE);
  25.                 else
  26.                         start = start.add(TWO);
  27.                 if (start.isProbablePrime(ERR_VAL))
  28.                         return (start);
  29.                 else
  30.       return (nextPrime(start));
  31.         }
  32.         private static boolean isEven(BigInteger n) {
  33.                 return (n.mod(TWO).equals(ZERO));
  34.         }
  35.         public BigInteger bigRandom(int numDigits) {
  36.                 StringBuffer s = new StringBuffer("");
  37.                 for (int i = 0; i < numDigits; i++)
  38.                         if (i == 0)
  39.                                 s.append(randomDigit(false));
  40.                         else
  41.                                 s.append(randomDigit(true));
  42.                 return (new BigInteger(s.toString()));
  43.         }
  44.         private StringBuffer randomDigit(boolean isZeroOK) {
  45.                 int index;
  46.                 if (isZeroOK)
  47.                         index = (int) Math.floor(Math.random() * 10);
  48.                 else
  49.                         index = 1 + (int) Math.floor(Math.random() * 9);
  50.                 return (digits[index]);
  51.         }
  52.         private static StringBuffer[] digits = { new StringBuffer("0"),
  53.                         new StringBuffer("1"), new StringBuffer("2"),
  54.                         new StringBuffer("3"), new StringBuffer("4"),
  55.                         new StringBuffer("5"), new StringBuffer("6"),
  56.                         new StringBuffer("7"), new StringBuffer("8"), new StringBuffer("9") };
  57.         public static void main(String[] args) {
  58.         }
  59. }
复制代码
RSA.java
  1. public class RSA {
  2.         static BigInteger x, y;
  3.         public static boolean fun(BigInteger x, BigInteger y) {
  4.                 BigInteger t;
  5.                 while (!(y.equals(new BigInteger("0")))) {
  6.                  t = x;
  7.                  x = y;
  8.                  y = t.mod(y);
  9.                 }
  10.                 if (x.equals(new BigInteger("1")))
  11.                 return false;
  12.                 else
  13.                 return true;
  14.         }
  15.         public static BigInteger siyue(BigInteger a, BigInteger b) {
  16.                 BigInteger temp;
  17.                 if (b.equals(new BigInteger("0"))) {
  18.                         x = new BigInteger("1");
  19.                         y = new BigInteger("0");
  20.                         return x;
  21.                 }
  22.                 siyue(b, a.mod(b));
  23.                 temp = x;
  24.                 x = y;
  25.                 y = temp.subtract((a.divide(b).multiply(y)));
  26.                 return x;
  27.         }
  28.         public static double entropy(String mess) {
  29.                 ArrayList<node> jieguo = new ArrayList<node>();
  30.                 jieguo.clear();
  31.                 double num = mess.length();
  32.                 for (int i = 0; i < num; i++) {
  33.                 boolean flag_exit = true;
  34.                 for (int j = 0; j < jieguo.size(); j++) {
  35.                 if (jieguo.get(j).getalpha() == mess.charAt(i)) {
  36.                 flag_exit = false;
  37.                 jieguo.get(j).setp(jieguo.get(j).getp() + 1 / num);
  38.                }
  39.                         }
  40.                         if (flag_exit)
  41.                jieguo.add(new node(1 / num, mess.charAt(i)));
  42.                 }
  43.                 /** 计算熵 */
  44.                 double entropy = 0;
  45.                 for (int i = 0; i < jieguo.size(); i++) {
  46.                         double p1 = jieguo.get(i).getp();
  47.                         entropy += (-p1 * (Math.log(p1) / Math.log(2)));
  48.                 }
  49.                 return entropy;
  50.         }
  51.         public static void main(String[] args) {
  52.                 BigInteger p = null;
  53.                 BigInteger e, d, n, t, c;
  54.                 BigInteger q = null;
  55.                 for (int i = 0; i < 2; i++) {
  56.                         int numDigits;
  57.                         Prime pri = new Prime();
  58.                         try {
  59.                                 numDigits = Integer.parseInt("15");
  60.                         } catch (Exception e1) {
  61.                                 numDigits = 128;
  62.                         }
  63.                         BigInteger start = pri.bigRandom(numDigits);
  64.                         BigInteger big = pri.nextPrime(start);
  65.                         if (i == 0)
  66.                             p = big;
  67.                         else
  68.                             q = big;
  69.                 }
  70.                 Scanner input = new Scanner(System.in);
  71.                 n = p.multiply(q);
  72.                 t = p.subtract(new BigInteger("1")).multiply(
  73.                         q.subtract(new BigInteger("1")));
  74.                 int numDigits;
  75.                 Prime pri = new Prime();
  76.                 try {
  77.                         numDigits = Integer.parseInt("15");
  78.                 } catch (Exception e1) {
  79.                         numDigits = 128;
  80.                 }
  81.                 BigInteger start = pri.bigRandom(numDigits);
  82.                 BigInteger big = pri.nextPrime(start);
  83.                 e = big;
  84.                 while (e.compareTo(t) == 1 || fun(e, t)) {
  85.                         System.out.println("e不合要求,请重新产生" + (e.compareTo(t) == 1)
  86.                         + fun(e, t));
  87.                         pri = new Prime();
  88.                         try {
  89.                         numDigits = Integer.parseInt("15");
  90.                         } catch (Exception e1) {
  91.                         numDigits = 128;
  92.                         }
  93.                         start = pri.bigRandom(numDigits);
  94.                         big = pri.nextPrime(start);
  95.                         e = big;
  96.                 }
  97.                 d = siyue(e, t);
  98.                 System.out.println("随机产生两个素数:"+p+"  "+q);
  99.                 System.out.println("随机产生公钥:"+e);
  100.                 System.out.println("由扩展的欧几里德算法求得私钥:"+d);
  101.                 while (true) {
  102.                         System.out.print("请输入明文:");
  103.                         String mess = input.nextLine();
  104.                         BigInteger m = new BigInteger(mess.getBytes());
  105.                         System.out.println("明文的大整数形式:" + m);
  106.                         /** 调用函数计算信息,统计信息 */
  107.                         mess = m.toString();
  108.                         System.out.println("明文熵:" + entropy(mess));
  109.                         c = m.modPow(e, n);
  110.                         System.out.println("密文的大整数形式:" + c);
  111.                         System.out.println("密文熵:" + entropy(c.toString()));
  112.                         System.out.println("密文的字符串形式:" + new String(c.toByteArray()));
  113.                         m = c.modPow(d, n);
  114.                         System.out.println("解密得到明文的大整数形式:" + c);
  115.                         String str = new String(m.toByteArray());// 把返回的结果还原成一个字串
  116.                         System.out.println("解密得到明文:" + str);
  117.                 }
  118.         }
  119. }
复制代码
三、运行界面

本帖子中包含更多资源

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

x
回复

使用道具 举报

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

本版积分规则

教学服务系统

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

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

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