EX - NO - 1 (B) - Playfair Cipher - K.MOHAMED HASIM - PROGRAM
EX - NO - 1 (B) - Playfair Cipher - K.MOHAMED HASIM - PROGRAM
import java.util.Scanner;
System.out.print("---------------------------\n");
System.out.print("\n");
String msg=in.nextLine();
String key=in.nextLine();
pfEncryption.makeArray(key);
msg=pfEncryption.manageMessage(msg);
pfEncryption.doPlayFair(msg, "Encrypt");
String en=pfEncryption.getEncrypted();
System.out.println("------------------------------------------\n");
int option=sc.nextInt();
if (option==1) {
pfEncryption.doPlayFair(en, "Decrypt");
}}}
class PFEncryption{
keyword=keyword.toUpperCase().replace("J","I");
int val=0;
int uniqueLen;
present=false;
uniqueLen=0;
if (Character.toString(uniqueChar[k])==null){
break;
uniqueLen++;
if (keyword.charAt(i)==uniqueChar[j]){
present=true;
if (!present){
uniqueChar[val]=keyword.charAt(i);
val++;
ch=ch.replaceAll(Character.toString(keyword.charAt(i)), "");
uniqueChar[val]=ch.charAt(i);
val++;
val=0;
alphabets[i][j]=uniqueChar[val];
val++;
System.out.print(alphabets[i][j] + "\t");
System.out.println();
}}
int val=0;
int len=msg.length()-2;
String newTxt="";
String intermediate="";
while (len>=0){
intermediate=msg.substring(val, val+2);
if (intermediate.charAt(0)==intermediate.charAt(1)){
msg=msg.replaceFirst(intermediate, newTxt);
len++;
len-=2;
val+=2;
if (msg.length()%2!=0){
msg=msg+'x';
}
EX.NO – 1(B) - Playfair Cipher– K.MOHAMED HASIM - PROGRAM
int val=0;
while (val<msg.length()){
val+=2;
}}
char ch1=doubblyCh.charAt(0);
char ch2=doubblyCh.charAt(1);
if (alphabets[i][j]==ch1){
row1=i;
col1=j;
}else if (alphabets[i][j]==ch2){
row2=i;
col2=j;
}}}
if (tag=="Encrypt")
else if(tag=="Decrypt")
if (row1==row2){
col1=col1+1;
col2=col2+1;
if (col1>4)
EX.NO – 1(B) - Playfair Cipher– K.MOHAMED HASIM - PROGRAM
col1=0;
if (col2>4)
col2=0;
encrypted+=(Character.toString(alphabets[row1][col1])+Character.toString(alphabets[row1]
[col2]));
}else if(col1==col2){
row1=row1+1;
row2=row2+1;
if (row1>4)
row1=0;
if (row2>4)
row2=0;
encrypted+=(Character.toString(alphabets[row1][col1])+Character.toString(alphabets[row2]
[col1]));
}else{
encrypted+=(Character.toString(alphabets[row1][col2])+Character.toString(alphabets[row2]
[col1]));
}}
if (row1==row2){
col1=col1-1;
col2=col2-1;
if (col1<0)
col1=4;
if (col2<0)
col2=4;
decrypted+=(Character.toString(alphabets[row1][col1])+Character.toString(alphabets[row1]
[col2]));
}else if(col1==col2){
row1=row1-1;
row2=row2-1;
if (row1<0)
EX.NO – 1(B) - Playfair Cipher– K.MOHAMED HASIM - PROGRAM
row1=4;
if (row2<0)
row2=4;
decrypted+=(Character.toString(alphabets[row1][col1])+Character.toString(alphabets[row2]
[col1]));
}else{
decrypted+=(Character.toString(alphabets[row1][col2])+Character.toString(alphabets[row2]
[col1]));
String getEncrypted(){
return encrypted;
String getDecrypted(){
return decrypted;