Sunday, 16 June 2019

Write a program to implement RSA Algorithm. Information and Network Security (2170709) GTU



Algorithm/Steps:
               1. First take the plain text in one input file i.e say input.txt.
               2. Then ask the user to enter the necessary input data to encrypt the plain text.
               3. Read the text (character by character) from the file and generate the cipher text using
               RSA algorithm encryption C = me mod N..
               4. Then the cipher text must be saved in separate output file i.e say output.txt.
               5. After encryption, in similar manner perform the decryption using m = cd mod N
      which will give us the original plain text.
Code:
import java.math.BigInteger;
import java.util.Scanner;
public class RSA_Array {
            public static int gcd(int a,int b)
            {
                        int temp;
                        do
                        {
                                    temp=a%b;
                                    a=b;
                                    b=temp;
                        }while(temp!=0);
                        return a;
            }
            public static void main(String args[])
            {
                        Scanner sc = new Scanner(System.in);
                        Scanner sc1 = new Scanner(System.in);
                        System.out.println("Enter plain text");
                        String m = sc.nextLine();
                        m = m.toUpperCase();
                        int cipher_m[]  = new int[m.length()];
                        System.out.println("Enter P");
                        int P = sc.nextInt();
                        System.out.println("Enter Q");
                        int Q = sc.nextInt();
                        int n=P*Q;
                        int fn = (P-1)*(Q-1);
                        int temp;
                        int i=2;
                        do
                        {
                                    temp = gcd(i,fn);
                                    i++;
                        }while(temp!=1);
                        i--;
                        //System.out.println(i);
                        int e=i;
                        System.out.println("Enter e if not than enter 0");
                        int temp3 = sc.nextInt();
                        if(temp3!=0)
                        {
                                    e = temp3;
                        }
                        int temp2;
                        int k=0;
                        do
                        {
                                    temp2 = (1+k*fn)%e;
                                    k++;
                        }while(temp2!=0);
                        k--;
                        int d = (1+k*fn)/e;
                        //System.out.println(d);
                        double c = 0;
                        for(int z=0;z<m.length();z++)
                        {
                                    int l;
                                    l = (int)m.charAt(z)-64;
                                    //System.out.println(l);
                                    if(l<n)
                                    {
                                                c = Math.pow(l,e)%n;
                                    }
                        //System.out.println("Cipher Text="+c);
                        cipher_m[z]= (int) c;
                        }
                        /*BigInteger bi1, bi2, bi3;
                        BigInteger exponent = new BigInteger(String.valueOf(d));
                  bi1 = new BigInteger(String.valueOf((int)c));
                  bi2 = new BigInteger(String.valueOf(n));

                  // perform modPow operation on bi1 using bi2 and exp
                  bi3 = bi1.modPow(exponent, bi2);
                        System.out.println("Plain Text = "+bi3);*/
                        System.out.println("Cipher Text");
                        System.out.println("Integer Form");
                        for(int z=0;z<cipher_m.length;z++)
                                    System.out.print(cipher_m[z]+",");
                        System.out.println("\nCharacter Form");
                        for(int z=0;z<cipher_m.length;z++)
                                    System.out.print((char)(cipher_m[z]+64)+",");
                        int plain_m[] = new int[cipher_m.length];
                        for(int z=0;z<cipher_m.length;z++)
                        {
                                    BigInteger bi1, bi2, bi3;
                                    BigInteger exponent = new BigInteger(String.valueOf(d));
                              bi1 = new BigInteger(String.valueOf(cipher_m[z]));
                              bi2 = new BigInteger(String.valueOf(n));
                              // perform modPow operation on bi1 using bi2 and exp
                              bi3 = bi1.modPow(exponent, bi2);
                              plain_m[z] = bi3.intValue();
                                    //System.out.println("Plain Text = "+bi3);
                        }
                        System.out.println("\n\nPlain Text");
                        System.out.println("Integer Form");
                        for(int z=0;z<cipher_m.length;z++)
                                    System.out.print(plain_m[z]+",");
                        System.out.println("\nCharacter Form");
                        for(int z=0;z<cipher_m.length;z++)
                                    System.out.print((char)(plain_m[z]+64)+",");                        
            }
}

No comments:

Post a Comment

It's time To increase blogging capability. To have a chance to contribute in digital world. Any Interested People who want to make t...