-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathplayfair.html
436 lines (220 loc) · 8.07 KB
/
playfair.html
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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
<!DOCTYPE html>
<html>
<head>
<link rel='stylesheet' href='style.css'/>
<script src='script.js'></script>
</head>
<body>
<h1>JavaScript Playfair Cipher</h1>
<p>Please enter your cipher key:</p>
<input id="cipherin" type="text">
<button type="button" onclick="ciphersquare()">Submit</button>
<p id="alphvalue"> </p>
<p id="ciphersquarevalues"></p>
<p id="ptable"></p>
<p>Now enter the text you want to encode:</p>
<input id="plaintextin" type="text">
<button type="button" onclick="encodectext()">Code!</button>
<p id="encoded">Your encoded text:</p>
<script>
function ciphersquare(cipher) {
//First, retrieve the value that was submitted:
var x = document.getElementById("cipherin");
var cipher = x.value;
//I create an array of the alphabet, minus the letter j. This is so I have 25 elements for my 5x5 cipher 'square'
var alph = ['a','b','c','d','e','f','g','h','i','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'];
//I then iterate through the alphabet array and remove all the letters that already occur in my cipher key
for(j = 0; j < cipher.length ; j++){
console.log("loop is executing");
for(i=0;i<alph.length;i++){
if (alph[i]==cipher[j]) {
alph.splice(i,1);
//document.getElementById("alphvalue").innerHTML ="For loop values"+ "j= "+ j +" i= " + i +"The value of alph: " +alph+ "\n";
} else {
}
}
}
//Here I need to check the input cipher for any letters that repeat themselves
//First I'll split cipher into an array so i can iterate through it.
var ptable=cipher.split("");
console.log("The initial value of ptable: " +ptable);
for (i=0;i<ptable.length;i++){
for(j=i+1;j<ptable.length;j++){
console.log("Loop two is executing"+ "i="+ i + "j= "+j);
console.log(ptable);
if (ptable[i]==ptable[j]){
ptable.splice(j,1);
console.log("Loop two is deleting values from cipher");
i=0;
j=0;
}
}
}
// Here i combine the ptable input cipher and the alphabet array to create my playfair cipher "square"
ptable=ptable.concat(alph);
//now make sure there aren't any 'j''s in our final array...if there are, it won't make it 5x5
for(i=0;i<ptable.length;i++){
if(ptable[i]=='j'){
ptable.splice(i,1);
}
}
console.log("Here you go: " + cipher);
console.log(ptable);
document.getElementById("ciphersquarevalues").innerHTML ="your cipher square is: ";
document.getElementById("ptable").innerHTML =ptable;
return ptable;
}
//we will use the following function to encode a block of text using the given cipher code
function encodectext(){
var x = document.getElementById("cipherin");
var cipher = x.value;
var y = document.getElementById("plaintextin");
var textin = y.value;
var textin = textin.split("");
// If there are an uneven amount of letters, append a 'z' to complete the last digraph
if ((textin.length%2)==1){
textin.push('z');
}else{
}
//Now check to see if there are two of the same letters in any given digraph, then in those digraphs change the second letter to an 'x'
for(i=0;i<textin.length;i=i+2){
if (textin[i]==textin[i+1]){
textin.splice(i+1,1,'x');
}
}
//now call the ciphersquare function to generate an array with the cipher square
var ptable = ciphersquare(cipher);
//make an array that records which digraphs have been changed(encoded) already
var chgrec=[];
//now for the hard parts: First, if the letters appear on the same row, replace them with the one directly to their right; wrapping around if needed
var textout=textin;
var iter=0;
for(l=0;l<textin.length-1;l=l+2){
var A=textin[l];
var B=textin[l+1];
for(i=0;i<25;i=i+5){
for(j=i;j<i+5;j++){
if(A == ptable[j]){
iter=iter+1;
console.log(A+" is in series "+ i+" through " + (i+4)+" : "+iter+" time.");
}
if(B == ptable[j]){
iter=iter+1;
console.log(B+" is in series "+ i+" through " + (i+4)+" : "+iter+" time.");
}
if(A == ptable[j]){
if(j==(i+4)){
var o=ptable[j-4];
}else{
var o=ptable[j+1];
}
}
if(B == ptable[j]){
if(j==(i+4)){
var t=ptable[j-4];
}else{
var t=ptable[j+1];
}
}
}
if(iter==2){
textout.splice(l,1,o);
textout.splice(l+1,1,t);
iter=0;
chgrec.push(l);
console.log("value of ptable: "+ ptable);
console.log("value of textout: "+textout);
console.log("Letters"+A+" and "+B+" are in same row, text out: "+textout);
}else{
iter=0;
console.log("Not in the same row: "+ A +" and "+ B);
}
}
}
//If the letter pair appears on the same column, replace them with the one immediately below, also wrapping if needed
for(l=0;l<textin.length-1;l=l+2){
var A=textin[l];
var B=textin[l+1];
for(i=0;i<5;i=i+1){
for(j=i;j<25;j=j+5){
if(A == ptable[j]){
iter=iter+1;
console.log(A+" is in Column "+ i+" : "+iter+" time.");
}
if(B == ptable[j]){
iter=iter+1;
console.log(B+" is in Column "+ i+" : "+iter+" time.");
}
if(A == ptable[j]){
if(j==(i+20)){
var o=ptable[j-20];
}else{
var o=ptable[j+5];
}
}
if(B == ptable[j]){
if(j==(i+20)){
var t=ptable[j-20];
}else{
var t=ptable[j+5];
}
}
}
if(iter==2){
textout.splice(l,1,o);
textout.splice(l+1,1,t);
iter=0;
chgrec.push(l);
console.log("value of ptable: "+ ptable);
console.log("value of textout: "+textout);
console.log("Letters"+A+" and "+B+" are in same column, text out: "+textout);
}else{
iter=0;
console.log("Not in the same comun: "+ A +" and "+ B);
}
}
}
//Finally,If the letter pair appeas in diffrent rows and collumns, replace each letter with the letter on the same row as itself, but on the column of the other letter
// I haven't been back to this in a while...this part still doesn't work as of 1/26/2016. Hmm. Well, I'll get there.
for(l=0;l<textin.length-1;l=l+2){
var A=textin[l];
var B=textin[l+1];
var ity=0;
var xyval=[0,0];
var mdiff=0;
for(i=0;i<chgrec.length;i=i+1){
if(l == chgrec[i]){
ity=ity+1;
}
}
if(ity==0){
for (j=0;j<ptable.length;j++){
if(A==ptable[j]){
xyval.splice(1,1,j);
}
if(B==ptable[j]){
xyval.splice(2,1,j);
}
}
if ((xyval[1]-xyval[2])>0){
mdiff=(xyval[1]-xyval[2])%5;
xyval.splice(1,1,(xyval[1]-mdiff));
xyval.splice(2,1,(xyval[2]+mdiff));
} else{
mdiff=Math.abs((xyval[1]-xyval[2]))%5;
xyval.splice(1,1,(xyval[1]+mdiff));
xyval.splice(2,1,(xyval[2]-mdiff));
}
textout.splice(l,1,ptable[xyval[1]]);
textout.splice(l+1,1,ptable[xyval[2]]);
}
}
//The following prints the encoded text to the browser:
document.getElementById("encoded").innerHTML =textout;
console.log("digraphs changed:"+ chgrec);
}
console.log("ptable: "+ ptable);
console.log("textin: "+ textin);
</script>
</body>
</html>