-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathINS_P5.java
More file actions
161 lines (149 loc) · 4.5 KB
/
Copy pathINS_P5.java
File metadata and controls
161 lines (149 loc) · 4.5 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
155
156
157
158
159
160
161
package ins;
import java.util.Scanner;
/**
*
* @author Raj Dhanani
*/
class MatrixIndex {
int x, y;
public MatrixIndex(int x, int y) {
this.x = x;
this.y = y;
}
}
class PlayFair {
String plainText;
String cipherText;
String key;
char matrix[][] = new char[5][5];
public PlayFair(String plainText, String key) {
this.plainText = plainText;
this.key = removeSpaces(key).toLowerCase();
String a2z = "abcdefghiklmnopqrstuvwxyz";
String temp = eleminateRedundancy(this.key);
temp = eleminateRedundancy(temp + a2z);
initMatrix(temp);
encrypt();
}
String removeSpaces(String s) {
String temp = "";
for (int i = 0; i < s.length(); i++) {
if (s.charAt(i) != ' ') {
temp += s.charAt(i);
}
}
return temp;
}
String eleminateRedundancy(String s) {
String temp = "";
for (int i = 0; i < s.length(); i++) {
char x = s.charAt(i);
if (temp.indexOf(x) == -1) {
temp += x;
}
}
return temp;
}
void initMatrix(String s) {
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 5; j++) {
matrix[i][j] = s.charAt(i * 5 + j);
}
}
}
String addJunk(String plainText) {
String temp = plainText;
for (int i = 0; i < temp.length(); i += 2) {
char x1, x2;
x1 = temp.charAt(i);
if (i + 1 == temp.length()) {
if (temp.length() % 2 != 0) {
temp = temp + "" + (x1 == 'x' ? 'y' : 'x');
}
} else {
x2 = temp.charAt(i + 1);
if (x1 == x2) {
temp = temp.substring(0, i + 1) + (x1 == 'x' ? 'y' : 'x') + temp.substring(i + 1);
}
}
}
return temp;
}
MatrixIndex indexOf(char x) {
if (x == 'j') {
x = 'i';
}
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 5; j++) {
if (matrix[i][j] == x) {
return new MatrixIndex(i, j);
}
}
}
return null;
}
void encrypt() {
String temp = addJunk(removeSpaces(plainText)).toLowerCase();
MatrixIndex a, b;
char p1, p2, c1, c2;
cipherText = "";
for (int i = 0; i < temp.length(); i += 2) {
p1 = temp.charAt(i);//same row cange column
p2 = temp.charAt(i + 1);
a = indexOf(p1);
b = indexOf(p2);
if (a.x == b.x) {
c1 = matrix[a.x][(a.y + 1) % 5];
c2 = matrix[b.x][(b.y + 1) % 5];
} else if (a.y == b.y) {
c1 = matrix[(a.x + 1) % 5][a.y];
c2 = matrix[(b.x + 1) % 5][b.y];
} else {
c1 = matrix[a.x][b.y];
c2 = matrix[b.x][a.y];
}
cipherText += c1 + "" + c2;
}
}
String decrypt(String cipherText) {
String temp = cipherText.toLowerCase();
MatrixIndex a, b;
char p1, p2, c1, c2;
String pT = "";
for (int i = 0; i < temp.length(); i += 2) {
p1 = temp.charAt(i);//same row cange column
p2 = temp.charAt(i + 1);
a = indexOf(p1);
b = indexOf(p2);
if (a.x == b.x) {
c1 = matrix[a.x][((a.y - 1) % 5 + 5 ) % 5 ];
c2 = matrix[b.x][((b.y - 1) % 5 + 5 ) % 5 ];
} else if (a.y == b.y) {
c1 = matrix[((a.x - 1) % 5 + 5 ) % 5][a.y];
c2 = matrix[((b.x - 1) % 5 + 5 ) % 5][b.y];
} else {
c1 = matrix[a.x][b.y];
c2 = matrix[b.x][a.y];
}
pT += c1 + "" + c2;
}
return pT;
}
}
public class INS_P5 {
public static void main(String[] args) {
/*
Example:
ENTER PLAIN TEXT AND KEY (LINE SEPERATED):
committee
textile
CIPHER:aqkirdeiexxi
DECRYPTION:comxmitxteex
*/
System.out.println("ENTER PLAIN TEXT AND KEY (LINE SEPERATED):");
Scanner in = new Scanner(System.in);
PlayFair p = new PlayFair(in.nextLine(), in.nextLine());
System.out.println("CIPHER:" + p.cipherText);
System.out.println("DECRYPTION:" + p.decrypt(p.cipherText));
}
}