Sunday, 18 October 2015


Practical No-6

AIM- Write a program to implement RSA algorithm in Java

import java.math.BigInteger;
import java.security.SecureRandom;
public class rsa
{
            private BigInteger n, d, e;
             private int bitlen = 1024;
              public rsa(BigInteger newn, BigInteger newe)
             {
                        n = newn;
                        e = newe;
            }
 public rsa(int bits)
{
                        bitlen = bits;
                        SecureRandom r = new SecureRandom();
                        BigInteger p = new BigInteger(bitlen / 2, 100, r);
                          BigInteger q = new BigInteger(bitlen / 2, 100, r);
                         n = p.multiply(q);
                        BigInteger m = (p.subtract(BigInteger.ONE)).multiply(q.subtract(BigInteger.ONE));
                        e = new BigInteger("3");
  while (m.gcd(e).intValue() > 1)
 {
                                     e = e.add(new BigInteger("2"));
                        }
                         d = e.modInverse(m);
             }
public synchronized BigInteger encrypt(BigInteger message)
 {
                         return message.modPow(e, n);
            }
 public synchronized BigInteger decrypt(BigInteger message)
{
                        return message.modPow(d, n);
}
public static void main(String[] args)
{
                         rsa rsa = new rsa(1024);
                        String text1 = "SERO";
                         System.out.println("Plaintext: " + text1);
                        BigInteger plaintext = new BigInteger(text1.getBytes());
                        BigInteger ciphertext = rsa.encrypt(plaintext);
                         System.out.println("Ciphertext: " + ciphertext);
                          plaintext = rsa.decrypt(ciphertext);
                           String text2 = new String(plaintext.toByteArray());
                         System.out.println("Plaintext: " + text2);
             }

}

No comments:

Post a Comment