java

位置:IT落伍者 >> java >> 浏览文章

RSA算法的实现——java版


发布日期:2019年11月24日
 
RSA算法的实现——java版

package rsa;

import javamathBigInteger;

public class RSA {

private long pqedn;

public RSA(){

int pIndex = (int)(Mathrandom()*);

int qIndex;

int eIndex;

do{

qIndex = (int)(Mathrandom()*);

}

while(qIndex==pIndex);

do{

eIndex = (int)(Mathrandom()*);

}

while(eIndex==pIndex||eIndex==pIndex);

p = ;

q = ;

e =;

n = p*q;

d = calculateD();

}

private long calculateD(){

long t = t = t = ;

long r = (p)*(q) m = rr = e r = ;

do{

long q = r/r;

r = rr*q;

if(r==)break;

t = t t*q;

while(t<){

t+=m;

}

if(t>=m){

t %= m;

}

r = r;

r = r;

t = t;

t = t;

}while(r!=);

if(r!=){

return ;

}

else{

return t;

}

}

public long getE() {

return e;

}

public long getN() {

return n;

}

public long getD() {

return d;

}

public BigInteger encode(BigInteger data){

return pow(datad)mod(new BigInteger(n+));

}

public BigInteger decode(BigInteger code){

return pow(codee)mod(new BigInteger(n+));

}

public BigInteger pow(BigInteger datalong p){

data = datapow((int)p);

return data;

}

public static void main(String args[]){

RSA rsa = new RSA();

BigInteger data = new BigInteger();

long oldtime = SystemcurrentTimeMillis();

BigInteger code = rsaencode(data);

long newtime = SystemcurrentTimeMillis();

double codetime = ((double)(newtimeoldtime))/;

oldtime = SystemcurrentTimeMillis();

BigInteger decode = rsadecode(code);

newtime = SystemcurrentTimeMillis();

double decodetime = ((double)(newtimeoldtime))/;

Systemoutprintln(privateKey:+rsad);

Systemoutprintln(publickKey:+rsae);

Systemoutprintln(N:+rsan);

Systemoutprintln(data:+data);

Systemoutprintln(code:+code+ time:+codetime);

Systemoutprintln(decode:+decode+ time:+decodetime);

}

}

               

上一篇:JavaMail 发送附件的例子

下一篇:如何在java中去掉文本文件中的“”