RSA implementation (Java)

Source: Internet
Author: User

ImportJava.util.Scanner; Public classRsaencry { Public Static voidMain (string[] args) {//TODO auto-generated Method Stub        LongNumber = 100;//plaintext        Longn;//p*q;        Long[] Thekey =New Long[2]; Scanner input=NewScanner (system.in); System.out.println ("Please input prime number p!"); Longp =Input.nextlong (); System.out.println ("Please input prime number q!"); LongQ =Input.nextlong (); N= p*Q; Key Key=NewKey (p,q,n); Thekey=Key.getkey (); System.out.println ("E:" +thekey[0]+ "D:" +thekey[1 "+" L: "+ (p-1) * (q-1)); Dorsa Test=NewDorsa (thekey[0],thekey[1],n); LongAnswer =test.doencry (number); System.out.println ( number+ "after Encryption" +answer); LongAnswer2 =test.dounencry (answer); System.out.println ("After decryption for" +answer2); }}
 Public classKey {LongP,q,n,l,e,d=0,num=0; Long[] e1 =New Long[1000]; Long[] D1 =New Long[50]; Long[] Thekey =New Long[2];  PublicKey (LongPLongQLongN) {//TODO auto-generated Constructor stub         This. P =p;  This. Q =Q;  This. N =N; L= (p-1) * (q-1); e1[0] = 1;  for(intJ=1,i =4;i<l;i++ ){        //MATH.GCB ();             intFlag =1; if(l%3!=0&&l>3) {e1[1] = 3; J++; }            if(l%2!=0&&l>2) {e1[1] = 3; J++; }             for(intX=2;X&LT;=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(inti = 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        returnThekey; }}
 Public classDorsa {LongNumber ; LongE,d,n; //private long answer;     PublicDorsa (LongELongDLongN) { This. E =e;  This, db=D;  This. N =N; //Number = num;    }             Public LongDoencry (Longc) {//Long A (c), Long K (e), long M (n)        LongAnswer=1;  while(e>=1){             if(e%2==1) {Answer=c*answer%N; } C= (c%n) * (c%n)%N; E/=2; }          returnanswer; }         Public LongDounencry (Longm) {//Long A (c), Long K (e), long M (n)        LongAnswer=1;  while(d>=1){             if(d%2==1) {Answer=m*answer%N; } m= (m%n) * (m%n)%N; D/=2; }          returnanswer; }}

RSA implementation (Java)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.