Download - Columnar and Railfence
PROGRAM : [COLUMNAR.java]
import java.io.*;
import java.util.*;
public class columnar
{ public String encrypt(String key,String message)
{ char[] c = key.toCharArray(); char temp;
String ciphertext = ""; Arrays.sort(c);
int keyl = key.length(); int x;
for(int i=0; i< keyl; i++)
{ x = key.indexOf(c[i]); //System.out.println(x);
for(int j=0 ; j< message.length()/keyl; j++)
{ ciphertext = ciphertext + message.charAt((x+ (j*keyl))); }
}return ciphertext;
}
public String decrypt(String key,String ciphertext)
{ String text = ""; int x; char[] c = key.toCharArray();
String ptext = ""; Arrays.sort(c);
String text1 = "";
for(int i=0; i<(ciphertext.length()/key.length()); i++)
{ for(int j=i;j< ciphertext.length(); j++)
{ text = text + ciphertext.charAt(j);j += (ciphertext.length()/key.length())-1;
}}
//System.out.println(text); char[] ptext1 = new char[key.length()]; int j=0;
for(int i=0; i< ciphertext.length()/key.length(); i++)
{ text1 = text.substring((j*key.length()),((j+1)*key.length()));
//System.out.println(text1);
for(int k=0; k < text1.length(); k++)
{ ptext1[key.indexOf(c[k])] = text1.charAt(k); } j++;
for(char v : ptext1) ptext = ptext + v;
}// System.out.println(ptext);
while(true)
{ if(ptext.charAt(ptext.length()-1) == 'x') ptext = ptext.substring(0,ptext.length()-1); else break;}
return ptext; }
public static void main(String[] args)throws IOException
{
BufferedReader b = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Please enter the key :");
String key = b.readLine();
int keylength = key.length();
System.out.println("Please enter the message to be encrypted :");
String message = b.readLine();
int mess_length = message.length();
if(mess_length%keylength !=0)
{ for(int i=0; i< keylength - (mess_length%keylength); i++)
{ message = message + 'x'; } }
columnar c = new columnar();
System.out.println("Ciphertext is :"+ c.encrypt(key,message));
System.out.println("Please enter the ciphertext :");
String ctext = b.readLine();
System.out.println("The Plaintext for ["+ctext+"] is "+c.decrypt(key,ctext));
}
}
OUTPUT :
PROGRAM : [RAILFENCE.java]
import java.io.*;
import java.util.*;
public class railfence
{ public String message = ""; public BufferedReader b ; public int key;
private int rounds;
private String encryption()throws IOException
{ System.out.println("Please enter the message :");
b = new BufferedReader(new InputStreamReader(System.in));
message = b.readLine(); System.out.println("Please enter the key:");
key = Integer.parseInt(b.readLine()); String s="";
if(key==0 || key==1){ System.out.println("Please try again !"); System.exit(0);}
String ciphertext="";
if(key==2)
{ for(int i=0;i< message.length();i++)
{ ciphertext = ciphertext + message.charAt(i); i++; }
for(int i=1;i< message.length();i++)
{ ciphertext = ciphertext + message.charAt(i); i++; }
System.out.println("The ciphertext is :" + ciphertext); System.exit(0);
}
int distance,distance1; rounds=1; String[] ciphers = new String[key];
if(key != 2) { if(key%2==0)
{ distance = key+1; } else distance = key;
distance1 = distance;
for(int i=1; i<=message.length(); i++)
{ if((i+distance+1) <= message.length()) rounds++; i +=distance; }
if(key%2==0) { int j=0;
for(int i=0;i< rounds;i++)
{ ciphers[0] = ciphers[0] + message.charAt(j); j++;
for(int k=1; k < key-1;k++)
{
ciphers[k] = ciphers[k] + message.charAt(j); distance = distance - 2;
if((j+distance+1) < message.length())
ciphers[k] = ciphers[k] + message.charAt((j+distance+1)); j++;
}
if(j< message.length())ciphers[key-1] = ciphers[key-1] + message.charAt(j);
distance = distance1; j += distance/2 + 1; }
}else { System.out.println(rounds); int j=0;
for(int i=0;i< rounds;i++)
{ ciphers[0] = ciphers[0] + message.charAt(j); j++;
if(j<message.length())
{ for(int k=1; k < key-1;k++)
{ ciphers[k] = ciphers[k] + message.charAt(j); distance = distance - 2;
if((j+distance+1) < message.length())
ciphers[k] = ciphers[k] + message.charAt((j+distance+1)); j++; }
if(j< message.length())ciphers[key-1] = ciphers[key-1] + message.charAt(j);
distance = distance1; j += distance/2 + 1; } } }
}
for(String v : ciphers) s = s + (v.substring(4,(v.length())));
return s;
}
private String decryption()throws IOException
{ String ciphertext = ""; System.out.println("Please enter the ciphertext :");
b = new BufferedReader(new InputStreamReader(System.in));
ciphertext = b.readLine(); System.out.println("Please enter the key :");
int key = Integer.parseInt(b.readLine()); int distance,distance1;
if(key%2==0) { distance = key+1; }
else distance = key; distance1 = distance; char ptext[] = new char[ciphertext.length()];
for(int u=0;u< ciphertext.length();u++) ptext[u] = 'X';
int dist_of_lastround = rounds + distance1* (rounds-1);
boolean hasprimarybranch = false; boolean hassecbranch = false;
if((ciphertext.length() - dist_of_lastround) > 0) hasprimarybranch = true;
if((ciphertext.length() - dist_of_lastround) > (key-1)) hassecbranch = true;
int prim_count=0; int sec_count=0;
if(hasprimarybranch) prim_count = ciphertext.length() - dist_of_lastround;
if((ciphertext.length() - dist_of_lastround) == (key-1)) prim_count = prim_count - 1;
if(hassecbranch) sec_count = ciphertext.length() - (dist_of_lastround + (key-1));
int i=0;for(int j=0;j<rounds;j++)
{ ptext[i] = ciphertext.charAt(j); i += distance + 1; }i = rounds; int counter; String primary = "";
String secondary = ""; for(int k=1;k<key-1;k++)
{ counter = k; distance = distance - 2;
for(int l=0;l< rounds-1; l++)
{ ptext[counter] = ciphertext.charAt(i);
counter += (distance) + 1; i++; ptext[counter] = ciphertext.charAt(i);
counter = (l+1) + distance1*(l+1)+ k; i++; }
if(hasprimarybranch && prim_count >0)
{ primary = primary + ciphertext.charAt(i); i++; prim_count--; }
if(hassecbranch && sec_count > 1)
{ secondary = secondary + ciphertext.charAt(i); i++; sec_count--; }
}
counter = key-1; for(int l=0;l< rounds-1; l++)
{ ptext[counter] = ciphertext.charAt(i);
counter += distance1 + 1; i++; }
if(hasprimarybranch && (ciphertext.length()-dist_of_lastround == (key-1))) primary = primary + ciphertext.charAt(i);
String text = ""; for(char c : ptext) if(c != 'X') text = text + c;
text = text + primary;
return text;
}
public static void main(String[] args)throws IOException
{
railfence r = new railfence();
String ciphertext = r.encryption();
System.out.println("The ciphertext is : "+ciphertext);
String ptext = r.decryption();
System.out.println("The plaintext is : " + ptext);
}
}
OUTPUT :