-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPlayfair.java
291 lines (245 loc) · 8.56 KB
/
Playfair.java
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
package com.company;
import java.util.LinkedHashSet;
import java.util.Scanner;
import java.util.Set;
//KOYQKOYQFI
public class Playfair
{
static String alphabets = "ABCDEFGHIKLMNOPQRSTUVWXYZ";
static char [][] matrix = new char[5][5];
//Encryption
static void encrypt(char [][] matrix, String input) {
//System.out.println(("Matrix Received")+matrix[0][0]);
String output = input;
int f = 0;
//separate duplicate letters with X
StringBuffer str = new StringBuffer(output);
for (int a = 0; a < input.length(); a++) {
if (a + 1 > input.length() - 1) {
break;
}
if (input.charAt(a) == input.charAt(a + 1)) {
f = f + a + 1;
str.insert(f, 'X');
f = f - 1;
}
}
System.out.println("New String: " + str);
StringBuffer cipher_string = new StringBuffer();
char cipher_char1=' ';
char cipher_char2=' ';
//Even length (No padding required)
if (input.length() % 2 == 0) {
//Directly move forward
}
//Odd Length (Padding Required)
else {
str = str.append('X');
System.out.println("Input padded is " + str);
}
for(int p=0;p<str.length();p+=2)
{
char current_char = str.charAt(p);
char next_char = str.charAt(p+1);
System.out.println("Pair: "+current_char+ " and "+next_char);//check
int current_row = position(current_char, matrix, "row");
int next_row = position(next_char, matrix, "row");
int current_col = position(current_char, matrix, "column");
int next_col = position(next_char, matrix, "column");
//Same row, move to right
if(current_row == next_row)
{
//increment column by 1 and wrap
if(current_col==4)
{
cipher_char1 = matrix[current_row][0];
}
else if(next_col==4)
{
cipher_char2 = matrix[next_row][0];
}
else
{
cipher_char1 = matrix[current_row][current_col+1];
cipher_char2 = matrix[next_row][next_col+1];
}
cipher_string.append(cipher_char1);
cipher_string.append(cipher_char2);
}
//Same column, move down
else if(current_col == next_col)
{
if(current_row==4)
{
cipher_char1 = matrix[current_col][0];
}
else if(next_row==4)
{
cipher_char2 = matrix[next_col][0];
}
else
{
cipher_char1 = matrix[current_row+1][current_col];
cipher_char2 = matrix[next_row+1][next_col];
}
cipher_string.append(cipher_char1);
cipher_string.append(cipher_char2);
}
//Rectangle: Take opposite sides
else
{
cipher_char1 = matrix[current_row][next_col];
cipher_char2 = matrix[next_row][current_col];
cipher_string.append(cipher_char1);
cipher_string.append(cipher_char2);
}
}
System.out.println("Encrypted Text: "+cipher_string);
}
static int position(char c, char [][] matrix, String pos)
{
int output=0;
for(int i=0;i < 5;i++)
{
for(int j=0;j < 5;j++)
{
if(matrix[i][j]==c)
{
if(pos == "row")
output =i;
else
output =j;
}
}
}
return output;
}
//Decryption
public static void decrypt(char [][] matrix, String input)
{
StringBuffer str = new StringBuffer(input);
StringBuffer plain_string = new StringBuffer();
char plain_char1=' ';
char plain_char2=' ';
//System.out.println("Matrix Received" + Arrays.deepToString(matrix));
for(int p=0;p<str.length();p+=2)
{
char current_char = str.charAt(p);
char next_char = str.charAt(p+1);
System.out.println("Pair: "+current_char+ " and "+next_char);//check
int current_row = position(current_char, matrix, "row");
int next_row = position(next_char, matrix, "row");
int current_col = position(current_char, matrix, "column");
int next_col = position(next_char, matrix, "column");
//Same row, move to left
if(current_row == next_row)
{
//increment column by 1 and wrap
if(current_col==0)
{
plain_char1 = matrix[current_row][4];
}
else if(next_col==0)
{
plain_char2 = matrix[next_row][4];
}
else
{
plain_char1 = matrix[current_row][current_col-1];
plain_char2 = matrix[next_row][next_col-1];
}
plain_string.append(plain_char1);
plain_string.append(plain_char2);
}
//Same column, move up
else if(current_col == next_col)
{
if(current_row==0)
{
plain_char1 = matrix[current_col][4];
}
else if(next_row==0)
{
plain_char2 = matrix[next_col][4];
}
else
{
plain_char1 = matrix[current_row-1][current_col];
plain_char2 = matrix[next_row-1][next_col];
}
plain_string.append(plain_char1);
plain_string.append(plain_char2);
}
//Rectangle: Take opposite sides
else
{
plain_char1 = matrix[current_row][next_col];
plain_char2 = matrix[next_row][current_col];
plain_string.append(plain_char1);
plain_string.append(plain_char2);
}
}
System.out.println("Decrypted Text with Xs: "+plain_string);
//Remove all Xs (Assuming message has no X)
String new_string = plain_string.toString();
String print_string = new_string.replaceAll("X","");
System.out.println("Decrypted Text without Xs: "+print_string);
}
//Main
public static void playfair_main()
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter Keyword:");
Scanner scc = new Scanner(System.in);
System.out.println("This is Playfair Cipher.\nEnter keyword: ");
String keyword = scc.nextLine();
System.out.println("Keyword is "+ keyword);
//Make a string for key matrix
String intermediate_str = keyword + alphabets;
//Remove duplicates from intermediate string for key matrix string
StringBuilder sb = new StringBuilder();
char[] chars = intermediate_str.toCharArray();
Set<Character> charSet = new LinkedHashSet<Character>();
for (char c : chars) {
charSet.add(c);
}
for (Character character : charSet) {
sb.append(character);
}
System.out.println("Matrix is : "+sb.toString());
//sb contains matrix
System.out.println("Key Matrix:");
//Make and display matrix
String matrix_str = sb.toString();
int s=0;
for(int j=0;j<5;j++)
{
for(int k=0;k<5;k++)
{
matrix[j][k] = matrix_str.charAt(s);
System.out.print(" "+matrix[j][k]+" ");
s++;
}
System.out.println("\n");
}
//Input
System.out.println("Enter Input: ");
String input = scc.nextLine();
input = input.replaceAll("\\s","");
//Encryption or decryption
System.out.println("1. Encryption\n2. Decryption\n");
int choice = scc.nextInt();
if(choice==1)
{
encrypt(matrix, input);
}
else if(choice==2)
{
decrypt(matrix, input);
}
else
{
System.out.println("Please enter valid choice");
}
}
}