-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
117 lines (98 loc) · 4.32 KB
/
script.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
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
// Script do select option / Quando selecionar o tipo de criptografia o nome do botão muda
const btnSubmit = document.getElementById("Input_Submit");
const inputCriptografar = document.getElementById("Criptografar");
const inputDescriptografar = document.getElementById("Descriptografar");
let escolherAlgoritimo = document.getElementById("SelecionarAlgoritimo");
let cifraIncrementar = document.getElementById("Incremento");
let alfabeto = "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz"
inputCriptografar.addEventListener('click', function (){
btnSubmit.style.display = 'inline';
btnSubmit.innerText = "Criptografar";
})
inputDescriptografar.addEventListener('click', function (){
btnSubmit.style.display = 'inline';
btnSubmit.innerText = "Descriptografar";
})
// Script do incremento / Quando selecionar Cifra de César o contador aparece no display
escolherAlgoritimo.addEventListener('change', function (){
if (escolherAlgoritimo.value == 'vCifra') {
cifraIncrementar.style.display = 'block';
} else if (escolherAlgoritimo.value == 'vBase') {
cifraIncrementar.style.display = 'none';
}
})
// Script para codificar Base 64
function codificarBase64() {
let vTextArea = document.getElementById("textareaEscreva").value;
let vTextInput = btoa(vTextArea);
let vTextOutput = document.getElementById("textareaCopie");
vTextOutput.innerText = vTextInput
}
// Script para descodificar Base 64
function descodificarBase64(){
let vTextArea = document.getElementById("textareaEscreva").value;
let vTextInput = atob(vTextArea);
let vTextOutput = document.getElementById("textareaCopie");
vTextOutput.innerText = vTextInput
}
// Script para codificar Cifra de César
function codificarCifra(){
let cifraIncrementar = document.getElementById("cifraIncremento").value;
let vTextArea = document.getElementById("textareaEscreva").value;
let vTextOutput = document.getElementById("textareaCopie");
let transformMinusculo = vTextArea.toLowerCase();
let transformNumero = (Number(cifraIncrementar) % 26);
let vTextOutputCodificado = "";
for (let i = 0; i < transformMinusculo.length; i++){
for (let x = 0; alfabeto.length; x++){
if (transformMinusculo[i] == alfabeto [x]){
vTextOutputCodificado += alfabeto [x + transformNumero]
break;
} else if (transformMinusculo[i] == " ") {
vTextOutputCodificado += " ";
break;
}
}
}
vTextOutput.innerText = vTextOutputCodificado
}
// Script para descodificar Cifra de César
function descodificarCifra(){
let cifraIncrementar = document.getElementById("cifraIncremento").value;
let vTextArea = document.getElementById("textareaEscreva").value;
let vTextOutput = document.getElementById("textareaCopie");
let transformMinusculo = vTextArea.toLowerCase();
let transformNumero = (Number(cifraIncrementar) % 26);
let vTextOutputCodificado = '';
for (let i = 0; i < transformMinusculo.length; i++){
for (let x = alfabeto.length -1; x >= 0; x--){
if (transformMinusculo[i] == alfabeto [x]){
vTextOutputCodificado += alfabeto [x - transformNumero]
break;
} else if (transformMinusculo[i] == ' ') {
vTextOutputCodificado += ' ';
break;
}
}
}
vTextOutput.innerText = vTextOutputCodificado
}
// Script do butão de enviar
function btnEnviar () {
let btnSubmit = document.getElementById("Input_Submit");
let escolherAlgoritimo = document.getElementById("SelecionarAlgoritimo");
const inputCriptografar = document.getElementById("Criptografar");
const inputDescriptografar = document.getElementById("Descriptografar");
event.preventDefault()
if (inputCriptografar.checked && escolherAlgoritimo.value == "vBase"){
codificarBase64();
} else if (inputCriptografar.checked && escolherAlgoritimo.value == "vCifra") {
codificarCifra();
} else if (inputDescriptografar.checked && escolherAlgoritimo.value == "vBase") {
descodificarBase64();
} else if (inputDescriptografar.checked && escolherAlgoritimo.value == "vCifra") {
descodificarCifra();
} else {
alert ("Escolha uma das opções para Criptografar")
}
}