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;
        }
    }
  • 相关阅读:
    我们可以用SharePoint做什么
    HTML <!DOCTYPE> 标签
    一种支持任意尺寸的图片滑动(上下左右滑动)效果
    CSS选择器
    用css截取字符 css排版隐藏溢出文本
    Web前端行业的了解
    java07课堂作业
    设计模式原型模式
    设计模式建造者
    设计模式抽象工厂
  • 原文地址:https://www.cnblogs.com/weiliuyby/p/8666666.html
Copyright © 2011-2022 走看看