rsa算法实现:RSA算法的实现思路方法——Java版放送

开始不知道有BigInteger这个大数类居然自己去实现了写了大数加法后才发现有现成T_T

以下是引用片段:
package rsa;
import java.math.BigInteger;

public RSA {
 private long p,q,e,d,n;
 public RSA{
  pIndex = ()(Math.random*10);
  qIndex;
  eIndex;
  do{
   qIndex = ()(Math.random*10);
  }
  while(qIndexpIndex);
  do{
   eIndex = ()(Math.random*10);
  }
  while(eIndexpIndex||eIndexpIndex);
  p = 1033;
  q = 2017;
  e = 29437;
  n = p*q;
  d = calculateD;
 }
 private long calculateD{
  long t0 = 0,t1 = 1,t2 = -1;
  long r0 = (p-1)*(q-1), m = r0,r1 = e ,r2 = -1;
  do{
   long q = r0/r1;
   r2 = r0-r1*q;
   (r20);
   t2 = t0 - t1*q;
   while(t2<0){
    t2m;
   }
   (t2>=m){
    t2 %= m;
   }    
   r0 = r1;
   r1 = r2;
   t0 = t1;
   t1 = t2;
  }while(r2!=0);
  (r1!=1){
   0;
  }
  {
   t2;
  }
 }
 
 public long getE {
  e;
 }
 public long getN {
  n;
 }
 public long getD {
  d;
 }
 public BigInteger encode(BigInteger data){
  pow(data,d).mod( BigInteger(n+""));
 }
 public BigInteger decode(BigInteger code){
  pow(code,e).mod( BigInteger(n+""));
 }
 public BigInteger pow(BigInteger data,long p){
  data = data.pow(()p);
  data;
 }
 public void (String args){
  RSA rsa = RSA;
  
  BigInteger data = BigInteger("222222");
  long oldtime = .currentTimeMillis;
  BigInteger code = rsa.encode(data);
  long time = .currentTimeMillis;
  double codetime = ((double)(time-oldtime))/1000;
  oldtime = .currentTimeMillis;
  BigInteger decode = rsa.decode(code);
  time = .currentTimeMillis;
  double decodetime = ((double)(time-oldtime))/1000;
  .out.prln("privateKey:"+rsa.d);
  .out.prln("publickKey:"+rsa.e);
  .out.prln("N:"+rsa.n);
  .out.prln("data:"+data);
  .out.prln("code:"+code+" time:"+codetime);
  .out.prln("decode:"+decode+" time:"+decodetime);

 }

}

Tags:  rsa算法的原理 javarsa加密算法 javarsa算法 rsa算法实现

延伸阅读

最新评论

发表评论