0% found this document useful (0 votes)
119 views12 pages

Playfair Cipher: Encryption & Java Code

The Playfair cipher was the first practical digraph substitution cipher, invented in 1854 but named after Lord Playfair who promoted its use. It is significantly harder to break than simple substitution ciphers since frequency analysis does not work with it. The Playfair cipher was used for tactical military purposes during World War I and World War II because it is reasonably fast and requires no special equipment.

Uploaded by

Maicl Purici
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
119 views12 pages

Playfair Cipher: Encryption & Java Code

The Playfair cipher was the first practical digraph substitution cipher, invented in 1854 but named after Lord Playfair who promoted its use. It is significantly harder to break than simple substitution ciphers since frequency analysis does not work with it. The Playfair cipher was used for tactical military purposes during World War I and World War II because it is reasonably fast and requires no special equipment.

Uploaded by

Maicl Purici
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
  • Introduction
  • Explanation
  • Encryption Code
  • Sample Output

PLAYFAIR CIPHER

PLAYFAIR CIPHER – 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 Playfair is significantly harder to break since the
frequency analysis used for simple substitution ciphers
does not work with it.
 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
PLAYFAIR CIPHER – EXPLANATION
 Input Plaintext
 Input Key
 Convert it to an intermediate string
 Create a reference table
 Apply the norms on each pair of the letters of intermediate
text using the reference table and keep on generating the
cipher text in the due process
 Print the Encrypted text
 Decode the encrypted text by using exactly the reverse
process used for encryption
 Print the Decrypted text

ENCRYPTION - - -

Example :

Plaintext : JAZZ
Key : MONARCHY

Intermediate Text : JAZXZX


Reference Table :
M O N A R
C H Y B D
E F G I/J K
L P Q S T
U V W X Z

Encrypted Text : SBUZUZ

Code [ Java ] :

import [Link];

