教学服务系统

 找回密码
 立即注册
搜索
查看: 524|回复: 3

信息计算2019级2班26号 何祥宝

[复制链接]

8

主题

17

帖子

76

积分

注册会员

Rank: 2

积分
76
发表于 2022-4-19 19:37:21 | 显示全部楼层 |阅读模式

本帖子中包含更多资源

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

x
回复

使用道具 举报

8

主题

17

帖子

76

积分

注册会员

Rank: 2

积分
76
 楼主| 发表于 2022-4-29 21:34:57 | 显示全部楼层

本帖子中包含更多资源

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

x
回复

使用道具 举报

8

主题

17

帖子

76

积分

注册会员

Rank: 2

积分
76
 楼主| 发表于 2022-5-3 11:29:54 | 显示全部楼层

本帖子中包含更多资源

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

x
回复

使用道具 举报

8

主题

17

帖子

76

积分

注册会员

Rank: 2

积分
76
 楼主| 发表于 2022-5-3 11:30:28 | 显示全部楼层
  1. package text;
  2. import java.nio.charset.Charset;
  3. import java.math.BigInteger;
  4. import java.util.Random;
  5. import java.util.Scanner;
  6. public class RAs {
  7.         static BigInteger p,q,e,q1,p1,p2,q2,e1,m;
  8.         static BigInteger nn = new BigInteger("1");
  9.         static BigInteger mm = new BigInteger("0");
  10.         public BigInteger  run(){
  11.                 Random rnd=new Random();
  12.                 BigInteger a = new BigInteger(500,rnd);
  13.                 int s=(int)Math.pow(2,20);
  14.                 String pp=String.valueOf(s);
  15.                 BigInteger t= new BigInteger(pp);
  16.                 BigInteger temp=a.add(t);
  17.                 testsu(temp);
  18.                 if(testsu(temp)==true){}
  19.                 p1=temp;
  20.                 p2=p1.nextProbablePrime();
  21.                 p=p2;
  22.                 return p;
  23.         }
  24.         public BigInteger   run1(){
  25.                 Random rnd=new Random();

  26.                 BigInteger a = new BigInteger(500,rnd);
  27.                 int s=(int)Math.pow(2,20);
  28.                 String pp=String.valueOf(s);
  29.                 BigInteger t= new BigInteger(pp);
  30.                 BigInteger temp=a.add(t);
  31.                 testsu(temp);
  32.                 if(testsu(temp)==true){}
  33.                 q1=temp;
  34.                 q2=q1.nextProbablePrime();
  35.                 q=q2;
  36.                 return q;
  37.         }
  38.         private boolean testsu(BigInteger temp) {
  39.                 for(int k=2; k<Math.pow(2,20);k++){
  40.                         int k1=k;
  41.                         String pp=String.valueOf(k1);
  42.                         BigInteger t= new BigInteger(pp);
  43.                         BigInteger[] m =temp.divideAndRemainder(t);
  44.                         BigInteger mm = new BigInteger("0");
  45.                         if(m[1].equals(mm)){return false;}

  46.                 }
  47.                 return true;
  48.         }
  49.         public boolean gcd(BigInteger k,BigInteger p){
  50.                 if(k.compareTo(p)>0){
  51.                         while(k.remainder(p)==nn){
  52.                                 if(k.remainder(p)==mm&&p!=nn){
  53.                                         return false;
  54.                                 }
  55.                                 else{
  56.                                         BigInteger ll=p;
  57.                                         p=k.remainder(p);
  58.                                         k=ll;
  59.                                 }
  60.                         }
  61.                 }
  62.                 else{BigInteger r=k;
  63.                 k=p;
  64.                 p=r;}
  65.                 return true;
  66.         }
  67.         public BigInteger ggcdeee(){
  68.                 int v=0;
  69.                 Random snd=new Random();
  70.                 while(v==0){
  71.                         BigInteger t=new BigInteger(400, snd);
  72.                         BigInteger tt=t.gcd((p.subtract(nn)).multiply(q.subtract(nn)));
  73.                         if(tt.equals(nn)){
  74.                                 e1=t;
  75.                                 e=e1;
  76.                                 v++;
  77.                         }
  78.                 }
  79.                 return e;
  80.         }
  81.         public BigInteger lock(BigInteger e1,BigInteger n1,BigInteger d1,BigInteger m1){

  82.                 BigInteger c=m1.modPow(e1, n1);
  83.                 return c;
  84.         }
  85.         public  BigInteger unlock(BigInteger c2,BigInteger d2,BigInteger n2){
  86.                 m=c2.modPow(d2,n2);
  87.                 return m;
  88.         }
  89.         public static BigInteger encodeString(String str){
  90.                 if(str.length()>4){               
  91.                         return BigInteger.ONE;               
  92.                 }
  93.                 if(str.length()<4){
  94.                         str=str+(4-str.length())*' ';
  95.                 }
  96.                 BigInteger a[] =new BigInteger[4];
  97.                 for(int i=0;i<4;i++){
  98.                         a[i]=BigInteger.valueOf(str.codePointAt(i));
  99.                 }
  100.                 return a[3].xor(a[2].shiftLeft(16)).xor(a[1].shiftLeft(32)).xor(a[0].shiftLeft(48));

  101.         }
  102.         public static String decodeBiginteger(BigInteger b){
  103.                 long a[] =new long[4];
  104.                 String result="";
  105.                 for(int i=3;i>=0;i--){
  106.                         a[i]=b.shiftLeft(i*16).shiftRight(48).longValue();
  107.                         result=(char)(a[i])+result;
  108.                 }
  109.                 return result;
  110.         }
  111.         public static void main(String[] args){
  112.     Scanner t=new Scanner(System.in);
  113.                 while(true){
  114.                         RAs s=new RAs();
  115.                         s.run();
  116.                         s.run1();
  117.                         s.ggcdeee();
  118.                         System.out.println("素数p="+s.run());
  119.                         System.out.println("素数q="+s.run1());
  120.                         BigInteger n=p.multiply(q);
  121.                         System.out.println("公共数模n="+n);
  122.                         BigInteger ss=(p.subtract(nn)).multiply(q.subtract(nn));
  123.                         System.out.println("欧拉函数φ(n)="+ss);
  124.                         System.out.println("公钥e="+e);
  125.                         BigInteger d=s.ggcdeee().modInverse(ss);
  126.                         System.out.println("私钥d="+d);
  127.                         System.out.println("请输入解密数据");
  128.                         String m=t.next();
  129.                         String str=m;
  130.                         BigInteger string1= encodeString(str);
  131.                         byte[] bs = str.getBytes();
  132.                         System.out.println("转换的中间代码="+encodeString(str));
  133.                         BigInteger c1=s.lock(e, n,d, string1);
  134.                         System.out.println("加密后="+c1);
  135.                         System.out.println("转换的中间代码="+s.unlock(c1, d, n));
  136.           System.out.println("解密后的文件时: "+decodeBiginteger(s.unlock(c1, d, n)));
  137.                 }
  138.         }
  139. }

复制代码

本帖子中包含更多资源

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

x
回复

使用道具 举报

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

本版积分规则

教学服务系统

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

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

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