-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPlayFair.java
More file actions
154 lines (145 loc) · 5.52 KB
/
PlayFair.java
File metadata and controls
154 lines (145 loc) · 5.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
import java.util.Scanner;
/**
*
* @author swa
*/
public class PlayFair {
public static char pfmatrix[][] = new char[5][5];
//To display matrix
public static void display(char array[][]){
for(int i=0; i<5; i++){
for(int j=0; j<5; j++){
System.out.print(array[i][j]+" ");
}
System.out.println();
}
}
//Initialising the playfair matrix
public static void initmatrix(String key){
int keyval = 0;
String alphabets = "abcdefghijklmnopqrstuvwXyz";
//Eliminate alphabets present in key
for(int i=0; i<key.length();i++)
alphabets = alphabets.replace(String.valueOf(key.charAt(i)),"");
//Fill in matrix with key and remaining alphabets
key+=alphabets;
for(int i=0; i<5; i++){
for(int j=0; j<5; j++){
pfmatrix[i][j] = key.charAt(keyval);
keyval++;
}
}
}
//Find the character to replace with
public static String find(char c1, char c2, int opt){
int row1=-1,row2=-1,col1=-1,col2=-1,flag1=0,flag2=0;
String code="";
for(int i=0; i<5; i++){
for(int j=0; j<5;j++){
if(pfmatrix[i][j] == c1) {
row1 = i;
col1 = j;
flag1 = 1;
}
else if(pfmatrix[i][j] == c2) {
row2 = i;
col2 = j;
flag2 = 1;
}
if(flag1==1 && flag2==1) break;
}
}
if(row1==row2) {
if(col1+opt == 5)
code += String.valueOf(pfmatrix[row1][0]) + String.valueOf(pfmatrix[row2][col2+opt]);
else if(col2+opt == 5)
code += String.valueOf(pfmatrix[row1][col1+opt]) + String.valueOf(pfmatrix[row2][0]);
else if(col1+opt == -1)
code += String.valueOf(pfmatrix[row1][4]) + String.valueOf(pfmatrix[row2][col2+opt]);
else if(col2+opt == -1)
code += String.valueOf(pfmatrix[row1][col1+opt]) + String.valueOf(pfmatrix[row2][4]);
else
code += String.valueOf(pfmatrix[row1][col1+opt]) + String.valueOf(pfmatrix[row2][col2+opt]);
}
else if(col1==col2) {
if(row1+opt == 5)
code += String.valueOf(pfmatrix[0][col1]) + String.valueOf(pfmatrix[row2+opt][col2]);
else if(row2+opt == 5)
code += String.valueOf(pfmatrix[row1+opt][col1]) + String.valueOf(pfmatrix[0][col2]);
else if(row1+opt == -1)
code += String.valueOf(pfmatrix[4][col1]) + String.valueOf(pfmatrix[row2+opt][col2]);
else if(row2+opt == -1)
code += String.valueOf(pfmatrix[row1+opt][col1]) + String.valueOf(pfmatrix[4][col2]);
else
code += String.valueOf(pfmatrix[row1+opt][col1]) + String.valueOf(pfmatrix[row2+opt][col2]);
}
else {
code += String.valueOf(pfmatrix[row1][col2]) + String.valueOf(pfmatrix[row2][col1]);
}
return code;
}
//Split the plaintext into pairs
public static String pair(String message) {
String pairing = "";
for(int i=0; i<message.length();i++){
pairing += message.charAt(i);
if(i+1<message.length() && message.charAt(i)==message.charAt(i+1))
pairing += 'X';
}
if(pairing.length()%2!=0)
pairing += 'X';
System.out.println("After pairing: " + pairing);
return pairing;
}
//Encryption
public static String encryptpf(String message, String key) {
String encrypt="";
initmatrix(key);
display(pfmatrix);
String pairing = pair(message);
for(int j=0; j<=pairing.length()-2; j+=2){
encrypt += find(pairing.charAt(j),pairing.charAt(j+1),1);
}
return encrypt;
}
//Decryption
public static String decryptpf(String encrypted_message, String key) {
String decrypt="";
for(int j=0; j<=encrypted_message.length()-2; j+=2){
decrypt += find(encrypted_message.charAt(j),encrypted_message.charAt(j+1),-1);
}
decrypt = decrypt.replace("X","");
return decrypt;
}
public static void main(String args[]){
System.out.println("Enter your message: ");
Scanner sc = new Scanner(System.in);
String message = sc.nextLine();
//For multiple words
String words[] = message.split(" ");
String key,tempkey;
System.out.println("Enter your key: ");
tempkey = sc.next();
key = ""+tempkey.charAt(0);
//Removing repeating letters
for(int i=1; i<tempkey.length();i++){
if(!key.contains(""+tempkey.charAt(i)))
key += tempkey.charAt(i);
}
System.out.println("Key: "+key);
String encrypted_message = "";
for(int i=0; i<words.length; i++) {
encrypted_message += encryptpf(words[i],key) + " ";
}
System.out.println("Encrypted message: " + encrypted_message);
String encrypted_words[] = encrypted_message.split(" ");
String decrypted_message = "";
for(int i=0; i<encrypted_words.length; i++) {
decrypted_message += decryptpf(encrypted_words[i],key) + " ";
}
if(decrypted_message.equals(message+" "))
System.out.println("Decryption successful! Message is: "+decrypted_message);
else
System.out.println("Decryption unsuccessful. Check your key.");
}
}