You are on page 1of 4

import; import; import java.math.

BigInteger; class rsa { BigInteger P,Q,p,q,n,phi,e[]; BigInteger PT,CT,E,D,rPT; BigInteger i,j; int flag; int k; void initialize(BigInteger a,BigInteger b,BigInteger msg) { p=a; q=b; PT=msg; System.out.println("\np:: "+p+"\tq:: "+q+"\tPT:: "+PT); begin(); } void begin() { BigInteger r1=BigInteger.ONE; n=p.multiply(q); System.out.println("N::"+n); P=p.subtract(r1); Q=q.subtract(r1); phi=P.multiply(Q); System.out.println("PHI::"+phi); getkeys(); } void getkeys() { System.out.println("In getkeys now."); k=0; flag=0; i=(BigInteger.ONE).add(BigInteger.ONE); int k=i.compareTo(phi); while(k==-1) { System.out.println("\ni:: "+i); BigInteger a=phi.mod(i); int b=a.compareTo(BigInteger.ZERO); if(b==-1 b==1) { System.out.println("\n"+phi+"%"+i+"!=0") ; j=(BigInteger.ONE).add(BigInteger.ONE); int t=j.compareTo(i); while(t==-1) { System.out.println("J:: "+j); BigInteger s=i.mod(j); BigInteger s1=phi.mod(j); int x=s.compareTo(BigInteger.ZER O); int y=s1.compareTo(BigInteger.ZE

compareTo(phi). System. while(p==-1) { BigInteger d=E. System.println("\nj= "+j).out.out. i=PT.println("\ne[k]="+e[k ]).mod(n).pow(E.out. i=(BigInteger. encrypt(). } } /*if(flag==0) E=i.mod(phi).ONE).out.println("At the sender's site::\n"). }*/ if(flag==0) { E=i.out.add(BigInteger.compareTo(BigInteger. .out.out. break.println("\nEncryption Key:: "+E).ONE).subtract(BigInteger.RO).multiply(i). BigInteger d1=d.println("Plain Text:: "+PT+"\nEncryption Key: : "+E+"\nCipher Text:: "+CT).println("\nEn key="+i +"\nk:"+k).ZERO). k++.ONE).println("\nDecryption Key:: " +D). decrypt().intValue()). } } } void encrypt() { i=BigInteger. System. BigInteger d2=d1. } else i. } } /*if(flag==0) { System. e[k]=i. if(x==0 && y==0) { System.out.ZERO. break. System. int g=d2. flag=1. CT=i. int p=i.add(BigInteger. if(g==0) { D=i. break.ONE). System.*/ System.println("\nk:: "+k).

out.ONE).b. if(PT==rPT) System. b=new BigInteger(bcd).} void decrypt() { i=BigInteger.ZERO). System. int j1=j. while(j1==-1) { BigInteger b2=b.println("At the Receiver's site:: \n").ONE).compareTo(BigInteger.ZERO.msg.mod(i). else System. int System.mod(j). int a3=a2.ZERO).println("\nSuccess!! :))"). BigInteger j=(BigInteger.pow(D. int i1=i.out.compareTo(BigInteger.readLine().compareTo(BigInteger. if(b3==0) .out. rPT=i. try { BufferedReader br=new BufferedReader(new InputStreamRead er(System. break.ONE).add(BigInteger. String bcd=br. if(a1==0) flag1=1.add(BigInteger. i=CT. BigInteger i=(BigInteger.mod(n).println("Received Cipher Text:: "+CT+"\nDecry ption Key:: "+D+"\nDecrypted Cipher Text:: "+rPT).out.readLine().compareTo(a).:: "). while(i1==-1) { BigInteger a2=a.compareTo(BigInteger.compareTo(b). } } public class rsa_algo { public static void main(String[] args) { rsa r=new rsa().:: ").ONE).println("Enter another prime no. } } System. int a1=a.println("\nFail!! :((").ONE). String abc=br. int flag1=0. int b1=b. if(a3==0) { flag1=1. System.out.flag2=0.println("Enter a prime no. BigInteger a.intValue()).out. a=new BigInteger(abc). if(b1==0) flag2=1.ZERO).

println("\na: "+a+"\nb: "+b+"\nMSG:: "+msg). break. r.out.out.println("The number(s) entered are no t prime nos. System.out.println(":P"). System. } } catch(Exception e) { } } } .readLine().!").out. msg).initialize(a. } else { System. String def=br. b.println("Enter the message: "). msg=new BigInteger(def). } } if(flag1==0 && flag2==0) { System.{ flag2=1.