-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdecifrarASCII.js
51 lines (36 loc) · 1.61 KB
/
decifrarASCII.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
document.addEventListener("DOMContentLoaded", function() {
//Ao clicar no botão o evento é ativado
document.querySelector("#botao").addEventListener("click", function() {
//Lista com caracteres printaveis da codificação ASCII
const ASCII = [];
for(let i = 32; i < 127; i++) { //captura espaço como primeiro elemento e inclui pontuações, números e letras
ASCII.push(String.fromCharCode(i));
}
//Mapeia chave para codificação ASCII
const chave = 'abc';
const chaveMapeada = [];
for (let j = 0; j < chave.length; j++) {
if (ASCII.some(item => chave.includes(item))) {
chaveMapeada.push(ASCII.indexOf(chave[j]));
}
}
//Mapeia input de acordo com alfabeto
let texto = document.querySelector("#mensagem").value;
let textoMapeado = [];
for (let j = 0; j < texto.length; j++) {
if (ASCII.some(item => texto.includes(item))) {
textoMapeado.push(ASCII.indexOf(texto[j]));
}
}
//Decifra mensagem
let decifradoMapeado = [];
let decifrado = [];
for (let k = 0; k < textoMapeado.length; k++) {
decifradoMapeado[k] = (textoMapeado[k] + ASCII.length - chaveMapeada[k%chaveMapeada.length]) % ASCII.length;
decifrado.push(ASCII[decifradoMapeado[k]]);
}
//Exibe o output decifrado
var textoDecifrado = document.querySelector("#resposta");
textoDecifrado.textContent = decifrado.join('');
});
});