-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathencriptador.js
More file actions
168 lines (146 loc) · 5.42 KB
/
Copy pathencriptador.js
File metadata and controls
168 lines (146 loc) · 5.42 KB
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
var boton_encriptar = document.querySelector('.boton_encriptar');
var boton_desencriptar = document.querySelector('.boton_desencriptar');
var boton_copiar = document.querySelector('.boton_copiar');
const texto_copiado = document.querySelector('.output_text');
/* Evento para boton encriptar y funcionalidades*/
boton_encriptar.addEventListener('click', function(event) {
var texto_encriptar = document.getElementById("entrada_texto").value;
if (validador_requisitos(texto_encriptar)) {
if (modificador_display(texto_encriptar.length)) {
var salida_texto = encriptador(texto_encriptar);
document.getElementById("salida_texto").value=salida_texto;
}
}
});
/* Evento para boton desencriptar y funcionalidades*/
boton_desencriptar.addEventListener('click', function(event) {
var texto_encriptar = document.getElementById("entrada_texto").value;
if (validador_requisitos(texto_encriptar)) {
if (modificador_display(texto_encriptar.length)) {
var salida_texto = desencriptador(texto_encriptar);
document.getElementById("salida_texto").value=salida_texto;
}
}
});
/* Evento para boton copiar y funcionalidades*/
boton_copiar.addEventListener('click', function(event) {
texto_copiado.select();
texto_copiado.setSelectionRange(0, 99999);
navigator.clipboard.writeText(texto_copiado.value);
texto_copiado.setSelectionRange(0,0);
/*document.execCommand('copy');*/
});
/**
* Funcion para validar requisitos
* @param {*} texto texto a validar
* Los requisitos:
* - Debe funcionar solo con letras minúsculas
* - No deben ser utilizados letras con acentos ni caracteres especiales
* Se logra a traves de los codigos ASCII
* @returns true si cumple los requisitos - false si no lo hace
*/
function validador_requisitos(texto) {
for (var i=0; i<texto.length; i++) {
if (!(texto.charCodeAt(i)>= 97 && texto.charCodeAt(i)<=122 || texto.charCodeAt(i)==32 || texto.charCodeAt(i)==10)) {
alert("Debe ingresar solo letras minusculas, sin acentos ni caracteres especiales")
return false;
}
}
return true;
}
/**
* Funcion para cambiar display de los div dentro del div (class="salida encriptador")
* @param {*} texto_lenght recibe un largo de un texto para ver si esta vacio o no
* @returns true si hay texto - false si no hay, cambiando displays correspondientes
*/
function modificador_display(texto_lenght) {
if (texto_lenght==0) {
document.getElementById("ocultar_encriptado").style.display="none";
document.getElementById("ocultar_vacio").style.display="block";
return false;
} else {
document.getElementById("ocultar_encriptado").style.display="block";
document.getElementById("ocultar_vacio").style.display="none";
return true;
}
}
/**
* Funcion para encriptar texto
* @param {*} texto Recibe el texto deseado a encriptar
* La letra "e" es convertida para "enter"
* La letra "i" es convertida para "imes"
* La letra "a" es convertida para "ai"
* La letra "o" es convertida para "ober"
* La letra "u" es convertida para "ufat"
* @returns texto1, es el parametro ya encriptado
*/
function encriptador(texto) {
var texto_encriptado = "";
for (var i = 0; i < texto.length; i++) {
switch (texto[i]) {
case 'a':
texto_encriptado +='ai';
break;
case 'e':
texto_encriptado +='enter';
break;
case 'i':
texto_encriptado +='imes';
break;
case 'o':
texto_encriptado +='ober';
break;
case 'u':
texto_encriptado +='ufat';
break;
default:
texto_encriptado += texto[i];
}
}
return texto_encriptado;
}
/*TODO: mejorar logica de desencriptado, no funcionan textos largos*/
/**
* Funcion para desencriptar texto
* @param {*} texto Recibe el texto deseado a desencriptar
* revierte lo convertido en la funcion encriptador
* @returns texto que ya estara desencriptado
*/
function desencriptador(texto) {
for (var i = 0; i < 4; i++) {
texto = texto.replace("ai","a");
texto = texto.replace("enter","e");
texto = texto.replace("imes","i");
texto = texto.replace("ober","o");
texto = texto.replace("ufat","u");
}
for (var i = 0; i < 4; i++) {
texto = texto.replace("ai","a");
texto = texto.replace("enter","e");
texto = texto.replace("imes","i");
texto = texto.replace("ober","o");
texto = texto.replace("ufat","u");
}
for (var i = 0; i < 4; i++) {
texto = texto.replace("ai","a");
texto = texto.replace("enter","e");
texto = texto.replace("imes","i");
texto = texto.replace("ober","o");
texto = texto.replace("ufat","u");
}
for (var i = 0; i < 4; i++) {
texto = texto.replace("ai","a");
texto = texto.replace("enter","e");
texto = texto.replace("imes","i");
texto = texto.replace("ober","o");
texto = texto.replace("ufat","u");
}
for (var i = 0; i < 4; i++) {
texto = texto.replace("ai","a");
texto = texto.replace("enter","e");
texto = texto.replace("imes","i");
texto = texto.replace("ober","o");
texto = texto.replace("ufat","u");
}
return texto
}