-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
108 lines (100 loc) · 3.94 KB
/
index.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
var selectType = document.querySelector('#selectType');
var idQttDiv = document.querySelector('#idQttDiv');
var btnDiv = document.querySelector('#btnDiv');
var selection = document.querySelector('#selectType');
var encode = document.querySelector('#codeId');
var decode = document.querySelector('#decodeId');
encode.addEventListener('click', function () {
if (encode.checked == true) {
btnDiv.innerHTML = '<input class="btnEnviar" id="btnEnviar" type="submit" value="Codificar">';
}
})
decode.addEventListener('click', function () {
if (decode.checked == true) {
btnDiv.innerHTML = '<input class="btnEnviar" id="btnEnviar" type="submit" value="Decodificar">';
}
})
selectType.addEventListener('click', function () {
if (selectType.value == 'cifra') {
idQttDiv.style.display = 'flex';
} else {
idQttDiv.style.display = 'none';
}
})
var btn = document.querySelector('#btnDiv');
btn.addEventListener('click', function (e) {
e.preventDefault();
if (document.getElementById('decodeId').checked) {
var code = document.getElementById('decodeId').value;
} else if (document.getElementById('codeId').checked) {
var code = document.getElementById('codeId').value;
} else {
alert('Selecione a opção Codificar ou Decodificar')
}
var text = document.querySelector('#text').value;
var passo = document.querySelector('#quantity').value;
var selection = document.querySelector('#selectType').value;
if ((selection == 'Base64') && (code == 'encode')) {
encodeBase64(text);
} else if ((selection == 'Base64') && (code == 'decode')) {
decodeBase64(text);
} else if (selection == 'cifra' && (Number(passo) > 0) && (code == 'encode')) {
cifraEncode(text, passo);
} else if (selection == 'cifra' && (Number(passo) > 0) && (code == 'decode')) {
cifraDecode(text, passo);
} else if ((selection == 'empty') || (passo == 0)) {
alert('Preencha todos os campos');
}
})
function encodeBase64(text) {
var result = document.querySelector('#textResult');
outputText = btoa(text);
result.innerText = outputText;
}
function decodeBase64(text) {
var result = document.querySelector('#textResult');
outputText = atob(text);
result.innerText = outputText;
}
function cifraEncode(text, passo) {
var outputText = '';
var adjust = 0;
var result = document.querySelector('#textResult');
for (var i = 0; i < text.length; i++) {
if ((text.charCodeAt(i) >= 65 && text.charCodeAt(i) <= 90)) {
adjust = (Number(text.charCodeAt(i))) - 65;
adjust = (adjust + (Number(passo))) % 26;
adjust += 65;
outputText += String.fromCharCode(adjust);
} else if (text.charCodeAt(i) >= 97 && text.charCodeAt(i) <= 122) {
adjust = (Number(text.charCodeAt(i))) - 97;
adjust = (adjust + (Number(passo))) % 26;
adjust += 97;
outputText += String.fromCharCode(adjust);
} else {
outputText += String.fromCharCode(text.charCodeAt(i));
}
}
result.innerText = outputText;
}
function cifraDecode(text, passo) {
var outputText = '';
var adjust = 0;
var result = document.querySelector('#textResult');
for (var i = 0; i < text.length; i++) {
if ((text.charCodeAt(i) >= 65 && text.charCodeAt(i) <= 90)) {
adjust = (Number(text.charCodeAt(i))) - 65;
adjust = (adjust - (Number(passo - 26))) % 26;
adjust += 65;
outputText += String.fromCharCode(adjust);
} else if (text.charCodeAt(i) >= 97 && text.charCodeAt(i) <= 122) {
adjust = (Number(text.charCodeAt(i))) - 97;
adjust = (adjust - (Number(passo - 26))) % 26;
adjust += 97;
outputText += String.fromCharCode(adjust);
} else {
outputText += String.fromCharCode(text.charCodeAt(i));
}
}
result.innerText = outputText;
}