123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081 |
- /*
- Name: Ajinkya Arun Gurav
- Div : H Roll No:21
- GR No : 121237
- Subject : Network Security Lab
- Lab 5: Implement encryption and decryption using RSA Algorithm.
- */
- import java.math.BigInteger;
- import java.util.*;
- public class RSA {
- public static void main(String[] args) {
- // TODO code application logic here
- BigInteger p,q,phin,n,m,c,e,l,d=BigInteger.ONE;
- Integer k=0,y=0,res=0,res1=0;
- Scanner sc=new Scanner(System.in);
- System.out.println("Enter p");
- p=sc.nextBigInteger();
- System.out.println("Enter q");
- q=sc.nextBigInteger();
- n=BigInteger.valueOf(p.intValue()*q.intValue());
- phin=BigInteger.valueOf((p.intValue()-1)*(q.intValue()-1));
- System.out.println("phin is:"+phin+"\nEnter e");
- e=sc.nextBigInteger();
- if(e.intValue()>phin.intValue())
- System.out.println("Enter value of e lesser than "+phin);
- k=e.intValue();
-
- /*for(d=1;d<phin.intValue();(d)++)
- {
- y=d*k;
- l=BigInteger.valueOf(y);
- //if(((y)).mod(phin)==1)
- if(l.mod(phin).equals(BigInteger.ZERO))
- break;
- }*/
- // d=e.modInverse(phin);
- res=d.compareTo(phin);
- long temp;
- while(res==-1)
- {
- y=d.intValue()*k;
- l=BigInteger.valueOf(y);
- res1=((l).mod(phin)).compareTo(BigInteger.ONE);
- if(res1==0)
- break;
- temp=d.intValue();
- temp++;
- d=BigInteger.valueOf(temp);
- //d.add(new BigInteger("1"));
- res=d.compareTo(phin);
- }
- System.out.println("d is: "+d.intValue()+"\nEnter Plaintext");
- m=sc.nextBigInteger();
-
- c=(BigInteger)((m.pow(e.intValue())).mod(n));
- System.out.println("C is: "+c);
-
- m=(BigInteger)((c.pow(d.intValue())).mod(n));
- System.out.println("M is: "+m);
- }
-
- }
- /*
- * OUTPUT:
- Enter p
- 3
- Enter q
- 11
- phin is:20
- Enter e
- 7
- d is: 3
- Enter Plaintext
- 2
- C is: 29
- M is: 2
- */
|