-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPlayfair.java
More file actions
183 lines (160 loc) · 4.89 KB
/
Copy pathPlayfair.java
File metadata and controls
183 lines (160 loc) · 4.89 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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
import java.util.Scanner;
import sun.reflect.ReflectionFactory.GetReflectionFactoryAction;
public class Playfair {
private static char[][] alphaMatrix;
private static String key;
private static String message;
private static Scanner scanner;
private static String encryptedMessage = "";
public static int getRow(char letter, char[][] alphaMatrix) {
int row = -1;
for (int i = 0; i<5; i++) {
for (int j =0; j<5; j++ ) {
if (alphaMatrix[i][j]==letter) {
row = i;
}
}
}
return row;
}
public static int getColumn(char letter, char[][] alphaMatrix) {
int column = -1;
for (int i = 0; i<5; i++) {
for (int j =0; j<5; j++ ) {
if (alphaMatrix[i][j]==letter) {
column = j;
}
}
}
return column;
}
public static void main (String[] args) {
System.out.println("Welcome");
scanner = new Scanner(System.in);
System.out.println("Enter your key:");
key = scanner.nextLine();
key = key.replaceAll("\\s","").trim().toUpperCase();
String temp = "";
for (char letter:key.toCharArray()) {
if (!(temp.indexOf(letter)>=0)) {
temp+=letter;
}
}
key = temp;
alphaMatrix = new char[5][5];
int counter = 0;
char symbol;
//create a string then transform to matrix
while (key.toCharArray().length != 26) {
symbol = (char) ('A' + counter);
if ((key.indexOf(symbol)<0) && (symbol!= 'Q') ) {
key +=symbol;
}
symbol = (char) ('A' + counter++);
}
System.out.println(key);
//transform a string to matrix
for (int i = 0, k =0; i<5; i++) {
for (int j =0; j<5; j++) {
alphaMatrix[i][j] = key.toCharArray()[k];
k = ++k % key.length();
}
}
//now the matrix is ready
for (int i = 0; i<5; i++) {
for (int j =0; j<5; j++ ) {
System.out.print(alphaMatrix[i][j] + " ");
}
System.out.println();
}
System.out.println("Enter your message: ");
message = scanner.nextLine();
message = message.replaceAll("\\s","").trim().toUpperCase();
//replace the repeating symbols
for (int i =0; i < message.length()-1; i++) {
if (message.
toCharArray()[i]==
message.toCharArray()[i+1]) {
message = message.substring(0,i+1)
+ 'X' + message.substring(i+1,message.length());
}
}
if (message.length()%2 !=0) {
message +='Z';
}
System.out.println(message);
//start working with letter pairs
//if the letters get columns and getrows correspond...
//either...
for (int i =0; i< message.length(); i++) {
System.out.println(message.
toCharArray()[i] + "~" +
message.toCharArray()[i+1]);
if (getColumn(message.
toCharArray()[i], alphaMatrix) == getColumn(message.
toCharArray()[i+1], alphaMatrix)) {
System.out.println("The columns are equal");
if (getRow(message.
toCharArray()[i], alphaMatrix) != 4) {
encryptedMessage +=alphaMatrix[getRow(message.
toCharArray()[i], alphaMatrix)+1]
[getColumn(message.
toCharArray()[i], alphaMatrix)];
} else {
encryptedMessage +=alphaMatrix[0]
[getColumn(message.
toCharArray()[i], alphaMatrix)];
}
if (getRow(message.
toCharArray()[i+1], alphaMatrix) != 4) {
encryptedMessage +=alphaMatrix[getRow(message.
toCharArray()[i+1], alphaMatrix)+1]
[getColumn(message.
toCharArray()[i+1], alphaMatrix)];
} else {
encryptedMessage +=alphaMatrix[0]
[getColumn(message.
toCharArray()[i+1], alphaMatrix)];
}
}else if (getRow(message.
toCharArray()[i], alphaMatrix) == getRow(message.
toCharArray()[i+1], alphaMatrix)) {
System.out.println("The rows are equal");
if (getColumn(message.
toCharArray()[i], alphaMatrix) != 4) {
encryptedMessage +=alphaMatrix[getRow(message.
toCharArray()[i], alphaMatrix)]
[getColumn(message.
toCharArray()[i], alphaMatrix)+1];
} else {
encryptedMessage +=alphaMatrix[getRow(message.
toCharArray()[i], alphaMatrix)]
[0];
}
if (getRow(message.
toCharArray()[i+1], alphaMatrix) != 4) {
encryptedMessage +=alphaMatrix[getRow(message.
toCharArray()[i+1], alphaMatrix)]
[getColumn(message.
toCharArray()[i+1], alphaMatrix)+1];
} else {
encryptedMessage +=alphaMatrix[getRow(message.
toCharArray()[i+1], alphaMatrix)]
[0];
}
} else {
System.out.println("Neither");
encryptedMessage +=alphaMatrix[getRow(message.
toCharArray()[i], alphaMatrix)]
[getColumn(message.
toCharArray()[i+1], alphaMatrix)];
encryptedMessage +=alphaMatrix[getRow(message.
toCharArray()[i+1], alphaMatrix)]
[getColumn(message.
toCharArray()[i], alphaMatrix)];
}
i++;
}
System.out.println(encryptedMessage);
}
}