📄 rsa.java
字号:
/**
* <p>Title:RSA算法 </p>
*
* <p>Description:简单实现RSA算法、以及利用RSA算法实现加密、解密、签名、验证 </p>
*
* <p>Copyright: Copyright (c) 2008/5/27</p>
*
* <p>Company: 建工学院</p>
*
* @author 04信息(1)程晟
* @version 1.0
*/
import java.io.*;
import java.math.BigInteger;
import java.util.ArrayList;
public class RSA {
private long p = 0;
private long q = 0;
private long n = 0;
private long m = 0;
private long public_key = 0; //公匙
private long private_key = 0; //密匙
private String text ; //明文
private long secretword = 0; //密文
private long word = 0; //解密后明文
//判断是否为素数
public boolean primenumber(long t) {
long k = 0;
k = (long) Math.sqrt((double) t);
boolean flag = true;
outer:for (int i = 2; i <= k; i++) {
if ((t % i) == 0) {
flag = false;
break outer;
}
}
return flag;
}
//随机产生大素数(1e6数量级,注意,太大了要超出范围)
public void bigprimeRandom() {
do{
p = (long) (Math.random() * 1000000);
}while(!this.primenumber(p));
do{
q=(long)(Math.random()*1000000);
}while(p==q||!this.primenumber(q));
}
//输入PQ
public void inputPQ() throws Exception {
this.bigprimeRandom();
System.out.println("自动生成两个大素数p,q分别为:" + this.p+" "+this.q);
this.n = (long)p*q;
this.m = (long)(p - 1) * (q - 1);
System.out.println("这两个素数的乘积为p*q:" + this.n);
System.out.println("所得的m=(p-1)(q-1):" + this.m);
}
//求最大公约数
public long gcd(long a, long b) {
long gcd;
if (b == 0)
gcd = a;
else
gcd = gcd(b, a % b);
//System.out.println("gcd:" + gcd);
return gcd;
}
//生成公匙
public void getPublic_key() throws Exception {
do {
this.public_key=(long)(Math.random()*100000);
} while ((this.public_key >= this.m) ||
(this.gcd(this.m, this.public_key) != 1));
System.out.println("生成的公钥为:" + this.public_key);
}
//计算得到密匙
public void getPrivate_key() {
long value = 1;
outer:for (long i = 1; ; i++) {
value = i * this.m + 1;
//System.out.println("value: " + value);
if ((value % this.public_key == 0) &&
(value / this.public_key < this.m)) {
this.private_key = value / this.public_key;
break outer;
}
}
System.out.println("产生的一个私钥为:" + this.private_key);
}
//输入明文
public void getText() throws Exception
{
System.out.println("请输入明文:");
BufferedReader stdin = new BufferedReader(new
InputStreamReader(System.in));
text = stdin.readLine();
}
//加密、解密计算
public long colum(long y, long n, long key) {
BigInteger bigy=new BigInteger(String.valueOf(y));
//System.out.println(bigy);
BigInteger bign=new BigInteger(String.valueOf(n));
BigInteger bigkey=new BigInteger(String.valueOf(key));
//System.out.println(Long.parseLong(bigy.modPow(bigkey,bign).toString()));
return Long.parseLong(bigy.modPow(bigkey,bign).toString());
}
//加密后解密
public void pascolum() throws Exception {
this.getText();
System.out.println("输入明文为: " + this.text);
//加密
ArrayList cestr=new ArrayList();
for (int i = 0; i < text.length(); i++) {
this.secretword = this.colum( (long)text.charAt(i), this.n,
this.public_key);
cestr.add(secretword);
}
System.out.println("加密后所得的密文为:" +cestr);
//解密
StringBuffer destr=new StringBuffer();
for(int j=0;j<cestr.size();j++) {
this.word = this.colum(Long.parseLong(cestr.get(j).toString()), this.n, this.private_key);
destr.append((char)word);
}
System.out.println("解密后所得的明文为:" +destr);
}
//签名后验证
public void signautre() throws Exception {
System.out.println("输入明文为: " + this.text);
//签名
ArrayList cestr=new ArrayList();
for (int i = 0; i < text.length(); i++) {
this.secretword = this.colum( (long)text.charAt(i), this.n,
this.private_key);
cestr.add(secretword);
}
System.out.println("签名后所得的密文为:" +cestr);
//验证
StringBuffer destr=new StringBuffer();
for(int j=0;j<cestr.size();j++) {
this.word = this.colum(Long.parseLong(cestr.get(j).toString()), this.n, this.public_key);
destr.append((char)word);
}
System.out.println("解密后所得的明文为:" +destr);
if(destr.toString().equals(text)) {
System.out.println("签名有效");
}else {
System.out.println("签名无效");
}
}
public static void main(String[] args) {
try {
RSA t = new RSA();
t.inputPQ();
t.getPublic_key();
t.getPrivate_key();
t.pascolum();
t.signautre();
}catch(Exception ex) {
System.out.println(ex.getMessage());
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -