Thursday, June 19, 2014

String Encryption & Decryption



There is many ways one can encrypt and decrypt the message. Following program is one way and given by TCS as assignment to the job aspirant.



Question


Mo and Larry have devised a way of encrypting messages.
They first decide secretly on the number of columns and write the message (letters only)
down the columns, padding with extra random letters so as to make a rectangular array
of letters. For example,
if the message is Theres no place like home on a snowy night and there are five columns,
Mo would write down

t o i o y
h p k n n
e l e a i
r a h s g
e c o n h
s e m o t
n l e w x

Note that Mo includes only letters and writes them all in lower case.
In this example, Mo used the character x to pad the message out to make a rectangle,
although he could have used any letter.
Mo then sends the message to Larry by writing the letters in each row,
alternating left-to-right and right-to-left. So, the above would be encrypted as


toioynnkpheleaigshareconhtomesnlewx


Your job is to recover for Larry the original message (along with any extra padding letters)
from the encrypted one.


Input Format
There will be multiple input sets.
Input for each set will consist of two lines.
The first line will contain an integer in the range 2...20 indicating the number of columns used.
The next line is a string of up to 200 lower case letters.
The last input set is followed by a line containing a single 0, indicating end of input.


Output Format

Each input set should generate one line of output,
giving the original plaintext message, with no spaces.


Sample Input

5
toioynnkpheleaigshareconhtomesnlewx
3
ttyohhieneesiaabss
0

Sample Output

theresnoplacelikehomeonasnowynightx
thisistheeasyoneab



Coding



import java.io.*;
public class ToandFro
    {

    public static void main(String args[])throws IOException
    {
        Convert_to();
        Convert_fro();
    }
    public static void Convert_to()throws IOException
     {
        char a[][];
        String s,s1;
        int n,n1,i,j,k;
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        System.out.println("Enter column and Sentence");
        n=Integer.parseInt(br.readLine());
        s=br.readLine();       
        s=s.replace(" ",""); 
        n1=(int)Math.ceil(s.length()/(double)n);
        s=s.toLowerCase();
        a=new char [n1][n];
        k=0;
        System.out.println("Convert To : ");
        System.out.println();
        for(i=0;i
         {
            for(j=0;j
             {
                if(k
                a[j][i]=s.charAt(k++);
                else
                a[j][i]='x';
             }
         }
        for(i=0;i
         {
           for(j=0;j
            {
                 System.out.print(a[i][j]+" ");
            }
            System.out.println();
        }
        System.out.println();
        s1="";
        for(i=0;i
         {
            if(i%2==0)
             {
               for(j=0;j
                {
                  s1=s1+a[i][j];
                }
             }
           else
            {
               for(j=n-1;j>=0;j--)
                  {
                   s1=s1+a[i][j];
                  }
            }
        }
    System.out.println(s1);
   }
    public static void Convert_fro()throws IOException
     {
        char a[][];
        String s,s1="",s2="";;
        int n=1,n1,i,j,k,c=0;;
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        System.out.println(s1);
        outer:
        while(n!=0)
         {
           while(true)
           {
             System.out.println("Enter column and Sentence");
             n=Integer.parseInt(br.readLine());
             if(n==0)
                    break outer;
             s=br.readLine();
             if(n>=2 && n<=20 && s.length()<=200)
              {
                    break;
              }
           }
           s=s.replace(" ","");
           s=s.toLowerCase();
           s2=s2+n+" "+s+" ";
           c++;

        }
        System.out.println("Convert Fro : ");
        System.out.println();
        for(int t=1;t<=c;t++)
         {
            int L=s2.indexOf(" ");
            n=Integer.parseInt(s2.substring(0,L));
            s2=s2.substring(L+1);
            L=s2.indexOf(" ");
            s=s2.substring(0,L);
            s2=s2.substring(L+1);
            n1=(int)Math.ceil(s.length()/(double)n);
            a=new char [n1][n];
            k=0;
            for(i=0;i
             {
                 if(i%2==0)
                   {
                       for(j=0;j
                         {
                             a[i][j]=s.charAt(k++);
                         }
                   }
                else
                   {
                       for(j=n-1;j>=0;j--)
                        {
                            a[i][j]=s.charAt(k++);
                        }
                    }
             }

             for(i=0;i
              {
                  for(j=0;j
                    {
                        s1=s1+a[j][i];
                    }
              }
             for(i=0;i
              {
                  for(j=0;j
                    {
                        System.out.print(a[i][j]+" ");
                    }
                  System.out.println();
              }
             System.out.println();
             s1=s1+" ";
     }
       j=0;
       for(i=0;i
         {
           if(s1.charAt(i)==' ')
             {
               s2=s1.substring(j,i);
               System.out.println(s2);
               j=i+1;
             }
  }
   }  
 }




Output:



Enter column and Sentence
5
Theres no place like home on a snowy night

Convert To :

t o i o y
h p k n n
e l e a i
r a h s g
e c o n h
s e m o t
n l e w x

toioynnkpheleaigshareconhtomesnlewx

Enter column and Sentence
5
toioynnkpheleaigshareconhtomesnlewx
Enter column and Sentence
3
thisistheeasyoneab
Enter column and Sentence
0

Convert Fro :

t o i o y
h p k n n
e l e a i
r a h s g
e c o n h
s e m o t
n l e w x

t h i
s i s
t h e
s a e
y o n
b a e

theresnoplacelikehomeonasnowynightx
tstsybhihaoaiseene




No comments: