Wednesday, December 15, 2010

RSA Encryption/ Decryption Algorithm

import java.util.*;

/**
 *
 * @author Sanket
 */
class RSA
{
    public static void main(String args[])
    {       
        Scanner sr=new Scanner(System.in);
               
        int x=0, j=0, p=0,q=0;
        int a[]=new int[100];
       
        System.out.println("Enter two prime numbers : ");
        main:
        {
            first:
                for(j=0;j<6;j++)
                {
                    if(j>=5)
                    {
                        System.out.println("First learn prime number concept and then try again");
                        break main;
                    }

                    System.out.print("Enter P : ");
                    p=sr.nextInt();
                   
                    x=0;
                    for(int i=1;i<=p;i++)
                    {
                        if(p%i==0)
                        {
                            a[x]=i;
                            if(i<p)
                                x++;
                        }
                    }
                   
                    if(x>1||x==0)
                    {
                        while(j<4)
                        {
                            System.out.println(p+" is not a prime number, Enter P again");
                            continue first;
                        }
                        continue first;
                    }
                   
                    if(x==1)
                        break first;
                }
               
            j=0;
            second:
                for(j=0;j<=6;j++)
                {
                    if(j>=5)
                    {
                        System.out.println("First learn prime number concept and then try again");
                        break main;
                    }
                   
                    System.out.print("Enter Q : ");
                    q=sr.nextInt();
                   
                    x=0;
                    for(int i=1;i<=q;i++)
                    {
                        if(q%i==0)
                        {
                            a[x]=i;
                            if(i<q)
                                x++;
                        }
                    }
                   
                    if(x>1||x==0)
                    {
                        while(j<4)
                        {
                            System.out.println(q+" is not a prime number, Enter Q again");
                            continue second;
                        }
                        continue second;
                    }
                   
                    if(x==1)
                        break second;
                }
   

            int n=p*q;
            int phi=(p-1)*(q-1);

            j=0;
            int e=0;
           
            third:
                for(j=0;j<6;j++)
                {
                    if(j==5)
                    {
                        System.out.println("First learn basic math & Prime number Concept and then try again");
                        break main;
                    }
                   
                    System.out.print("Enter value for E between 1 and "+phi+" and prime number : ");   
                    e=sr.nextInt();
                       
                    x=0;
                    for(int i=1;i<=p;i++)
                    {
                        if(e%i==0)
                        {
                            a[x]=i;
                            if(i<e)
                                x++;
                        }
                    }
                   
                    if(x>1||x==0)
                    {
                        while(j<4)
                        {
                            System.out.println(e+" is not a prime number, Enter E again");
                            continue third;
                        }
                    }
                    else
                    {
                        if(x==1)   
                            if(e>1&&e<phi)
                            {
                                break third;
                            }
                            else
                            {
                                System.out.println(e+" is not between 1 and "+phi);
                                continue third;
                            }
                    }
                }
           
            int d1=0;
            four:   
                for(int i=1;i<e;i++)
                {
                    d1=((phi*i)+1);
                    if(d1%e==0)
                    {
                        break four;
                    }
                }
           
            int d=d1/e;
           
            System.out.print("\nPublic key is : { "+n+", "+e+" } \n");
            System.out.print("Private key is : { "+n+", "+d+" } \n\n");
           
            System.out.print("Enter message to encrypt : ");
            int msg=sr.nextInt();

            int c=1, m=1;

            for(int i=0;i<e;i++)
            {
                c=c*msg;
                c=c%n;
            }
            System.out.println("\nEncrypted message is : "+c);

            for(int w=0;w<d;w++)
            {
                m=m*c;
                m=m%n;
            }
            System.out.println("Decrypted message is : "+m);
        }
    }
}

2 comments:

  1. Interesting! However, I have a suggestion.

    Instead of creating them as standalone console based applications, if you can convert them into re-usable functions, everyone (and you too) and just plug-n-play the functionality.

    Try it.

    Cheers!
    -Avinash

    ReplyDelete