RSA实现(java)

import java.util.Scanner;

public class RsaEncry {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        long number = 100;//明文
        long n ;             //p*q;
        long[] theKey = new long[2];
        Scanner input = new Scanner(System.in);
        System.out.println("please input prime number p!");
        long  p = input.nextLong();
        System.out.println("please input prime number q!");
        long  q = input.nextLong();
        n = p*q;
        Key key = new Key(p,q,n); 
        theKey = key.getKey(); 
        System.out.println("e:"+theKey[0]+"  d: "+theKey[1] + "  l: "+(p-1)*(q-1));
        doRsa  test  = new doRsa(theKey[0],theKey[1],n);
        long answer = test.doEncry(number);
        System.out.println(number+"加密后为"+answer);
        long answer2 = test.doUnEncry(answer);
        System.out.println("解密后为"+answer2);
        
    }

}
public class Key {
    long p,q,n,l,e,d=0,num=0;
    long[] e1 = new long[1000];
    long[] d1 = new long[50];
    long[] theKey = new long[2]; 
    public Key(long p, long q, long n) {
        // TODO Auto-generated constructor stub
        this.p = p;
        this.q = q;
        this.n = n;
        l = (p-1)*(q-1);
        e1[0] = 1;
        for(int j=1,i =4;i<l;i++ ){
        //Math.gcb();    
            int flag =1;
            if(l%3!=0&&l>3){
                e1[1] = 3;
                j++;
            }
            if(l%2!=0&&l>2){
                e1[1] = 3;
                j++;
            }
            for(int x=2;x<=Math.sqrt(l)&&x<=Math.sqrt(i);x++){
                if(l%i==0){
                    flag = 0;
                    break;
                }
                if(l%x==0&&i%x==0){
                    flag = 0;
                    break;
                }        
            }
            if(flag==1){
                e1[j] = i;
                num++;
                j++;
            }
            flag = 1;
        }
        e = e1[(int)(Math.random()*num)];
        theKey[0] = e;
        
        for(int i = 1;i<2*e;i++)
        {
            if((i*l+1)%e==0)
            {
                d=  (i*l+1)/e;
            }
        }
        
        theKey[1] = d;
        
    }
    public long[] getKey() {
        // TODO Auto-generated method stub
        return theKey;
    }

}
public class doRsa {
    long  number;
    long e,d,n;
    //private long answer;
    public doRsa(long e,long d,long n)
    {
        this.e = e;
        this.d = d; 
        this.n = n;
        //number = num;
    }
    
    
    public long doEncry(long c)
    {
         // long a(c),long k(e),long M(n)  
        long answer=1;  
        while(e>=1){  
           if(e%2==1){  
               answer=c*answer%n;  
           }  
           c=(c%n)*(c%n)%n;  
           e/=2;  
        }  
        return  answer;
    }
    
    public long doUnEncry(long m)
    {
         // long a(c),long k(e),long M(n)  
        long answer=1;  
        while(d>=1){  
           if(d%2==1){  
               answer=m*answer%n;  
           }  
           m=(m%n)*(m%n)%n;  
           d/=2;  
        }  
        return  answer;
    }
}

 

posted @ 2018-03-28 22:01  little——boy  阅读(211)  评论(0编辑  收藏  举报