You are on page 1of 7

IT117-Jaimish Trivedi

EXPERIMENT: 2

AIM: Write a program to implement PlayFair cipher.

Playfair Cipher:
import​ java.lang.*;
import​ java.util.*;

class​ ​Playfair​ {
​private​ String key;
​private​ List<List<Character>> matrix = ​new​ ArrayList<>();
​private​ ​boolean​ J = ​false​;
​private​ ​boolean​ I = ​false​;
​private​ List<Integer> iLocation = ​new​ ArrayList<>();
​private​ List<Integer> jLocation = ​new​ ArrayList<>();
​char​ filler;

​public​ ​Playfair​(String key){


​this​.key = key;
​this​.prepareMatrix();
}

​//Helper Function for Preparing key matrix based on the key


​//If j comes first in key, then ‘j’ will be stored in matrix otherwise i
will be stored
​private​ ​void​ ​prepareMatrix​(){
StringBuilder track = ​new​ StringBuilder(​25​);
​int​ count = ​0​;
​int​ m = ​97​;
​boolean​ flagJ = ​false​;
​boolean​ flagI = ​false​;
​for​(​int​ i=​0​; i<​5​; i++){
List<Character> temp = ​new​ ArrayList<>();
​for​(​int​ j=​0​; j<​5​; j++){
​if​(count < ​this​.key.length()){
String t = track.toString();
​char​ c = ​this​.key.charAt(count);
​if​(t.indexOf(c) != -​1​){
count++;
j--;
​continue​;
}​else​{
​//All the possibilities of i and j occurring in keyword
​if​(c==​'j'​ && t.indexOf(​'i'​)!=-​1​){
count++;
j--;
flagI = ​true​;
​continue​;
}
​if​(c==​'i'​ && t.indexOf(​'j'​)!=-​1​){
IT117-Jaimish Trivedi

count++;
j--;
flagJ = ​true​;
​continue​;
}
​if​(c==​'j'​ && t.indexOf(​'i'​)==-​1​){
flagJ = ​true​;
}
​if​(c==​'i'​ && t.indexOf(​'j'​)==-​1​){
flagI = ​true​;
}
temp.add(c);
track.append(c);
count++;
}
}​else​{
String t = track.toString();
​//All the handling of next characters being added in matrix,
​//and controlling all the Possibilities of i and j added or
not added in matrix previously
​while​((t.indexOf((​char​)m)!=-​1​ && m<​123​)||(m==​105​ ||
m==​106​)){
​if​((m==​105​ && flagJ==​true​) || (m==​106​ && flagI==​true​) ||
(m==​105​ && flagI==​true​) || (m==​106​ && flagJ==​true​)){
m=​107​;
​continue​;
}
​if​((m==​105​ && flagI==​false​ && flagJ==​false​) || (m==​106
&& flagI==​false​ && flagJ==​false​)){
​if​(m==​105​) flagI=​true​;
​if​(m==​106​) flagJ=​true​;
​break​;
}
m++;
}
temp.add((​char​)m);
track.append((​char​)m);
}
}
​this​.matrix.add(temp);
}
​this​.J = flagJ;
​this​.I = flagI;
}

​//Encryption of plain text


​public​ String ​encrypt​(String plain){
String text = preparePlain(plain);
String[] pairs = divideText(text);
StringBuilder encrypted = ​new​ StringBuilder();
IT117-Jaimish Trivedi

​for​(​int​ i=​0​; i<pairs.length; i++){


​char​ c1 = pairs[i].charAt(​0​);
​char​ c2 = pairs[i].charAt(​1​);
​int​[] idx1 = getIndex(c1);
​int​[] idx2 = getIndex(c2);
​int​[] idx3 = ​new​ ​int​[​2] ​ ;
​int​[] idx4 = ​new​ ​int​[​2] ​ ;
​if​(idx1[​0​] == idx2[​0​]){
idx3[​0]​ = idx1[​0​];
idx4[​0] ​ = idx1[​0​];
idx3[​1] ​ = (idx1[​1​]+​1​)%​5​;
idx4[​1] ​ = (idx2[​1​]+​1​)%​5​;
}​else​ ​if​(idx1[​1​] == idx2[​1​]){
idx3[​1] ​ = idx1[​1​];
idx4[​1] ​ = idx1[​1​];
idx3[​0] ​ = (idx1[​0​]+​1​)%​5​;
idx4[​0] ​ = (idx2[​0​]+​1​)%​5​;
}​else​{
idx3[​0] ​ = idx1[​0​];
idx3[​1] ​ = idx2[​1​];
idx4[​0] ​ = idx2[​0​];
idx4[​1] ​ = idx1[​1​];
}
encrypted.append(matrix.get(idx3[​0​]).get(idx3[​1​]));
encrypted.append(matrix.get(idx4[​0​]).get(idx4[​1​]));
}
​return​ encrypted.toString();
}

​//Helper Function to Prepare plain text for encryption


​private​ String ​preparePlain​(String plain){
​char​ filler;
​//Decide fillers based on the plain text
​if​(plain.indexOf(​'x'​)!=-​1​)
filler = ​'z'​;
​else
filler = ​'x'​;

​this​.filler = filler;
​int​ len = plain.length();
​int​ og_len = len;
​//Remove whitespaces from plain text
plain = plain.replace(Character.toString(​' '​), ​""​);
​//Replace i with j or j with i based on the key matrix
​if​(​this​.J == ​true​){
setLocations(plain, ​'i'​);
plain = plain.replace(​'i'​, ​'j'​);
}​else​ ​if​(​this​.I == t ​ rue​){
setLocations(plain, ​'j'​);
plain = plain.replace(​'j'​, ​'i'​);
IT117-Jaimish Trivedi

​int​[] idx = findIndexes(plain);


plain = addFillers(plain, filler, idx);
​return​ plain;
}

​//Helper Function to Get locations of i or j, because we are going to


replace them on the basis of key matrix
​private​ ​void​ ​setLocations​(String plain, ​char​ c){
​for​(​int​ i=​0​; i<plain.length();i++){
​if​(plain.charAt(i)==c && c==​'i'​){
​this​.iLocation.add(i);
}​else​ ​if​(plain.charAt(i)==c && c==​'j'​){
​this​.jLocation.add(i);
}
}
}

​//Helper Function to Add filler after the indices of the repeating letters
​private​ String ​addFillers​(String plain, ​char​ filler, ​int​[] idx){
​int​ n = plain.length();
​if​(idx.length==​0​ && n%​2​==​0) ​ {
​return​ plain;
}​else​ ​if​(idx.length!=​0) ​ {
​for​(​int​ i=idx.length-​1;​ i>=​0​; i--){
StringBuilder temp = ​new​ StringBuilder(plain);
temp.insert(idx[i]+​1​, filler);
plain = temp.toString();
}
}
​//After adding fillers if the length becomes odd, add another filler at
the end
​if​(plain.length()%​2​!=​0) ​ {
StringBuilder temp = ​new​ StringBuilder(plain);
temp.insert(plain.length(), filler);
plain = temp.toString();
}
​return​ plain;
}

​//Helper Function to Divide the plain text into the pairs of 2 letters
​private​ String[] divideText(String text){
String[] pairs = ​new​ String[text.length()/​2​];
​int​ count=​0​;
​for​(​int​ i=​0​; i<text.length(); i+=​2​){
StringBuilder temp = ​new​ StringBuilder(​2​);
temp.append(text.charAt(i));
temp.append(text.charAt(i+​1​));
pairs[count] = temp.toString();
IT117-Jaimish Trivedi

count++;
}
​return​ pairs;
}

​//Helper Function to Find the index position of letter in the key matrix
​private​ ​int​[] getIndex(​char​ c){
​int​[] idx = ​new​ ​int​[​2​];
​for​(​int​ i=​0​; i<matrix.size(); i++){
​int​ j = matrix.get(i).indexOf(c);
​if​(j!=-​1​){
idx[​0​] = i;
idx[​1​] = j;
​return​ idx;
}
}
​return​ idx;
}

​//Decryption of Cipher Text


​public​ String ​decrypt​(String cipher){
String[] pairs = divideText(cipher);
StringBuilder decrypted = ​new​ StringBuilder();
​for​(​int​ i=​0​; i<pairs.length; i++){
​char​ c1 = pairs[i].charAt(​0​);
​char​ c2 = pairs[i].charAt(​1​);
​int​[] idx1 = getIndex(c1);
​int​[] idx2 = getIndex(c2);
​int​[] idx3 = ​new​ ​int​[​2] ​ ;
​int​[] idx4 = ​new​ ​int​[​2] ​ ;
​if​(idx1[​0​] == idx2[​0​]){
idx3[​0] ​ = idx1[​0​];
idx4[​0] ​ = idx1[​0​];
idx3[​1] ​ = (idx1[​1​]-​1​)%​5​;
​if​(idx3[​1​]<​0​) idx3[​1​]+=​5​; ​//If index becomes -ve then round up
again
idx4[​1] ​ = (idx2[​1​]-​1​)%​5​;
​if​(idx4[​1​]<​0​) idx4[​1​]+=​5​;
}​else​ ​if​(idx1[​1​] == idx2[​1​]){
idx3[​1] ​ = idx1[​1​];
idx4[​1] ​ = idx1[​1​];
idx3[​0] ​ = (idx1[​0​]-​1​)%​5​;
​if​(idx3[​0​]<​0​) idx3[​0​]+=​5​;
idx4[​0] ​ = (idx2[​0​]-​1​)%​5​;
​if​(idx4[​0​]<​0​) idx4[​0​]+=​5​;
}​else​{
idx3[​0] ​ = idx1[​0​];
idx3[​1] ​ = idx2[​1​];
idx4[​0] ​ = idx2[​0​];
idx4[​1] ​ = idx1[​1​];
IT117-Jaimish Trivedi

}
decrypted.append(matrix.get(idx3[​0​]).get(idx3[​1​]));
decrypted.append(matrix.get(idx4[​0​]).get(idx4[​1​]));
}
​return​ prepareCipher(decrypted.toString());
}

​//Helper Function to Prepare the cipher after decryption, and simply replace
fillers with none
​private​ String ​prepareCipher​(String cipher){
cipher = cipher.replace(Character.toString(​this​.filler), ​""​);
cipher = replaceChar(cipher);
​return​ cipher;
}

​//Helper Function for Replacing i with j or j with i in cipher text


corresponding to the matrix
​private​ String ​replaceChar​(String cipher){
StringBuilder temp = ​new​ StringBuilder(cipher);
​if​(​this​.J == ​true​){
​for​(​int​ c=​0​; c<​this​.iLocation.size(); c++){
temp.setCharAt(​this​.iLocation.get(c), ​'i'​);
}
}​else​ ​if​(​this​.I == t ​ rue​){
​for​(​int​ c=​0​; c<​this​.jLocation.size(); c++){
temp.setCharAt(​this​.jLocation.get(c), ​'j'​);
}
}
​return​ temp.toString();
}

​//Helper Function to Find the indices of the first repeating letters


​private​ ​int​[] findIndexes(String plain){
List<Integer> id = ​new​ ArrayList<>();
​int​ n = plain.length();
​int​ i = ​0​;
​for​(i=​0​; i<n; i++){
​if​(i==n-​1​)
​break​;
​if​(plain.charAt(i) == plain.charAt(i+​1​)){
id.add(i);
}
}
​return​ id.stream().mapToInt(j->j).toArray();
}

​//Helper Function to Print the Keyword Matrix


​public​ ​void​ ​showMatrix​(){
System.out.println(​"--------KEYMATRIX--------"​);
IT117-Jaimish Trivedi

​for​(​int​ i=​0​; i<​5​; i++){


​for​(​int​ j=​0​; j<​5​; j++){
System.out.print(​this​.matrix.get(i).get(j) + ​" "​);
}
System.out.println();
}
System.out.println(​"-------------------------"​);
}

​public​ ​static​ ​void​ ​main​(String[] args){


System.out.print(​"Enter A Key : "​);
Scanner sc = ​new​ Scanner(System.in);
String key = sc.nextLine();
Playfair c = ​new​ Playfair(key.toLowerCase()); ​//One Key per Object
c.showMatrix();
System.out.print(​"Enter a text : "​);
String plain = sc.nextLine();
String encrypted = c.encrypt(plain.toLowerCase());
System.out.println(​"Encrypted Text : "​+ encrypted);
System.out.println(​"Decrypted Text(Lower Letters without spaces) : "​+
c.decrypt(encrypted));
}
}

OUTPUT:

You might also like