Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
196 views

Playfair Cipher - Encryption (Java)

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
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
196 views

Playfair Cipher - Encryption (Java)

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
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

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 java.util.Scanner;

public class MyProgram{

static void encrypt(String pt, String key) {


char[] pt_arr=pt.toCharArray();
char[] key_arr=key.toCharArray();
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<alpha.length;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';

System.out.print("\nThe Intermediate Text


is: ");
for(int i=0;i<c;i++)
System.out.print(any[i]);
System.out.println();

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
{
}
}

System.out.println("\nThe Reference Table


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

System.out.print("\nThe Encrypted text is:


");
for(int i=0;i<encpt_arr_ctr;i++)
System.out.print(encpt_arr[i]);
}

public static void main(String[] args) {


String pt, key;
System.out.print("Enter plaintext: ");
Scanner scanner = new Scanner(System.in);
pt=scanner.nextLine();
System.out.print("Enter key: ");
key=scanner.nextLine();
scanner.close();

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

You might also like