public class MyProgram{

static void encrypt(String pt, String key) {


char[] pt_arr=[Link]();
char[] key_arr=[Link]();
char[]
alpha={'A','B','C','D','E','F','G','H','I','K','L',
'M',

'N','O','P','Q','R','S','T','U','V','W','X','Y'
,'Z'};
char[][] table=new char[5][5];
char[] encpt_arr = new char[100];
int encpt_arr_ctr=0;
int ctr=-1;
for(int i=0;i<key_arr.length;i++)
for(int j=0;j<25;j++)
if(key_arr[i]==alpha[j])
{
ctr++;
int round = ctr/5;
table[round][ctr%5]=alpha[j];
alpha[j]='0';
break;
}
for(int i=0;i<[Link];i++)
{
if(alpha[i]!='0')
{
ctr++;
int round = ctr/5;
table[round][ctr%5]=alpha[i];
}
}

char[] any=new char[100];


int c=0;
for(int i=0;i<pt_arr.length;i++)
{
if(i==0 && pt_arr[i]!=pt_arr[i+1] &&
i+1<pt_arr.length)
{
any[c++]=pt_arr[i];
continue;
}
if(i==1 && pt_arr[i]!=pt_arr[i+1] &&
i+1<pt_arr.length)
{
any[c++]=pt_arr[i];
continue;
}

if(i==pt_arr.length-2 && i%2==0 &&


pt_arr[i]!=pt_arr[i-1]
&& pt_arr[i]!=pt_arr[i+1])
{
any[c++]=pt_arr[i];
continue;
}
if(i==pt_arr.length-2 && i%2==0 &&
pt_arr[i]!=pt_arr[i-1]
&& pt_arr[i]==pt_arr[i+1])
{
any[c++]=pt_arr[i];
any[c++]='X';
continue;
}
if(i==pt_arr.length-2 && i%2==0 &&
pt_arr[i]==pt_arr[i-1]
&& pt_arr[i]!=pt_arr[i+1])
{
any[c++]=pt_arr[i];
continue;
}
if(i==pt_arr.length-2 && i%2==0 &&
pt_arr[i]==pt_arr[i-1]
&& pt_arr[i]==pt_arr[i+1])
{
any[c++]=pt_arr[i];
any[c++]='X';
continue;
}
if(i==pt_arr.length-2 && i%2!=0 &&
pt_arr[i]!=pt_arr[i-1]
&& pt_arr[i]!=pt_arr[i+1])
{
any[c++]=pt_arr[i];
continue;
}
if(i==pt_arr.length-2 && i%2!=0 &&
pt_arr[i]!=pt_arr[i-1]
&& pt_arr[i]==pt_arr[i+1])
{
any[c++]=pt_arr[i];
any[c++]='X';
continue;
}
if(i==pt_arr.length-2 && i%2!=0 &&
pt_arr[i]==pt_arr[i-1]
&& pt_arr[i]!=pt_arr[i+1])
{
any[c++]=pt_arr[i];
continue;
}
if(i==pt_arr.length-2 && i%2!=0 &&
pt_arr[i]==pt_arr[i-1]
&& pt_arr[i]==pt_arr[i+1])
{
any[c++]=pt_arr[i];
any[c++]='X';
continue;
}

if(i==pt_arr.length-1 && i%2==0 &&


pt_arr[i]!=pt_arr[i-1])
{
any[c++]=pt_arr[i];
continue;
}
if(i==pt_arr.length-1 && i%2==0 &&
pt_arr[i]==pt_arr[i-1])
{
any[c++]=pt_arr[i];
any[c++]='X';
continue;
}
if(i==pt_arr.length-1 && i%2!=0 &&
pt_arr[i]!=pt_arr[i-1])
{
any[c++]=pt_arr[i];
continue;
}
if(i==pt_arr.length-1 && i%2!=0 &&
pt_arr[i]==pt_arr[i-1])
{
any[c++]=pt_arr[i];
any[c++]='X';
continue;
}

if(i%2==0 && pt_arr[i]!=pt_arr[i-1] &&


pt_arr[i]==pt_arr[i+1])
{
any[c++]=pt_arr[i];
any[c++]='X';
continue;
}
if(i%2==0 && pt_arr[i]!=pt_arr[i-1] &&
pt_arr[i]!=pt_arr[i+1])
{
any[c++]=pt_arr[i];
continue;
}
if(i%2==0 && pt_arr[i]==pt_arr[i-1] &&
pt_arr[i]!=pt_arr[i+1])
{
any[c++]=pt_arr[i];
continue;
}
if(i%2==0 && pt_arr[i]==pt_arr[i-1] &&
pt_arr[i]==pt_arr[i+1])
{
any[c++]=pt_arr[i];
any[c++]='X';
continue;
}

if(i%2!=0 && pt_arr[i]!=pt_arr[i-1] &&


pt_arr[i]==pt_arr[i+1])
{
any[c++]=pt_arr[i];
any[c++]='X';
continue;
}
if(i%2!=0 && pt_arr[i]!=pt_arr[i-1] &&
pt_arr[i]!=pt_arr[i+1])
{
any[c++]=pt_arr[i];
continue;
}
if(i%2!=0 && pt_arr[i]==pt_arr[i-1] &&
pt_arr[i]!=pt_arr[i+1])
{
any[c++]=pt_arr[i];
continue;
}
if(i%2!=0 && pt_arr[i]==pt_arr[i-1] &&
pt_arr[i]==pt_arr[i+1])
{
any[c++]=pt_arr[i];
any[c++]='X';
continue;
}
}
if(c%2!=0)
any[c++]='X';

[Link]("\nThe Intermediate Text


is: ");
for(int i=0;i<c;i++)
[Link](any[i]);
[Link]();

for(int i=0;i<c;i=i+2)
{
if(any[i]=='J')
any[i]='I';
if(any[i+1]=='J')
any[i+1]='I';
int row1=0,row2=0,col1=0,col2=0;
for(int j=0;j<5;j++)
{
for(int k=0;k<5;k++)
{
if(any[i]==table[j][k])
{
row1=j;
col1=k;
break;
}
}
}
for(int j=0;j<5;j++)
{
for(int k=0;k<5;k++)
{
if(any[i+1]==table[j][k])
{
row2=j;
col2=k;
break;
}
}
}
if(row1==row2)
{
col1=(col1+1)%5;
col2=(col2+1)%5;

encpt_arr[encpt_arr_ctr++]=table[row1][col1];

encpt_arr[encpt_arr_ctr++]=table[row2][col2];
}
else if(col1==col2)
{
row1=(row1+1)%5;
row2=(row2+1)%5;

encpt_arr[encpt_arr_ctr++]=table[row1][col1];

encpt_arr[encpt_arr_ctr++]=table[row2][col2];
}
else if(row1!=row2 && col1!=col2)
{
int row=0,col=0;
row=row1;
col=col2;

encpt_arr[encpt_arr_ctr++]=table[row][col];
row=row2;
col=col1;

encpt_arr[encpt_arr_ctr++]=table[row][col];
}
else
{
}
}

[Link]("\nThe Reference Table


is: ");
for(int i=0;i<5;i++)
{
for(int j=0;j<5;j++)
[Link](table[i][j]+" ");
[Link]("\n");
}

[Link]("\nThe Encrypted text is:


");
for(int i=0;i<encpt_arr_ctr;i++)
[Link](encpt_arr[i]);
}

public static void main(String[] args) {


String pt, key;
[Link]("Enter plaintext: ");
Scanner scanner = new Scanner([Link]);
pt=[Link]();
[Link]("Enter key: ");
key=[Link]();
[Link]();

encrypt(pt,key);

}
}
Sample Output :
Enter plaintext: JAZZ
Enter key: MONARCHY

The Intermediate Text is: JAZXZX

The Reference Table is:


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

The Encrypted text is: SBUZUZ

Common questions

Powered by AI

The computational complexity of implementing the Playfair cipher in Java is primarily determined by the need to create and manage data structures (such as arrays for input, key storage, and the 5x5 matrix) and the nested iteration involved in both building and searching the matrix. Considerations include ensuring uniqueness in the letter matrix, handling text preprocessing (such as dealing with 'J/I' or repeated characters), and maintaining performance when dealing with potentially large texts. Effective memory management and efficient iteration techniques are necessary to keep the process scalable and responsive .

In the Playfair cipher, when a pair of consecutive identical letters is encountered in the plaintext, an 'X' is inserted between them to differentiate the pair, as digraph substitution would otherwise not work meaningfully. This ensures that each digraph consists of two distinct letters and aligns with the rule that no digraph can consist of repeat letters .

The key is integral to both the security and functional application of the Playfair cipher. It influences the structure of the 5x5 matrix, which directly affects the output of the digraph substitutions used for encrypting the plaintext. This customization ensures that the encryption is variable and unique to each key, providing a layer of complexity that makes decryption without the key considerably challenging. The security of the cipher largely depends on an attacker not knowing the key, rather than any inherent complexity in its operation .

The Playfair cipher was employed by British forces in the Second Boer War and World War I, and by Australians in World War II. Its suitability lay in its relative speed and ease of use, coupled with the fact that it required no specialized equipment, making it ideal for tactical use on the battlefield. By the time the enemy could potentially break the cipher's messages, the encrypted information was often of no operational value, adding a layer of temporal security .

The Playfair cipher represents a transitional phase in cryptography, moving from simple substitution ciphers, where individual letters are substituted independently, to more complex systems that begin to encrypt patterns in text. Its use of digraphs exemplifies an early step toward recognizing and disrupting inherent patterns in language, setting the stage for future cryptographic techniques that would employ increasingly sophisticated mathematical principles to secure communications. It indicates a growing understanding of the need to protect against statistical attacks, pushing the boundaries of encryption to data structures like matrices .

The Playfair cipher uses digraph substitution, meaning it encrypts pairs of letters at a time, rather than single letters as in simple substitution ciphers. This approach eliminates straightforward frequency analysis, as the frequency of individual characters no longer directly correlates to the ciphertext. Moreover, the use of a key-based 5x5 matrix further obfuscates the substitution pattern since each letter pair can be substituted in multiple ways depending on their position in the matrix .

The reference table in the Playfair cipher is a 5x5 matrix constructed from a keyword. The keyword is written into the matrix, followed by filling in the remaining spaces with the rest of the alphabet, omitting duplicates, and typically combining 'I' and 'J'. This prearranged table determines how each digraph in the plaintext is substituted during encryption. The arrangement of letters in this table significantly influences which letter pairs are generated and substituted, making the encryption dependent on the keyword .

The 'X' character is inserted in the Playfair cipher for two primary reasons: one, to separate duplicate letters within a digraph, ensuring each letter pair is distinct, and two, to complete an odd-length message by providing an even number of characters. This maintains the integrity of the digraph substitution rule, which requires pairing letters to encrypt them effectively .

Despite its historical advantages, the Playfair cipher suffers from several limitations. Its security is relatively low by modern standards since it can be susceptible to more sophisticated analyses than frequency alone, such as digraph frequency attacks or brute-force if the key is short. It also only encrypts pairs of letters, which leaves some patterns intact. The cipher's reliance on a shared secret key means that key distribution remains a vulnerability, particularly in environments where key compromise is possible .

The decryption process in the Playfair cipher involves reversing the encryption procedure by using the same 5x5 matrix. This means taking each ciphertext digraph and finding its corresponding plaintext digraph by adjusting based on the rules applied during encryption, such as row, column, or rectangle swaps, effectively reversing them. The key is crucial for alignment in this reverse operation, ensuring that the plaintext is recovered accurately .

PLAYFAIR CIPHER 
 
PLAYFAIR CIPHER – INTRODUCTION 
 The Playfair cipher was the first practical digraph 
substitution ci
PLAYFAIR CIPHER – EXPLANATION  
 Input Plaintext 
 Input Key 
 Convert it to an intermediate string 
 Create a refere
Reference Table : 
M  
O  
N  
A  
R  
C  
H  
Y  
B  
D 
E  
F  
G     I/J  K 
L  
P  
Q  
S  
T 
U  
V  
W  X  
Z
for(int i=0;i<key_arr.length;i++) 
 
 
 
for(int j=0;j<25;j++) 
 
 
 
 
if(key_arr[i]==alpha[j]) 
 
 
 
 
{
} 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
if(i==pt_arr.length-2 && i%2==0 && 
pt_arr[i]!=pt_arr[i-1] 
 
 
 
&& pt_arr[i]!=pt
{ 
 
 
 
 
any[c++]=pt_arr[i]; 
 
 
 
 
continue; 
 
 
 
} 
 
 
 
if(i==pt_arr.length-2 && i%2!=0 && 
pt_arr[i]!=pt
{ 
 
 
 
 
any[c++]=pt_arr[i]; 
 
 
 
 
any[c++]='X'; 
 
 
 
 
continue; 
 
 
 
} 
 
 
 
if(i==pt_arr.length-1 && i
continue; 
 
 
 
} 
 
 
 
if(i%2==0 && pt_arr[i]==pt_arr[i-1] && 
pt_arr[i]==pt_arr[i+1]) 
 
 
 
{ 
 
 
 
 
any[c
} 
 
 
if(c%2!=0) 
 
 
 
any[c++]='X'; 
 
 
 
 
 
System.out.print("
The Intermediate Text 
is: "); 
 
 
for(int i=0
break; 
 
 
 
 
 
} 
 
 
 
 
} 
 
 
 
} 
 
 
 
if(row1==row2) 
 
 
 
{ 
 
 
 
 
col1=(col1+1)%5; 
 
 
 
 
col

You might also like