-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcipher.js
47 lines (38 loc) · 1.06 KB
/
cipher.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
const cipher = {
code,
decode
};
//FUNCIÓN DE CIFRADO
function code (text1, displace1) {
let result1 = "";
const letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
displace1 = (displace1 % 26 + 26) % 26;
if (text1) {
for (let i = 0; i < text1.length; i++) {
if (letters.indexOf(text1[i])!=-1) {
let pos = ((letters.indexOf(text1[i]) + displace1) % 26);
result1 += letters [pos];
}
else
result1 += text1[i];
}
}
return result1;
}
//FUNCIÓN DE DESCIFRADO
function decode (text2, displace2) {
let result2 = "";
const letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
displace2 = (displace2 % 26 - 26) % 26;
if (text2) {
for (let i = 0; i < text2.length; i++) {
if (letters.indexOf(text2[i])!=-1) {
let pos = ((letters.indexOf(text2[i]) - displace2) % 26);
result2 += letters [pos];
}
else result2 += text2[i];
}
}
return result2;
}
export default cipher;