Sunday, 16 June 2019

Write a program to implement Transposition Cipher. Information and Network Security (2170709)



Algorithm/Steps:
1. First of all take the input file which contains the plaintext.
2. Read the file and provide the key.
3. Then arrange the plaintext letters row by row as per the key provided and if at the end space remains empty then append X.
4. Now the cipher text can easily be obtained by reading the column by column starting in sequence from 1,2,3…etc and arrange them in a single row.
Code:
Encryption
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.*;
public class Encryption {
            public static void main(String args[])
            {
                        try
                          {
                           File in = new File("En_input.txt");
                           File out = new File("En_output.txt");
                           Scanner sc = new Scanner(System.in);
                           BufferedReader inbr = new BufferedReader(new FileReader(in)) ;
                           FileWriter fwr = new FileWriter(out);
                           String input = inbr.readLine();
                           System.out.println("Enter key");
                           String key = sc.nextLine();
                           int key_length = key.length();
                           int input_length = input.length();
                           int temp;
                          
                           if((input_length%key_length)!=0)
                           {
                                       temp = input_length/key_length;
                                       temp = temp+1;
                           }
                           else
                           {
                                       temp = input_length/key_length;
                           }
                           String arr[][] = new String[temp+1][key_length];
                           int k=0;
                           for(int i=0;i<(temp+1);i++)
                           {
                                       for(int j=0;j<key_length;j++)
                                       {
                                                   if(i==0)
                                                   {
                                                               arr[i][j] = String.valueOf(key.charAt(j));
                                                   }
                                                   else
                                                   {
                                                               if((input_length%key_length)!=0)
                                                               {
                                                                           if(k<input.length())
                                                                           {
                                                                                       arr[i][j] = String.valueOf(input.charAt(k));
                                                                                       k++;
                                                                           }
                                                                           else
                                                                           {
                                                                                       arr[i][j] = String.valueOf('X');
                                                                           }
                                                               }
                                                               else
                                                               {
                                                                           arr[i][j] = String.valueOf(input.charAt(k));
                                                                           k++;
                                                               }
                                                   }
                                       }
                           }
                          /* for(int i=0;i<arr.length;i++)
                                    {
                                                for(int j=0;j<arr[i].length;j++)
                                                {
                                                            System.out.print("\t"+arr[i][j]+"\t");
                                                }
                                                System.out.print("\n");
                                    }
                                    */
                           String swap="";
                           int tmp;
                                    for(int i=0;i<key_length;i++)
                                    {
                                                for(int j=i+1;j<key_length;j++)
                                                {
                                                            if(Integer.parseInt(arr[0][i])>Integer.parseInt(arr[0][j]))
                                                            {
                                                                        tmp=Integer.parseInt(arr[0][i]);
                                                                        arr[0][i]=arr[0][j];
                                                                        arr[0][j]=String.valueOf(tmp);
                                                                       
                                                                        for(int q=1;q<temp+1;q++)
                                                                        {
                                                                                    swap=arr[q][i];
                                                                                    arr[q][i]=arr[q][j];
                                                                                    arr[q][j]=swap;
                                                                        }
                                                            }
                                                }
                                    }
                                    String cipher="";
                                    for(int i=0;i<key_length;i++)
                                    {
                                                for(int j=1;j<arr.length;j++)
                                                {
                                                            cipher = cipher + arr[j][i];
                                                }
                                    }
                           fwr.write(cipher);
                           System.out.println("Descrypted Successfully");
                           fwr.close();
                           }
                                    catch (IOException  e)
                                    { e.printStackTrace(); }             
            }
}
Decryption
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;
public class Decryption {
            public static void main(String args[])
            {
                        try
                          {
                           File in = new File("De_input.txt");
                           File out = new File("De_output.txt");
                           Scanner sc = new Scanner(System.in);
                           BufferedReader inbr = new BufferedReader(new FileReader(in)) ;
                           FileWriter fwr = new FileWriter(out);
                           String input = inbr.readLine();
                           System.out.println("Enter key");
                           String key = sc.nextLine();
                           String arr[][] = new String[(input.length()/key.length())+1][key.length()];
                           int q=0;
                           for(int i=0;i<key.length();i++)
                           {
                                       for(int j=0;j<(input.length()/key.length())+1;j++)
                                       {
                                                   if(j==0)
                                                   {
                                                               arr[j][i] = String.valueOf(i+1);
                                                   }
                                                   else
                                                   {
                                                               arr[j][i] = String.valueOf(input.charAt(q));
                                                               q++;
                                                   }
                                       }
                           }
                           String plain[][] = new String[(input.length()/key.length())+1][key.length()];
                           int column=0;
                           for(int i=0;i<key.length();i++)
                           {
                                       for(int j=0;j<key.length();j++)
                                       {
                                                   if(arr[0][j].equals(String.valueOf(key.charAt(i))))
                                                   {
                                                               for(int k=0;k<arr.length;k++)
                                                               {
                                                                           plain[k][column] = arr[k][j];
                                                               }
                                                               column++;
                                                   }
                                       }
                           }
                           StringBuilder out_build = new StringBuilder();
                           for(int i=1;i<plain.length;i++)
                           {
                                       for(int j=0;j<plain[i].length;j++)
                                       {
                                                   out_build.append(String.valueOf(plain[i][j])) ;
                                       }
                           }
                           for(int i=out_build.length()-1;i>=0;i--)
                           {
                                       if(out_build.charAt(i)=='X')
                                       {
                                                   out_build.setLength(out_build.length()-1);
                                       }
                                       else
                                       {
                                                   break;
                                       }
                           }
                           System.out.println(out_build);
                         }
                                    catch (IOException  e)
                                    { e.printStackTrace(); }  
            }         
}

1 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...