-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcipher.js
More file actions
26 lines (24 loc) · 890 Bytes
/
cipher.js
File metadata and controls
26 lines (24 loc) · 890 Bytes
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
const cipher = {
encode: function(clave, mensaje) {
let mensajeCifrado = '';
for (let i = 0; i < mensaje.length; i++) {
let charCode = mensaje.charCodeAt(i);
if (charCode >= 65 && charCode <= 90) { // mayúsculas
charCode = ((charCode - 65 + clave) % 26) + 65; // aplicar el desplazamiento
}
mensajeCifrado += String.fromCharCode(charCode);
}
return mensajeCifrado;
},
decode: function(clave, mensajeCifrado) {
let mensajeOriginal = '';
for (let i = 0; i < mensajeCifrado.length; i++) {
let charCode = mensajeCifrado.charCodeAt(i);
if (charCode >= 65 && charCode <= 90) { // mayúsculas
charCode = ((charCode - 65 - clave + 26) % 26) + 65; // deshacer el desplazamiento
}
mensajeOriginal += String.fromCharCode(charCode);
}
return mensajeOriginal;
}}
export default cipher