⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 rsa.java

📁 使用BigInteger类实现,实现了RSA的加解密
💻 JAVA
字号:
import java.math.*;
public class RSA {

	//BigDecimal
	private final static BigInteger p = BigInteger.valueOf(17l);
	private final static BigInteger q = BigInteger.valueOf(11l);
	private BigInteger e ;
	private BigInteger d = null;
	private BigInteger n = null;
	private BigInteger n_1=null;
	
	RSA(long e){
		this.e=BigInteger.valueOf(e);
		this.birth_n();
		this.birth_n_1();
		this.birth_d();
	}
	//计算n=pq
	private void birth_n(){
		this.n=this.p.multiply(this.q);
	}
	//计算n_1=(p-1)(q-1)
	private void birth_n_1(){
		this.n_1=this.p.subtract(BigInteger.ONE).multiply(this.q.subtract(BigInteger.ONE));
	}
	
	//计算d
	private void birth_d(){
		//this.d.multiply(this.e)\
		/*long i=1;
		BigInteger temp=this.n_1.multiply(BigInteger.valueOf(i)).add(BigInteger.ONE);
		
		while(temp.mod(this.e).compareTo(BigInteger.ZERO)!=0){
			i++;
		}
		this.d=temp.divide(this.e);*/
		//System.out.println(this.d);
		this.d = this.e.modInverse(this.n_1);
	}
	//加密
	public BigInteger public_rsa(long m){
		return BigInteger.valueOf(m).pow(this.e.intValue()).remainder(this.n);
	}
	//解密
	public BigInteger private_rsa(long c){
		return BigInteger.valueOf(c).pow(this.d.intValue()).remainder(this.n);
	}
	
	//获取公钥
	public BigInteger getE() {
		return e;
	}
	//设置公钥
	public void setE(BigInteger e) {
		this.e = e;
	}
	//获取私钥
	public BigInteger getD() {
		return d;
	}
	public static void main(String[] args) {
		RSA r = new RSA(7);
		System.out.println(r.public_rsa(99));
		RSA s = new RSA(3);
		System.out.println(s.getD());
		System.out.println(r.getD());
	}
	
	

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -