PlayFair Cipher CryptoSystem Using JAVA with Example
Introduction:
The Playfair cipher was the first practical digraph substitution
cipher. The scheme was invented in 1854 by Charles Wheatstone, but was
named after Lord Playfair who promoted the use of the cipher. The
technique encrypts pairs of letters (digraphs), instead of single
letters as in the simple substitution cipher. The Playfair is
significantly harder to break since the frequency analysis used for
simple substitution ciphers does not work with it. Frequency analysis
can still be undertaken, but on the 25*25=625 possible digraphs rather
than the 25 possible monographs. Frequency analysis thus requires much
more ciphertext in order to work. For a tutorial on breaking Playfair
with a simulated annealing algorithm, see Cryptanalysis of the Playfair Ciphe.
It was used for tactical purposes by British forces in the Second
Boer War and in World War I and for the same purpose by the Australians
during World War II. This was because Playfair is reasonably fast to use
and requires no special equipment. A typical scenario for Playfair use
would be to protect important but non-critical secrets during actual
combat. By the time the enemy cryptanalysts could break the message the
information was useless to them.
From Kahn's 'The CodeBreakers':
Perhaps the most famous cipher of 1943 involved the future
president of the U.S., J. F. Kennedy, Jr. On 2 August 1943, Australian
Coastwatcher Lieutenant Arthur Reginald Evans of the Royal Australian
Naval Volunteer Reserve saw a pinpoint of flame on the dark waters of
Blackett Strait from his jungle ridge on Kolombangara Island, one of the
Solomons. He did not know that the Japanese destroyer Amagiri had
rammed and sliced in half an American patrol boat PT-109, under the
command of Lieutenant John F. Kennedy, United States Naval Reserve.
Evans received the following message at 0930 on the morning of the 2 of
August 1943:
Inventor of PlayFair Cipher CryptoSystem
Algorithm:
The 'key' for a playfair cipher is generally a word, for the sake of example we will choose 'monarchy'. This is then used to generate a 'key square', e.g.
m o n a r
c h y b d
e f g i k
l p q s t
u v w x z
Any sequence of 25 letters can be used as a key, so long as all letters are in it and there are no repeats. Note that there is no 'j', it is combined with 'i'. We now apply the encryption rules to encrypt the plaintext.
- Remove any punctuation or characters that are not present in the key square (this may mean spelling out numbers, punctuation etc.).
- Identify any double letters in the plaintext and replace the second occurence with an 'x' e.g. 'hammer' -> 'hamxer'.
- If the plaintext has an odd number of characters, append an 'x' to the end to make it even.
- Break the plaintext into pairs of letters, e.g. 'hamxer' -> 'ha mx er'
- The algorithm now works on each of the letter pairs.
- Locate the letters in the key square, (the examples given are using the key square above)
- If the letters are in different rows and columns, replace the pair with the letters on the same row respectively but at the other pair of corners of the rectangle defined by the original pair. The order is important – the first encrypted letter of the pair is the one that lies on the same row as the first plaintext letter. 'ha' -> 'bo', 'es' -> 'il'
- If the letters appear on the same row of the table, replace them with the letters to their immediate right respectively (wrapping around to the left side of the row if a letter in the original pair was on the right side of the row). 'ma' -> 'or', 'lp' -> 'pq'
- If the letters appear on the same column of the table, replace them with the letters immediately below respectively (wrapping around to the top side of the column if a letter in the original pair was on the bottom side of the column). 'rk' -> 'dt', 'pv' -> 'vo'
Clarification with pictures - Assume one wants to encrypt the digraph OR. There are three general cases:
-
m * * a * * * * * * * * * * * l * * s * * * * * *
Hence, al -> ms -
* * * * * * h y b d * * * * * * * * * * * * * * *
Hence, hb -> yd -
* * n * * * * y * * * * * * * * * q * * * * w * *
Hence, nq -> yw
plaintext: wearediscoveredsaveyourselfx ciphertext: ugrmkcsxhmufmkbtoxgcmvatluiv
Exmaple Program Using JAVA
Encryption
import java.util.*;
class PlayFairCipher{
String keyText;
String plainText;
String cipherText;
PlayFairCipher(){
keyText=new String("LGDBAQMHECURNIFXVSOKZYWTP");
plainText=new String();
cipherText=new String();
}
PlayFairCipher(String k,String p,String c){
keyText=k;
plainText=p;
cipherText=c;
}
void setKeyText(String k){
keyText=k.toUpperCase();
}
void setPlainText(String p){
plainText=p.toUpperCase();
}
void setCipherText(String c){
cipherText=c.toUpperCase();
}
String getKeyText(){
return keyText;
}
String getCipherText(){
return cipherText;
}
String getPlainText(){
return plainText;
}
void valideText(){
int i,k=0;
int len;
len=(plainText.length()%2==0)?(plainText.length()/2):(plainText.length()/2)+1;
String [] result=new String[len];
for(i=1;i<=result.length;i=i+2){
if(plainText.charAt(i-1)==plainText.charAt(i))
result[k++]=new String(""+plainText.charAt(i-1)+'X');
else
result[k++]=new String(""+plainText.charAt(i-1)+plainText.charAt(i));
}
for(i=0;i<result.length;i++)
System.out.println("result["+i+"]:- "+result[i]);
}
void removeSpace(){
int i,k=0;
char [] result=new char[plainText.length()];
for(i=0;i<plainText.length();i++){
if(plainText.charAt(i)==' ')
continue;
result[k++]=plainText.charAt(i);
}
plainText=new String("");
for(i=0;i<k;i++)
plainText+=result[i];
}
void addBogusLetter(){
int i;
String str=new String("");
removeSpace();
for(i=1;i<plainText.length();i++){
if(plainText.charAt(i-1)==plainText.charAt(i))
str+=""+plainText.charAt(i-1)+'X';
else
str+=plainText.charAt(i-1);
}
str+=plainText.charAt(plainText.length()-1);
if(str.length()%2!=0)
str+="X";
plainText=new String(str);
}
String makeCode(String str){
int i,j=0,k=0,pos=0,row,col;
int [][]temp=new int[2][2];
for(k=0;k<2;k++){
if(str.charAt(k)=='J')
pos=13;
else
pos=keyText.indexOf(str.charAt(k));
row=pos/5;
col=(pos%5);
temp[k][0]=row;
temp[k][1]=col;
}
int row1,col1;
row=temp[0][0];
col=temp[0][1];
row1=temp[1][0];
col1=temp[1][1];
if(row==row1){
col=(col+1)%5;
col1=(col1+1)%5;;
}
else if(col==col1){
row=(row+1)%5;
row1=(row1+1)%5;
}
else{
col=col1;
col1=row;
}
str=new String(""+keyText.charAt((row*5)+col));
str+=""+keyText.charAt((row1*5)+col1);
return str;
}
void makeCipher(){
String [] result=new String[plainText.length()/2];
String [] cipher=new String[result.length];
int i,j=0,k=0;
for(i=0;i<plainText.length();i=i+2){
result[j++]=new String(""+plainText.charAt(i)+plainText.charAt(i+1));
}
for(i=0;i<result.length;i++)
cipher[i]=makeCode(result[i]);
cipherText=new String("");
for(i=0;i<cipher.length;i++){
cipherText+=cipher[i];
}
}
}
class PlayFairEncryptoin{
public static void main(String [] args){
Scanner read=new Scanner(System.in);
PlayFairCipher pfc=new PlayFairCipher();
System.out.print("enter plainText:- ");
pfc.setPlainText(read.nextLine());
pfc.addBogusLetter();
System.out.println("PlainText:- "+pfc.getPlainText());
pfc.makeCipher();
System.out.println("cipherText:- "+pfc.getCipherText());
}
}
class PlayFairCipher{
String keyText;
String plainText;
String cipherText;
PlayFairCipher(){
keyText=new String("LGDBAQMHECURNIFXVSOKZYWTP");
plainText=new String();
cipherText=new String();
}
PlayFairCipher(String k,String p,String c){
keyText=k;
plainText=p;
cipherText=c;
}
void setKeyText(String k){
keyText=k.toUpperCase();
}
void setPlainText(String p){
plainText=p.toUpperCase();
}
void setCipherText(String c){
cipherText=c.toUpperCase();
}
String getKeyText(){
return keyText;
}
String getCipherText(){
return cipherText;
}
String getPlainText(){
return plainText;
}
void valideText(){
int i,k=0;
int len;
len=(plainText.length()%2==0)?(plainText.length()/2):(plainText.length()/2)+1;
String [] result=new String[len];
for(i=1;i<=result.length;i=i+2){
if(plainText.charAt(i-1)==plainText.charAt(i))
result[k++]=new String(""+plainText.charAt(i-1)+'X');
else
result[k++]=new String(""+plainText.charAt(i-1)+plainText.charAt(i));
}
for(i=0;i<result.length;i++)
System.out.println("result["+i+"]:- "+result[i]);
}
void removeSpace(){
int i,k=0;
char [] result=new char[plainText.length()];
for(i=0;i<plainText.length();i++){
if(plainText.charAt(i)==' ')
continue;
result[k++]=plainText.charAt(i);
}
plainText=new String("");
for(i=0;i<k;i++)
plainText+=result[i];
}
void addBogusLetter(){
int i;
String str=new String("");
removeSpace();
for(i=1;i<plainText.length();i++){
if(plainText.charAt(i-1)==plainText.charAt(i))
str+=""+plainText.charAt(i-1)+'X';
else
str+=plainText.charAt(i-1);
}
str+=plainText.charAt(plainText.length()-1);
if(str.length()%2!=0)
str+="X";
plainText=new String(str);
}
String makeCode(String str){
int i,j=0,k=0,pos=0,row,col;
int [][]temp=new int[2][2];
for(k=0;k<2;k++){
if(str.charAt(k)=='J')
pos=13;
else
pos=keyText.indexOf(str.charAt(k));
row=pos/5;
col=(pos%5);
temp[k][0]=row;
temp[k][1]=col;
}
int row1,col1;
row=temp[0][0];
col=temp[0][1];
row1=temp[1][0];
col1=temp[1][1];
if(row==row1){
col=(col+1)%5;
col1=(col1+1)%5;;
}
else if(col==col1){
row=(row+1)%5;
row1=(row1+1)%5;
}
else{
col=col1;
col1=row;
}
str=new String(""+keyText.charAt((row*5)+col));
str+=""+keyText.charAt((row1*5)+col1);
return str;
}
void makeCipher(){
String [] result=new String[plainText.length()/2];
String [] cipher=new String[result.length];
int i,j=0,k=0;
for(i=0;i<plainText.length();i=i+2){
result[j++]=new String(""+plainText.charAt(i)+plainText.charAt(i+1));
}
for(i=0;i<result.length;i++)
cipher[i]=makeCode(result[i]);
cipherText=new String("");
for(i=0;i<cipher.length;i++){
cipherText+=cipher[i];
}
}
}
class PlayFairEncryptoin{
public static void main(String [] args){
Scanner read=new Scanner(System.in);
PlayFairCipher pfc=new PlayFairCipher();
System.out.print("enter plainText:- ");
pfc.setPlainText(read.nextLine());
pfc.addBogusLetter();
System.out.println("PlainText:- "+pfc.getPlainText());
pfc.makeCipher();
System.out.println("cipherText:- "+pfc.getCipherText());
}
}
to run this program
compile: javac PlayFairEcryption.java
run: java PlayFairEncryption
No comments:
Post a Comment