-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrf.js
More file actions
231 lines (208 loc) · 7.03 KB
/
rf.js
File metadata and controls
231 lines (208 loc) · 7.03 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
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
let btnE = document.querySelector('#encryptB')
let btnD = document.querySelector('#decryptB')
function init() {
btnE.addEventListener('click', encrypt);
btnD.addEventListener('click', decrypt);
}
function encrypt(event){
event.preventDefault();
var key = document.querySelector("#key").value;
key = parseInt(key, 10);
var plain = document.querySelector("#ptext").value;
var down = true;
var row = 0;
//Creates the 2D Array depending on size of plaintext & key.
var array = Array.from(Array(plain.length), () => new Array(key))
// Fills array with the plaintext
for(var i = 0; i < array.length; i++){
// Checks for down direction.
if(down === true){
//prints character
array[i][row] = plain[i];
//Checks for the bottom row.
if(row === (key-1)){
//redirect
var down = false;
//sets to the above row.
row--;
}else{
//sets to the bellow row.
row++;
}
}
// Checks for up direction.
else if(down === false){
//prints character
array[i][row] = plain[i];
//Checks if back at the top.
if(row === 0){
//redirect
var down = true;
//sets to the bellow row.
row++;
}else{
//sets to the above row.
row--;
}
}
}
//final array will hold the cipher text.
var final = new Array(plain.length);
var counter = 0;
row = 0;
//Stays on the same column level while searching the entire row.
while(row < key){
for(let i = 0; i < plain.length; i++){
//if the address is not empty, save to final array.
if(array[i][row] !== undefined){
let x = array[i][row];
final[counter] = x.toString();
counter++;
}
}
row++;
}
// Changes final array into a string and removes commas.
final = final.toString();
final = final.replace(/,/g, '')
document.querySelector("#ctext").value = final;
}
function decrypt(event) {
event.preventDefault();
var key = document.querySelector("#key").value;
key = parseInt(key, 10);
var ctext = document.querySelector("#ctext").value;
var down = true;
var row = 0;
var t = 0;
var counter = 0;
// Creates the 2D Array depending on size of plaintext & key.
var array = Array.from(Array(ctext.length), () => new Array(key))
//fills array with stars *
for(var i = 0; i < ctext.length; i++){
for(let j = 0; j < key; j++){
array[i][j] = '*';
}
}
while(counter !== ctext.length){ // while loop closes if counter equals the cipher text
// Fills top row only in array with the cipher text, except the last time.
for(var i = 0; i < array.length; i++){
if(row === t){ // Checks if at the affected row.
// prints character
array[i][row] = ctext[counter];
counter++;
}
else{
if(array[i][row] === '*'){
array[i][row] = "-";
}
}
// Checks for down direction.
if(down === true){
// If not the bottom row.
if(row !== (key-1)){
row++; //sets to the below row.
}
else{ // If bottom
//redirect
var down = false;
row--; //sets to the above row.
}
}
// If up direction.
else{
// If not the top row.
if(row !== 0){
row--; //sets to the above row.
}
else{ // If top
down = true;
row++; //sets to the below row.
}
}
}
t++; //Shifts the top, one row lower.
// Last Print Run (top = bottom)
if(t === key-1){
row = 0;
down = true;
i = 0;
// Fills array with the cipher text for last row.
for(var i = 0; i < array.length; i++){
// Checks for down direction.
if(down === true){
//Checks for last empty slots
if(array[i][row] === "-"){
//Continues writing out the cipher text.
array[i][row] = ctext[counter];
counter++;
//top = bottom, so index is at the bottom.
down = false;
row--; //sets to the above row.
}else{
row++; //sets to the bellow row.
}
}
//Checks for up direction.
else{
//Checks if back at the first row.
if(row === 0){
var down = true; //redirect
row++; //sets to the bellow row.
}else{
row--; //sets to the above row.
}
}
}
}
else{ // Reset
row = 0;
down = true;
i = 0;
}
} // while loop closes if counter equals the cipher text
// Final array picking up the plain text.
var final = new Array(ctext.length);
counter = 0;
row = 0;
t = 0;
down = true;
//Reads diagonally
for(var i = 0; i < array.length; i++){
// Checks for down direction.
if(down === true){
if(array[i][row] !== '-' || array[i][row] !== '*'){
//puts character into final array.
final[counter] = array[i][row];
counter++;
}
if(row !== (key-1)){ // If not bottom
row++; //set to the below row.
}
else{ // If bottom
var down = false; //redirect
row--; //sets to the above row.
}
}
// Checks for up direction.
else{
if(array[i][row] !== '-' || array[i][row] !== '*'){
//puts character into final array.
final[counter] = array[i][row];
counter++;
}
if(row !== 0){ // If not top
row--; //sets to the above row.
}else{ // If top
//redirect
var down = true;
row++; //sets to the bellow row.
}
}
}
// Changes final array into a string and removes commas.
final = final.toString();
final = final.replace(/,/g, '')
document.querySelector("#ptext").value = final;
}
document.addEventListener('DOMContentLoaded', init);