IMPLEMENTATION OF PLAYFAIR CIPHER

AIM:
To implement the Playfair Cipher using Java

ALGORITHM:
Step 1: Input the Plain text value and Key value.
Step 2: Generate the keymatrix.
Step 3: Encrypt the Plain text using the key.
Step 4: Decrypt the encrypted text

PROGRAM:
import java.util.Scanner;
class PlayFair {
String alphabets = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
char keyMatrix[][] = new char[5][5];

public static void main(String args[])
{
Scanner scanner = new Scanner(System.in);
System.out.println("Please enter the plain text : ");
String inputText = scanner.nextLine();
inputText = inputText.toLowerCase();
System.out.println("Please enter the Key :");
String encrKey = scanner.nextLine();
encrKey = encrKey.toLowerCase();
scanner.close();
PlayFair obj = new PlayFair();
String encryptText = obj.getEncryptedText(encrKey, inputText);
System.out.println("Encrypted text : " + encryptText);
String decryptedText = obj.getDecryptedText(encrKey,encryptText);
System.out.println( "Decrypted text : " + decryptedText );
}
boolean validateChar(char currChar)
{ if(alphabets.indexOf(currChar) < 0)
{
return true;
}
for(int i=0;i < keyMatrix.length;i++)
{
for(int j=0;j < keyMatrix[i].length;j++)
{
if(keyMatrix[i][j]==currChar || currChar=='J')
return true;
}
}
return false;
}
void addKey(String key)
{
key=key.toUpperCase();
key=key.replaceAll("J", "I");
key=key.replaceAll(" ", "");
int x = 0;
int y = 0;
char z ='A';
for(int i=0;i < key.length();i++)
{
if(!validateChar(key.charAt(i)))
{
keyMatrix[x][y++]=key.charAt(i);
if(y>4)
{
y=0;
x++;
}
}
}
while(x < 5)
{
while(y < 5)
{
if(!validateChar(z))
{
keyMatrix[x][y++] = z;
}
z++;
}
y=0;
x++;
}
System.out.println("Key Matrix ");
for(int i=0;i < 5;i++)
{
System.out.println();
for(int j=0;j < 5;j++)
{
System.out.print("\t" + keyMatrix[i][j]);
}
}
System.out.println("\n");

}
int getRowPos(char c)
{
for(int i=0;i < keyMatrix.length;i++)
{
for(int j=0;j < keyMatrix[i].length;j++)
{
if(keyMatrix[i][j]==c)
return i;
}
}
return -1;
}
int getColumnPos(char c)
{
for(int i=0;i < keyMatrix.length;i++)
{
for(int j=0;j < keyMatrix[i].length;j++)
{
if(keyMatrix[i][j]==c)
return j;
}
}
return -1;
}



String encryptChar(String plain)
{
plain = plain.toUpperCase();
char x = plain.charAt(0);
char y = plain.charAt(1);
String cipherChar = "";
int row1 = 0;
int col1 = 0;
int row2 = 0;
int col2 = 0;
row1 = getRowPos(x);
col1 = getColumnPos(x);
row2 = getRowPos(y);
col2 = getColumnPos(y);
if(col1==col2)
{
++row1;
++row2;
if(row1>4)
row1=0;
if(row2>4)
row2=0;
cipherChar+=keyMatrix[row1][col2];
cipherChar+=keyMatrix[row2][col1];
}
else if(row1==row2)
{
++col1;
++col2;
if(col1>4)
col1=0;
if(col2>4)
col2=0;
cipherChar+=keyMatrix[row1][col1];
cipherChar+=keyMatrix[row2][col2];
}
else{
cipherChar+=keyMatrix[row1][col2];
cipherChar+=keyMatrix[row2][col1];
}
return cipherChar;
}
String getEncryptedText(String key, String text)
{
addKey(key);
text=text.replaceAll("j", "i");
text=text.replaceAll(" ", "");
text=text.toUpperCase();
int textLength =text.length();
if(textLength/2 != 0)
{
text = text + "X";
textLength = textLength + 1;
}
String encrText = "";
for(int i = 0; i < textLength - 1; i = i+2)
{
encrText = encrText + encryptChar(text.substring(i,i+2));
encrText = encrText + " ";
}
return encrText;
}
String decryptChar(String text)
{
text=text.toUpperCase();
char x = text.charAt(0);
char y = text.charAt(1);
String decriptedChar = "";
int row1;
int col1;
int row2;
int col2;
row1=getRowPos(x);
col1=getColumnPos(x);
row2=getRowPos(y);
col2=getColumnPos(y);
if(col1==col2)
{
--row1;
--row2;
if(row1 < 0)
row1=4;
if(row2 < 0)
row2=4;
decriptedChar+=keyMatrix[row1][col2];
decriptedChar+=keyMatrix[row2][col1];
}
else if(row1==row2)
{
--col1;
--col2;
if(col1 < 0)
col1=4;
if(col2 < 0)
col2=4;
decriptedChar+=keyMatrix[row1][col1];
decriptedChar+=keyMatrix[row2][col2];
}
else{
decriptedChar+=keyMatrix[row1][col2];
decriptedChar+=keyMatrix[row2][col1];
}
return decriptedChar;
}
String getDecryptedText(String key, String text)
{
String decriptedText = "";
text=text.replaceAll("j", "i");
text=text.replaceAll(" ", "");
text=text.toUpperCase();
int len=text.length();
for(int i=0;i < len-1;i=i+2)
{
decriptedText = decriptedText + decryptChar(text.substring(i,i+2));
decriptedText = decriptedText + " ";
}
return decriptedText;
}
}

Output
Please enter the plain text : welcome
Please enter the Key :hello

Key Matrix

H    E    L    O    A

B    C    D    F    G

I    K    M    N    P

Q    R    S    T    U

V    W    X    Y   Z


Encrypted text :   EC   ED   LN   LW

Decrypted text :   WE   LC   OM   EX