|

楼主 |
发表于 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
- package src;
- public class node {
- private double p;// 记录概率
- private char a;// 记录对应的字母
- public node(double p, char a) {
- this.p=p;
- this.a=a;
- }
- public void setp(double p) {
- this.p=p;
- }
- public void seta(char a) {
- this.a=a;
- }
- public double getp() {
- return p;
- }
- public char geta() {
- return a;
- }
复制代码 Prime.java
- package src;
- import java.math.BigInteger;
- public class Prime {
- public boolean sushu(double n) {
- boolean isPrime=true;
- if (n>2) {
- if (n%2==0) {
- isPrime=false;
- }
- else {
- for (int i=3;i<=(int) Math.sqrt(n);i+=2) {
- if (n%i==0) {
- isPrime=false;
- break;
- }
- }
- }
-
- }
- return isPrime;
- }
-
- private static final BigInteger ZERO=BigInteger.ZERO;
- private static final BigInteger ONE=BigInteger.ONE;
- private static final BigInteger TWO=new BigInteger("2");
- private static final int ERR_VAL=100;
-
- public BigInteger nextPrime(BigInteger start) {
- if (isEven(start))
- start=start.add(ONE);
- else
- start=start.add(TWO);
- if (start.isProbablePrime(ERR_VAL))
- return (start);
- else
- return (nextPrime(start));
- }
-
- private static boolean isEven(BigInteger n) {
- // 测试一个大整数是否为偶数
- return (n.mod(TWO).equals(ZERO));
- }
- public BigInteger bigRandom(int numDigits) {
- // 产生一个随机大整数,各位上的数字都是随机产生的,首位不为 0
- StringBuffer s = new StringBuffer("");
- for (int i=0;i<numDigits; i++)
- if (i==0)
- s.append(randomDigit(false));
- else
- s.append(randomDigit(true));
- return (new BigInteger(s.toString()));
- }
-
- private StringBuffer randomDigit(boolean isZeroOK) {
- int index;
- if (isZeroOK)
- index=(int)Math.floor(Math.random() * 10);
- else
- index=1+(int)Math.floor(Math.random() * 9);
- return (digits[index]);
- }
- private static StringBuffer[] digits = { new StringBuffer("0"),
- new StringBuffer("1"),new StringBuffer("2"),
- new StringBuffer("3"),new StringBuffer("4"),
- new StringBuffer("5"),new StringBuffer("6"),
- new StringBuffer("7"),new StringBuffer("8"), new StringBuffer("9") };
- public static void main(String[] args) {
- }
- }
复制代码 RSA.java
- package src;
- import java.math.BigInteger;
- import java.util.ArrayList;
- import java.util.Scanner;
-
- public class RSA {
- static BigInteger x,y;
- public static boolean fun(BigInteger x, BigInteger y) {
- BigInteger t;
- while(!(y.equals(new BigInteger("0")))) {
- t=x;
- x=y;
- y=t.mod(y);
- }
- if (x.equals(new BigInteger("1")))
- return false;
- else
- return true;
- }
- public static BigInteger sy(BigInteger a, BigInteger b) {
- BigInteger temp;
- if (b.equals(new BigInteger("0"))) {
- x=new BigInteger("1");
- y=new BigInteger("0");
- return x;
- }
- sy(b, a.mod(b));
- temp = x;
- x = y;
- y = temp.subtract((a.divide(b).multiply(y)));
- return x;
- }
- public static double entropy(String mess) {
- ArrayList<node> result=new ArrayList<node>();
- result.clear();
- double num=mess.length();
- for (int i=0;i<num;i++) {
- boolean flag_exit=true;
- for (int j=0;j<result.size();j++) {
- if (result.get(j).geta()==mess.charAt(i)) {
- flag_exit=false;
- result.get(j).setp(result.get(j).getp()+1/num);
- }
- }
- if (flag_exit)
- result.add(new node(1/num,mess.charAt(i)));
- }
- double entropy=0;
- for (int i=0;i<result.size();i++) {
- double p1=result.get(i).getp();
- entropy +=(-p1*(Math.log(p1)/Math.log(2)));
- }
- return entropy;
- }
-
- public static void main(String[] args) {
- BigInteger p=null;
- BigInteger e,d,n,t,c;
- BigInteger q=null;
- for (int i=0;i<2;i++) {
- int numDigits;
- Prime pri=new Prime();
- try {
- numDigits=Integer.parseInt("15");
- } catch (Exception e1) {
- numDigits=128;
- }
- BigInteger start=pri.bigRandom(numDigits);
- BigInteger big=pri.nextPrime(start);
- if (i==0)
- p=big;
- else
- q=big;
- }
- Scanner input = new Scanner(System.in);
- n=p.multiply(q);
- t=p.subtract(new BigInteger("1")).multiply(
- q.subtract(new BigInteger("1")));
- int numDigits;
- Prime pri=new Prime();
- try {
- numDigits=Integer.parseInt("15");
- } catch (Exception e1) {
- numDigits=128;
- }
- BigInteger start=pri.bigRandom(numDigits);
- BigInteger big=pri.nextPrime(start);
- e = big;
- while (e.compareTo(t)==1||fun(e, t)){
- System.out.println("e不合要求,请重新产生"+(e.compareTo(t)==1)
- + fun(e,t));
- pri=new Prime();
- try{
- numDigits=Integer.parseInt("15");
- } catch (Exception e1) {
- numDigits=128;
- }
- start=pri.bigRandom(numDigits);
- big=pri.nextPrime(start);
- e=big;
- }
- // 求出私钥d
- d=sy(e, t);
- System.out.println("随机产生2个素数p,q:");
- System.out.println(p);
- System.out.println(q);
- System.out.println("计算得n:");
- System.out.println(n);
- System.out.println("计算得t:");
- System.out.println(t);
- System.out.println("随机产生公钥:");
- System.out.println(e);
- System.out.println("求得私钥:");
- System.out.println(d);
- while (true) {
- System.out.println("\n请输入明文:");
- String mess=input.nextLine();
- BigInteger m=new BigInteger(mess.getBytes());// 把字串转成一个BigInteger对象
- System.out.println("明文的大整数表示形式:"+m);
- c=m.modPow(e, n);
- System.out.println("密文的大整数表示形式:"+c);
- m=c.modPow(d, n);
- System.out.println("解密后得到明文的大整数表示形式:"+c);
- String str=new String(m.toByteArray());// 把返回的结果还原成一个字串
- System.out.println("解密后得到明文为:"+str);
- }
- }
- }
复制代码 4.运行截图:
|
本帖子中包含更多资源
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
|