首页 > 代码库 > RSA的作业完成啦~~
RSA的作业完成啦~~
//RSA 基本实现版
import java.io.BufferedReader;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader;import java.math.BigInteger;import java.util.Random;import java.util.Scanner;import org.omg.CORBA.PUBLIC_MEMBER;public class RSAFrame{ private RSA rsa = new RSA(); private BigInteger p, q, e, n, d, ran; private Random rnd = new Random(); //全局变量 private int numBit = 10; public static void main(String[] args) { RSAFrame myrsa=new RSAFrame(); myrsa.beforeMessage(); //计算p,q,n,ran,e,d的值 BigInteger[] encodingM =myrsa.Encryption(); //对输入的信息m进行加密 myrsa.Decryption(encodingM); //对密文进行解密 myrsa.showValue(); //输出p,q,n,ran,e,d的值 } public void beforeMessage() //在发生信息之前,把这些算出来 { p = rsa.getPrimes(rnd); //得到素数p q = rsa.getPrimes(rnd); //得到素数q n = rsa.getN(p, q); //得到p*q ran = rsa.getRan(p, q); //得到(p-1)*(q-1),ran e = rsa.getE(ran); //根据生成的大随机数与ran公约数是否为1,得到了公钥e d = rsa.getKey(e,ran); //根据公钥e,和ran,生成了密钥d System.out.println("请输入随机数的位数(大于9):"); Scanner input=new Scanner(System.in); numBit=input.nextInt(); if(numBit<9) { System.out.println("随机数的位数应该大于9"); } } public BigInteger[] Encryption() { String Message = ""; System.out.println("请输入要发送的消息M(please in English):"); InputStream mym= System.in; BufferedReader rd = new BufferedReader(new InputStreamReader(mym)); try { Message= rd.readLine(); System.out.println("输入的消息为: "+Message); } catch(IOException e) { System.out.println(e.getMessage()); } char[]c = Message.toCharArray(); BigInteger[] deal_message=new BigInteger[c.length]; for (int i = 0;i < c.length;i ++) { // System.out.println(c[i]); //分解成每一位: int a=(int)c[i]; BigInteger bigInteger = BigInteger.valueOf(a); deal_message[i]=bigInteger; //成功赋值 } deal_message=RSA.encodeRSA(deal_message,e,n); //直接在原来基础上修改 return deal_message; } public void Decryption(BigInteger[] encodingM ) { encodingM=RSA.dencodeRSA(encodingM, d, n); //解码后,得到的信息,二维 char[] c=new char[encodingM.length]; for (int i = 0;i < encodingM.length;i ++) { // System.out.println(encodingM[i]); //分解成每一位: int d = encodingM[i].intValue(); c[i]=(char)d; } String message = String.valueOf(c); System.out.println("解密后的信息为: "+message); } private void showValue() { System.out.println(" "); System.out.println("素数p: "+p); System.out.println("素数q: "+q); System.out.println("计算p*q: "+n); System.out.println("计算(p-1)*(q-1): "+ran); System.out.println(" "); System.out.println("公钥e为: "+e); System.out.println("密钥d为: "+d); }}
import java.math.BigInteger;import java.util.Random;public class RSA { private BigInteger primes = BigInteger.ZERO; private BigInteger n, ran, d = BigInteger.ZERO; private BigInteger e = BigInteger.ZERO; private String m; Random rnd = new Random(); private int numBit = 10; public int getNumBit(int n) { numBit = n; return numBit; } public BigInteger getPrimes(Random rnd) { return primes = BigInteger.probablePrime(numBit, rnd); } public BigInteger getN(BigInteger p, BigInteger q) { return n = p.multiply(q); } public BigInteger getRan(BigInteger p, BigInteger q) { /*long r = (p.intValue() - 1)*(q.intValue() - 1); String tr = String.valueOf(r);*/ ran = (p.subtract(BigInteger.ONE)).multiply(q.subtract(BigInteger.ONE)); return ran; } public BigInteger getE(BigInteger ran) { BigInteger temp = null; //中间变量 e = BigInteger.ZERO; do { temp = BigInteger.probablePrime(numBit, rnd); // 随机生成一个素数,看他是否与ran的公约数为1,如果为1,e=temp退出循环 if((temp.gcd(ran)).equals(BigInteger.ONE)) { e = temp; } } while(!((temp.gcd(ran)).equals(BigInteger.ONE))); return e; } public BigInteger getKey(BigInteger e, BigInteger ran) { d = e.modInverse(ran); return d; } public static BigInteger[] encodeRSA(BigInteger[] encodeM , BigInteger e, BigInteger n) { //直接用encodeM表示: for(int i=0; i < encodeM.length; i++) { encodeM[i] = encodeM[i].modPow(e, n); } return encodeM; } public static BigInteger[] dencodeRSA(BigInteger[] encodeM, BigInteger d, BigInteger n) { if (encodeM == null) return null; for (int i = 0; i <encodeM.length; i++) { encodeM[i] = encodeM[i].modPow(d,n); } return encodeM; //直接返回 }}
RSA的作业完成啦~~
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。