您的位置:首页 > 编程语言 > Java开发

用JAVA实现RSA加密解密

2013-09-22 17:55 597 查看
package rsaa;

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 prime(long a)//判断是否是素数

{

long b=0;

b=(long) Math.sqrt((double) a);

int c,i;

c=0;

for(i=2;i<b;i++)

{if(a%i==0)

{

c=1;

break;

}

}

if(c==1)

return false;

else

return true;

}

public void bigprimeRandom() {//产生随机的大素数

do{

p = (long) (Math.random() * 1000000);

}while(!this.prime(p));

do{

q=(long)(Math.random()*1000000);

}while(p==q||!this.prime(q));

}

public void pq()throws Exception{ //P Q的生成

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 gys(long a ,long b) //求最大公约数

{

long c;

if(b==0)

c=a;

else

c=gys(b,a%b);

return c;

}

public void getPublic_key()throws Exception {//生成公钥

do {

this.public_key=(long)(Math.random()*100000);

} while ((this.public_key >= this.m) ||

(this.gys(this.m, this.public_key) != 1));

System.out.println("生成的公钥为:" + this.public_key);

}

public void getPrivate_key() {//得到秘钥

int i;

long a = 1;

for ( i = 1; ; i++) {

a = i * this.m + 1;

if ( a%this.public_key== 0 &&a/this.public_key<this.m) {

this.private_key = a / this.public_key;

break;

}

}

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 jm(long y, long n, long key) {//加密

BigInteger yy=new BigInteger(String.valueOf(y));//将long变成字符型的

BigInteger nn=new BigInteger(String.valueOf(n));

BigInteger kkey=new BigInteger(String.valueOf(key));

return Long.parseLong(yy.modPow(kkey,nn).toString());//modpow()计算y^e mod n的函数

}

public void jmjm() throws Exception { //加密,解密

this.getText();

System.out.println("输入明文为: " + this.text);

ArrayList cestr=new ArrayList(); //动态数组

for (int i = 0; i < text.length(); i++) {//一个字符为一组

this.secretword = this.jm( (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.jm(Long.parseLong(cestr.get(j).toString()), this.n, this.private_key);

destr.append((char)word);//在被选元素的结尾插入指定内容

}

System.out.println("解密后所得的明文为:" +destr);

}

public static void main(String[] args) {

try {

int i;

for(i=0;i<10;i++){

RSA t = new RSA();

t.pq();

t.getPublic_key();

t.getPrivate_key();

t.jmjm();

}

}catch(Exception ex) {

System.out.println(ex.getMessage());

}

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: