-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCCesar.html
81 lines (66 loc) · 2.37 KB
/
CCesar.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Cifrado César</title>
</head>
<body>
<h1>Cifrado César</h1>
<label for="opcion">Elige una opción:</label>
<select id="opcion">
<option value="1">Cifrar</option>
<option value="2">Descifrar</option>
</select>
<div>
<label for="frase">Ingresa una frase:</label>
<input type="text" id="frase">
</div>
<button onclick="cifrarDescifrar()">Ejecutar</button>
<div id="resultado"></div>
<script>
function codificar(letras, texto) {
let textoCodificado = '';
texto = texto.toUpperCase();
for (let i = 0; i < texto.length; i++) {
let caracter = texto.charAt(i);
let pos = letras.indexOf(caracter);
if (pos === -1) {
textoCodificado += caracter;
} else {
let nuevaPos = (pos + 5) % letras.length;
textoCodificado += letras.charAt(nuevaPos);
}
}
return textoCodificado;
}
function descodificar(letras, texto) {
let textoDescodificado = '';
texto = texto.toUpperCase();
for (let i = 0; i < texto.length; i++) {
let caracter = texto.charAt(i);
let pos = letras.indexOf(caracter);
if (pos === -1) {
textoDescodificado += caracter;
} else {
let nuevaPos = (pos - 5 + letras.length) % letras.length;
textoDescodificado += letras.charAt(nuevaPos);
}
}
return textoDescodificado;
}
function cifrarDescifrar() {
const letras = 'ABCDEFGHIJKLMNÑOPQRSTUVWXYZ';
const opcion = document.getElementById('opcion').value;
const frase = document.getElementById('frase').value;
let resultado = '';
if (opcion === '1') {
resultado = codificar(letras, frase);
} else if (opcion === '2') {
resultado = descodificar(letras, frase);
}
document.getElementById('resultado').textContent = 'Resultado: ' + resultado;
}
</script>
</body>
</html>