//package algorithm.rsa; import java.io.BufferedReader; import java.io.IOException; import java.io.

InputStreamReader; public class RSAAlgo { static int calcGCD(int no1,int no2){ if (no2==0) return no1; return calcGCD(no2,no1%no2); } static int calcEncrypt(int funN){ int j=0,gcdarr[]; gcdarr= new int[10]; for(int i=2;i<funN;i++){ if(calcGCD(funN,i)==1){ gcdarr[j++]=i; } } int x=((int)(Math.random()*10))%j; return gcdarr[x]; } static int calcDecrypt(int encrypt,int funN){ for(int i=0;i<funN;i++){ if((encrypt*i)%funN==1){ return i; } } return -1; } static int getPrimeNumber() { int num=-1,i; System.out.println("\nEnter Prime no : "); BufferedReader br= new BufferedReader(new InputStreamReader(Syst em.in) ); try { num=Integer.parseInt(br.readLine()); } catch (NumberFormatException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } for (i=2; i < num ;i++ ){ int n = num%i; if (n==0){ return -1; } } if(i == num){ return i; } return -1; } static void display(int no1,int no2,int prodN,int funN,int encrypt,int d ecrypt,int plaintext,int ciphertext,int verifyPlainText){

System.out. display(no1.funN). System.println("\nfunN:"+funN).ciphertext = 0.in) ). no2=getPrimeNumber(). if(no1==-1 no2==-1 no1==no2){ System.System. System.out. System.println("Entered Number are not Prime or are Same..out.funN. } ciphertext=(int)(Math.. System.exit(0).no2.!!"). System.(float)decrypt) )%prodN. } catch (NumberFormatException e) { e.out. encrypt=calcEncrypt(funN). } catch (IOException e) { e.println("\nVerifyPlainText:"+verifyPlainText).println("\ndecrypt:"+decrypt). } .out. System.decrypt.parseInt(br.funN.println("\nno1:"+no1). verifyPlainText).encrypt.ciphertext. no1=getPrimeNumber(). System.prodN.println("\nciphertext:"+ciphertext).out. try { plaintext=Integer. funN=(no1-1)*(no2-1). } public static void main(String[] args) { int no1.(float)encrypt))%prod N.printStackTrace(). } decrypt=calcDecrypt(encrypt.out.readLine()).out. verifyPlainText=(int)(Math.plaintext = 0. if(encrypt==0){ System.!!").decrypt..out. } prodN=no1*no2.println("\nprodN:"+prodN).encrypt.println("\nencrypt:"+encrypt). System.pow((float)plaintext.plaintext.pow((float)ciphertext..verifyPlainText.println("\nEnter Plain Text no : ").no2.println("\nplaintext:"+plaintext).println("\nno2:"+no2).out.out.exit(0).println("Difference between two Prime Numbers should be more.out. System. System.prodN. BufferedReader br= new BufferedReader(new InputStreamReader(Syst em.printStackTrace().

} .

Sign up to vote on this title
UsefulNot useful