7/23/2014

RSA Algorithm Code

I would like to share some 100% workiing programs which are generally included in Computer Science & Engineering Subjects like Network Security.
Encryption Algorithm :-
RSA is an encryption algorithm used to transfer data securely on network.
It is an asymmetric algorithm as there are two keys one for locking the message and other for opening the message.

RSA Algorithm in java for number  as input.

/*Author Name:Bhanupratap Singh
    Date:11/03/2014
    Subject:Internet and Network Security
*/
import java.math.BigInteger;
import java.math.BigInteger.*;
import java.util.*;
class RSA
{
    BigInteger cTextValue,cdTextValue;
    int count=0,err[],pTextValue,q,e,d,n,func_n,p;
    Scanner sc =new Scanner(System.in);
    public void getInput()
    {
        System.out.println("Enter the plaintext");
        pTextValue=sc.nextInt();   
        System.out.println(pTextValue);
        System.out.println("Enter the value of p");
        p=sc.nextInt();
        System.out.println("Enter the value of q");
        q=sc.nextInt();
    }
    public int gcd(int a,int b)
    {
        if(b==0)
        return a;
        return gcd(b,a%b);
    }
    public void keyGenerator()
    {   
        n=p*q;
        func_n=(p-1)*(q-1);
        err=new int[func_n];int j=0;
        for(int i=2;i<func_n;i++)
        {
        if(gcd(i,func_n)==1)
            {
               
                    e=i;
                    err[j]=e;
                    j++;
            }           
        }
        for(int l=0;l<func_n;l++)
        {
            if(err[l]==0)
            break;           
            else
            count++;
        }
        System.out.println("The posssible values of e are :");
        for(int c=0;c<count;c++)
        System.out.println(c+") "+err[c]);
        System.out.println("Enter the value of e you would like to use from displayed options...");
        e=sc.nextInt();
        for(int k=1;k<func_n;k++)
        {
        if(((k*e)%func_n)==1)
            {
            d=k;
            break;
            }
        }
        System.out.println("Public key: "+"{"+e+","+n+"}");
        System.out.println("Private key: "+"{"+d+","+n+"}");
    }
    public BigInteger calculate(int a,int b,int n1)
    {
        BigInteger c=new BigInteger(Integer.toString(a));
        BigInteger d=new BigInteger(Integer.toString(b));
        BigInteger n2=new BigInteger(Integer.toString(n));
        BigInteger res=c.modPow(d, n2);
        return res;
    }
     public BigInteger calculate(BigInteger a,int b,int n1)
    {
        BigInteger c=a;
        BigInteger d=new BigInteger(Integer.toString(b));
        BigInteger n2=new BigInteger(Integer.toString(n));
        BigInteger res=c.modPow(d, n2);
        return res;
    }
    public void enCryption()
    {   
        cTextValue=calculate(pTextValue,e,n);
        System.out.println("The cipher text value is: "+cTextValue);
    }
    public void deCryption()
    {
        cdTextValue=calculate(cTextValue, d, n);
        System.out.println("The plain text value is: "+cdTextValue);
    }
}

public class RSADemo {
    public static void main(String args[])
    {
    RSA r=new RSA();
    r.getInput();
    r.keyGenerator();
    r.enCryption();
    r.deCryption();
    }
   
}
/*OUTPUT
C:\Users\Home-Technology\Desktop\InsJava>javac RSADemo.java

C:\Users\Home-Technology\Desktop\InsJava>java RSADemo
Enter the plaintext
5
5
Enter the value of p
7
Enter the value of q
11
The posssible values of e are :
0) 7
1) 11
2) 13
3) 17
4) 19
5) 23
6) 29
7) 31
8) 37
9) 41
10) 43
11) 47
12) 49
13) 53
14) 59
Enter the value of e you would like to use from displayed options...
41
Public key: {41,77}
Private key: {41,77}
The cipher text value is: 38
The plain text value is: 5
*/


The sbove code will work for any key and plaintext.
NOTE:But plaintext should be a number .





No comments:

Post a Comment