教学服务系统

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

信息计算2019级1班29号敖若瑶

[复制链接]

8

主题

18

帖子

124

积分

注册会员

Rank: 2

积分
124
发表于 2022-4-20 18:15:10 | 显示全部楼层 |阅读模式
0419mooc学习

本帖子中包含更多资源

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

x
回复

使用道具 举报

8

主题

18

帖子

124

积分

注册会员

Rank: 2

积分
124
 楼主| 发表于 2022-4-22 15:20:26 | 显示全部楼层
4.22mooc学习

本帖子中包含更多资源

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

x
回复

使用道具 举报

8

主题

18

帖子

124

积分

注册会员

Rank: 2

积分
124
 楼主| 发表于 2022-4-29 19:07:07 | 显示全部楼层
4月29日mooc学习



本帖子中包含更多资源

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

x
回复

使用道具 举报

8

主题

18

帖子

124

积分

注册会员

Rank: 2

积分
124
 楼主| 发表于 2022-5-2 20:25:23 | 显示全部楼层
本帖最后由 信息计算敖若瑶 于 2022-5-8 22:02 编辑

RSA大数版本
1.环境:java
2.原理:RSA算法,随机选择两个大素数p、q,计算n=p*q,计算t=(p-1)*(q-1),然后在区间(0~n)之间取一个与t互质的数-公钥e,即GCD(e,t)=1,然后通过欧几里得算法计算得到私钥d,得(e*d)%t=1,再对明文进行加密与解密。
3.源代码:node.java

  1. package src;

  2. public class node {
  3.         private double p;// 记录概率
  4.         private char a;// 记录对应的字母
  5.         public node(double p, char a) {
  6.                 this.p=p;
  7.                 this.a=a;
  8.         }
  9.         public void setp(double p) {
  10.                 this.p=p;
  11.         }
  12.         public void seta(char a) {
  13.                 this.a=a;
  14.         }
  15.         public double getp() {
  16.                 return p;
  17.         }
  18.         public char geta() {
  19.                 return a;
  20.         }
复制代码
Prime.java
  1. package src;
  2. import java.math.BigInteger;

  3. public class Prime {
  4.         public boolean sushu(double n) {
  5.                 boolean isPrime=true;
  6.                 if (n>2) {
  7.                         if (n%2==0) {
  8.                                 isPrime=false;
  9.                         }
  10.                         else {
  11.                                 for (int i=3;i<=(int) Math.sqrt(n);i+=2) {
  12.                                         if (n%i==0) {
  13.                                                 isPrime=false;
  14.                                                 break;
  15.                                         }
  16.                                 }
  17.                         }

  18.                 }
  19.                 return isPrime;
  20.         }

  21.         private static final BigInteger ZERO=BigInteger.ZERO;
  22.         private static final BigInteger ONE=BigInteger.ONE;
  23.         private static final BigInteger TWO=new BigInteger("2");
  24.         private static final int ERR_VAL=100;

  25.         public BigInteger nextPrime(BigInteger start) {
  26.                 if (isEven(start))
  27.                         start=start.add(ONE);
  28.                 else
  29.                         start=start.add(TWO);
  30.                 if (start.isProbablePrime(ERR_VAL))
  31.                         return (start);
  32.                 else
  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.                 int index;
  51.                 if (isZeroOK)
  52.                         index=(int)Math.floor(Math.random() * 10);
  53.                 else
  54.                         index=1+(int)Math.floor(Math.random() * 9);
  55.                 return (digits[index]);
  56.         }
  57.         private static StringBuffer[] digits = { new StringBuffer("0"),
  58.                         new StringBuffer("1"),new StringBuffer("2"),
  59.                         new StringBuffer("3"),new StringBuffer("4"),
  60.                         new StringBuffer("5"),new StringBuffer("6"),
  61.                         new StringBuffer("7"),new StringBuffer("8"), new StringBuffer("9") };
  62.         public static void main(String[] args) {
  63.         }
  64. }
复制代码
RSA.java
  1. package src;
  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 sy(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.                 sy(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> result=new ArrayList<node>();
  34.                 result.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<result.size();j++) {
  39.                                 if (result.get(j).geta()==mess.charAt(i)) {
  40.                                         flag_exit=false;
  41.                                         result.get(j).setp(result.get(j).getp()+1/num);
  42.                                 }
  43.                         }
  44.                         if (flag_exit)
  45.                                 result.add(new node(1/num,mess.charAt(i)));
  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=sy(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("\n请输入明文:");
  115.                         String mess=input.nextLine();
  116.                         BigInteger m=new BigInteger(mess.getBytes());// 把字串转成一个BigInteger对象
  117.                         System.out.println("明文的大整数表示形式:"+m);
  118.                         c=m.modPow(e, n);
  119.                         System.out.println("密文的大整数表示形式:"+c);
  120.                         m=c.modPow(d, n);
  121.                         System.out.println("解密后得到明文的大整数表示形式:"+c);
  122.                         String str=new String(m.toByteArray());// 把返回的结果还原成一个字串
  123.                         System.out.println("解密后得到明文为:"+str);
  124.                 }
  125.         }
  126. }
复制代码
4.运行截图:





本帖子中包含更多资源

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

x
回复

使用道具 举报

8

主题

18

帖子

124

积分

注册会员

Rank: 2

积分
124
 楼主| 发表于 2022-5-3 14:24:29 | 显示全部楼层
5月3日mooc学习

本帖子中包含更多资源

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

x
回复

使用道具 举报

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

本版积分规则

教学服务系统

GMT+8, 2025-4-30 12:28 , Processed in 0.017024 second(s), 19 queries .

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

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