zoukankan      html  css  js  c++  java
  • 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;
        }
    }
  • 相关阅读:
    java课后作业-4
    课堂练习
    16年9月27日上午
    《大道至简》第二章读后感
    《大道至简》-编程的精义读后感
    用Windbg来看看CLR的JIT是什么时候发生的
    如何调试什么时候SaveFileDialog会被Dispose
    针对C#程序做性能测试的一些基本准则
    要注意null合并运算符的优先级比+还要低
    实现GetHashCode时要遵循的规则
  • 原文地址:https://www.cnblogs.com/weiliuyby/p/8666666.html
Copyright © 2011-2022 走看看