Sie sind auf Seite 1von 4

import java.io.BufferedReader; import java.io.InputStreamReader; 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

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

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

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

Das könnte Ihnen auch gefallen