-
Notifications
You must be signed in to change notification settings - Fork 1
/
playfair.js
298 lines (262 loc) · 7.86 KB
/
playfair.js
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
292
293
294
295
296
297
let isChet = false;
let isEnd = false;
let flag = false;
let flagX = false;
let flagAdd = false;
const form = document.forms[0];
const operationRadios = form.elements['operation'] //returns nodeRadioList
const inputField = document.getElementById('input')
const outputField = document.getElementById('output')
const keyField = document.getElementById('key')
inputField.addEventListener('input',runChosenOperation)
keyField.addEventListener('input',runChosenOperation)
for (const radio of operationRadios) radio.addEventListener("change",runChosenOperation)
function processKeyword(keyword) {
keyword = keyword.toUpperCase().replace(/\s/g, '').replace(/[^A-Z]/gi,"").replace(/J/g, "I")//remove spaces & allow alphabet only & replace every "J" with "I"
let key=""
for (const letter of keyword) {
if(key.indexOf(letter)===-1) key+= letter
}
return key
}
function generateMatrixValues(key){
let alphabet='ABCDEFGHIKLMNOPQRSTUVWXYZ'//alphabet without "J" (we assume that we will use I only, no J)
let matrix = [];
let temp = '';
for(let i = 0; i < key.length; i++) {
if (alphabet.indexOf(key[i]) !== -1) {
alphabet = alphabet.replace(key[i], '');
temp += key[i];
}
}
temp += alphabet;
temp = temp.split('');
while(temp[0]) {
matrix.push(temp.splice(0,5));
}
return matrix;
}
function encrypt(str, keyword) {
let key = processKeyword(keyword);
let keyMatrix = generateMatrixValues(key);
displayPlayfairMatrix(keyMatrix,key)
if(!str.length) return ""
let textPhrase, separator;
str = str.toUpperCase().replace(/\s/g, '').replace(/J/g, "I");
textPhrase = str[0];
let help = 0; flagAdd = false;
for(let i = 1; i < str.length; i++) {
if(str[i - 1] === str[i]) {
if(str[i] === 'X') {
separator = 'Q';
}
else {
separator = 'X';
}
textPhrase += separator + str[i];
help = 1;
}
else {
textPhrase += str[i];
}
if(help === 1) {
flagAdd = true;
}
}
if(textPhrase.length % 2 !== 0) {
if(textPhrase[textPhrase.length - 1] === 'X') {
textPhrase += 'Q';
isEnd = true;
flagX = false;
}
else {
textPhrase += 'X';
isEnd = true;
flagX = true;
}
}
let ciphertext = '';
for(let i = 0; i < textPhrase.length; i += 2){
let pair1 = textPhrase[i];
let pair2 = textPhrase[i + 1];
let p1i, p1j, p2i, p2j;
for(let stroka = 0; stroka < keyMatrix.length; stroka++) {
for(let stolbec = 0; stolbec < keyMatrix[stroka].length; stolbec++){
if (keyMatrix[stroka][stolbec] == pair1){
p1i = stroka;
p1j = stolbec;
}
if (keyMatrix[stroka][stolbec] == pair2){
p2i = stroka;
p2j = stolbec;
}
}
}
let coord1 = '', coord2 = '';
if(p1i === p2i) {
if(p1j === 4) {
coord1 = keyMatrix[p1i][0];
}
else {
coord1 = keyMatrix[p1i][p1j + 1];
}
if(p2j === 4) {
coord2 = keyMatrix[p2i][0];
}
else {
coord2 = keyMatrix[p2i][p2j + 1]
}
}
if(p1j === p2j) {
if(p1i === 4) {
coord1 = keyMatrix[0][p1j];
}
else {
coord1 = keyMatrix[p1i + 1][p1j];
}
if(p2i === 4) {
coord2 = keyMatrix[0][p2j];
}
else {
coord2 = keyMatrix[p2i + 1][p2j]
}
}
if(p1i !== p2i && p1j !== p2j) {
coord1 = keyMatrix[p1i][p2j];
coord2 = keyMatrix[p2i][p1j];
}
ciphertext = ciphertext + coord1 + coord2;
}
return ciphertext;
}
function decrypt(input, keyword) {
try {
let key = processKeyword(keyword);
let keyMatrix = generateMatrixValues(key)
displayPlayfairMatrix(keyMatrix, key)
let plaintext = '';
if(input === '') return ""
input = input.toUpperCase().replace(/\s/g, '').replace(/J/g, "I");
for(let i = 0; i < input.length; i += 2){
let pair1 = input[i];
let pair2 = input[i + 1];
let p1i, p1j, p2i, p2j;
for(let stroka = 0; stroka < keyMatrix.length; stroka++) {
for(let stolbec = 0; stolbec < keyMatrix[stroka].length; stolbec++){
if (keyMatrix[stroka][stolbec] == pair1){
p1i = stroka;
p1j = stolbec;
}
if (keyMatrix[stroka][stolbec] == pair2){
p2i = stroka;
p2j = stolbec;
}
}
}
let coord1 = '', coord2 = '';
if(p1i === p2i) {
if(p1j === 0) {
coord1 = keyMatrix[p1i][4];
}
else {
coord1 = keyMatrix[p1i][p1j - 1];
}
if(p2j === 0) {
coord2 = keyMatrix[p2i][4];
}
else {
coord2 = keyMatrix[p2i][p2j - 1]
}
}
if(p1j === p2j) {
if(p1i === 0) {
coord1 = keyMatrix[4][p1j]
}
else {
coord1 = keyMatrix[p1i - 1][p1j];
}
if(p2i === 0) {
coord2 = keyMatrix[4][p2j];
}
else {
coord2 = keyMatrix[p2i - 1][p2j]
}
}
if(p1i !== p2i && p1j !== p2j) {
coord1 = keyMatrix[p1i][p2j];
coord2 = keyMatrix[p2i][p1j];
}
plaintext = plaintext + coord1 + coord2;
}
plaintext = plaintext.split('');
for(let i = 0; i < plaintext.length; i++) {
let count;
if (flagAdd) {
if(plaintext[i] === plaintext[i + 2] && (plaintext[i + 1] === 'X' || plaintext[i + 1] === 'Q')) {
count = i + 1;
plaintext.splice(count, 1);
}
}
else if(flagAdd && isEnd && (flagX || !flagX)) {
if(plaintext[i - 2] === plaintext[i] && (plaintext[i - 1] === 'X' || plaintext[i - 1] === 'Q'))
count = i + 1;
plaintext.splice(count, 1);
}
else if(!flagAdd) {
break;
}
}
if(flagX) {
plaintext.pop();
}
if(isEnd && !flagX) {
plaintext.pop();
}
plaintext = plaintext.join('');
return plaintext
} catch (error) {
return ""
}
}
function displayPlayfairMatrix(keyMatrix, keyword){
let table = document.createElement('table')
let tableBody = document.createElement('tbody')
let cell, cellText
for (const row of keyMatrix) {
let tableRow = document.createElement('tr')
for (const letter of row) {
cell = document.createElement('td')
cellText = document.createTextNode(letter)
if(keyword.indexOf(letter)!==-1)
cell.classList.add('highlight')
cell.appendChild(cellText)
tableRow.appendChild(cell)
}
tableBody.appendChild(tableRow)
}
table.appendChild(tableBody)
table.classList.add('matrix')
const matrixDiv = document.getElementById('matrix-div')
matrixDiv.innerHTML =""
matrixDiv.append(table)
return table
}
function runChosenOperation(){
const keyword = keyField.value
const input = inputField.value
const operation = operationRadios.value
let output
if(output = operation==="encrypt"){
output=encrypt(input,keyword)
const inputLabel = document.querySelectorAll('.card-title')
inputLabel[0].innerText = "Plain text:"
inputLabel[1].innerText = "Cipher Text:"
}
else{
output=decrypt(input,keyword)
const inputLabel = document.querySelectorAll('.card-title')
inputLabel[0].innerText = "Cipher text:"
inputLabel[1].innerText = "Plain Text:"
}
outputField.innerText = output
}