RSA算法的Java实现
来源:互联网 发布:属于大数据的关键技术 编辑:程序博客网 时间:2024/05/24 04:53
import java.math.BigInteger;
import java.security.SecureRandom;
public class CRSA{
private final static SecureRandom random = new SecureRandom();
private BigInteger SK;
private BigInteger PK;
private BigInteger npq;
CRSA(int N){
SecureRandom rnd = new SecureRandom();
BigInteger p = new BigInteger(N/2,200,rnd);
BigInteger q = new BigInteger(N/2,200,rnd);
BigInteger f =(p.subtract(new BigInteger("1")).multiply(q.subtract(new BigInteger("1"))));
npq =p.multiply(q);
PK =new BigInteger("65537");
SK =PK.modInverse(f);
}
BigInteger encrypt(BigInteger message){
return message.modPow(PK, npq);
}
BigInteger decrypt(BigInteger encrypted){
return encrypted.modPow(SK, npq);
}
public String output(){
String str = "";
str = str + "\n 公钥 :"+ PK +"\n";
str = str + "\n 私钥 :"+ SK +"\n";
return str;
}
public static void main(String[] a){
int N;
byte length[]=new byte[10];
System.out.print("input N:");
try{
System.in.read(length);
}
catch(Exception e){}
String str = new String(length);
N = Integer.parseInt(str.trim());
CRSA key =new CRSA(N);
BigInteger message =new BigInteger(N-1,random);
BigInteger encrypt =key.encrypt(message);
BigInteger decyrpt =key.decrypt(encrypt);
System.out.println(key.output());
System.out.println("the source info: " + message + "\n");
System.out.println("the encrypted info: " + encrypt + "\n");
System.out.println("the decyrpted info: " + decyrpt + "\n");
}
}
import java.security.SecureRandom;
public class CRSA{
private final static SecureRandom random = new SecureRandom();
private BigInteger SK;
private BigInteger PK;
private BigInteger npq;
CRSA(int N){
SecureRandom rnd = new SecureRandom();
BigInteger p = new BigInteger(N/2,200,rnd);
BigInteger q = new BigInteger(N/2,200,rnd);
BigInteger f =(p.subtract(new BigInteger("1")).multiply(q.subtract(new BigInteger("1"))));
npq =p.multiply(q);
PK =new BigInteger("65537");
SK =PK.modInverse(f);
}
BigInteger encrypt(BigInteger message){
return message.modPow(PK, npq);
}
BigInteger decrypt(BigInteger encrypted){
return encrypted.modPow(SK, npq);
}
public String output(){
String str = "";
str = str + "\n 公钥 :"+ PK +"\n";
str = str + "\n 私钥 :"+ SK +"\n";
return str;
}
public static void main(String[] a){
int N;
byte length[]=new byte[10];
System.out.print("input N:");
try{
System.in.read(length);
}
catch(Exception e){}
String str = new String(length);
N = Integer.parseInt(str.trim());
CRSA key =new CRSA(N);
BigInteger message =new BigInteger(N-1,random);
BigInteger encrypt =key.encrypt(message);
BigInteger decyrpt =key.decrypt(encrypt);
System.out.println(key.output());
System.out.println("the source info: " + message + "\n");
System.out.println("the encrypted info: " + encrypt + "\n");
System.out.println("the decyrpted info: " + decyrpt + "\n");
}
}
- RSA算法的Java实现
- RSA算法Java实现
- Java实现RSA算法
- JAVA实现RSA算法
- RSA算法Java实现
- java实现RSA算法
- RSA算法Java实现
- RSA算法Java的简单实现
- RSA算法的实现
- RSA算法的实现
- 用java实现RSA算法
- RSA的java实现
- RSA算法的Java实现及Base64的正确使用
- RSA算法的python实现
- rsa算法的python实现
- RSA算法的简单实现
- RSA算法的实现——java版
- RSA算法的实现方法——Java版放送
- Linux-2.6.32.2内核在mini2440上的移植(十三)---移植UDA1341音频驱动
- Repeater控件的应用
- ubuntu link 命令
- Linux-2.6.32.2内核在mini2440上的移植(十四)---移植看门狗驱动
- TreeViewTest菜单
- RSA算法的Java实现
- Linux find命令用法小结
- Linux-2.6.32.2内核在mini2440上的移植(十五)---移植LED驱动
- SiteMapDataSource
- Linux-2.6.32.2内核在mini2440上的移植(十六)---基于中断的按键驱动移植
- SHP
- .Linux-2.6.32.2内核在mini2440上的移植(十七)---移植PWM控制蜂鸣器驱动
- sitemappath
- Linux-2.6.32.2内核在mini2440上的移植(十八)---将UART2更成普通串口驱动