Eces Experiment - 2
Eces Experiment - 2
EXPERIMENT: 2
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;
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;
}
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
//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;
}
}
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;
}
OUTPUT: