Skip to content
This repository was archived by the owner on Jan 8, 2020. It is now read-only.

Cifrado Cesar - Andrea Chumioque #68

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"parserOptions": {
"ecmaVersion": 6
},
"rules": {
"keyword-spacing": 1,
"space-before-function-paren": [1, "never"],
"eqeqeq": 1,
"space-infix-ops": 1,
"comma-spacing": 1,
"brace-style": 1,
"no-multiple-empty-lines": 1,
"camelcase": 1,
"func-call-spacing": 1,
"key-spacing": 1,
"semi": 1,
"no-floating-decimal": 1,
"no-multi-spaces": 1,
"object-property-newline": 1,
"padded-blocks": [1, "never"],
"space-before-blocks": 1,
"space-in-parens": 1,
"spaced-comment": 1,
"quotes": [1, "single"],
"id-length": [1, { "exceptions": ["i", "j", "x"] }],
"indent": [1, 2],
"no-array-constructor": 1
}
}
Binary file added assets/docs/flow-diagram-caesar-cipher.jpeg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
12 changes: 12 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<script type="text/javascript" src="js/app.js"></script>
</body>
</html>
88 changes: 88 additions & 0 deletions js/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
// Creando funcion Cifrado Cesar
function cipher(msg, x) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Segun las especificaciones, el usuario no debe poder ingresar numeros. Trate de poner numeros en mi mensaje y si pude. Favor de actualizar el code para que no pueda ingresar numeros en el mensaje

Creo que no es necesario pasar el variable x.
Solo llamas esta function una vez y le das un valor de 33.

// Creando variables
var origCode = 0;
var newCode = 0;
var newLetter = '';
var cipherMsg = '';

// Recorriendo el string de izquierda a derecha
for (var i = 0; i < msg.length; i++) {
// Encontramos el código de cada caracter con el método charCodeAt
origCode = msg.charCodeAt(i);
// Si el caracter es una letra mayúscula o minúscula (entre los códigos 65 y 90 o 97 y 122),
// realiza el cifrado, de lo contrario agrega el caracter original al nuevo string
if (origCode >= 'A'.charCodeAt(0) && origCode <= 'Z'.charCodeAt(0)) {
newCode = (origCode - 'A'.charCodeAt(0) + x) % 26 + 'A'.charCodeAt(0);
newLetter = String.fromCharCode(newCode);
cipherMsg += newLetter;
} else if (origCode >= 'a'.charCodeAt(0) && origCode <= 'z'.charCodeAt(0)) {
newCode = (origCode - 'a'.charCodeAt(0) + x) % 26 + 'a'.charCodeAt(0);
newLetter = String.fromCharCode(newCode);
cipherMsg += newLetter;
} else {
cipherMsg += String.fromCharCode(origCode);
}
}
// Retornando mensaje cifrado
return alert('Mensaje cifrado:\n' + cipherMsg);
}

// Creando función para descrifrar
function decipher(msg, x) {
// Creando variables
var newCode = 0;
var origCode = 0;
var origLetter = '';
var decipherMsg = '';

// Recorriendo el string de izquierda a derecha
for (var i = 0; i < msg.length; i++) {
// Encontramos el código de cada caracter mediante el método charCodeAt
newCode = msg.charCodeAt(i);
// Si el caracter es una letra mayúscula o minúscula (entre los códigos 65 y 90 o 97 y 122),
// realiza el cifrado, de lo contrario agrega el caracter original al nuevo string
if (newCode >= 'A'.charCodeAt(0) && newCode <= 'Z'.charCodeAt(0)) {
if ((newCode - x % 26) < 'A'.charCodeAt(0)) {
origCode = 'Z'.charCodeAt(0) - (x % 26) + (newCode - 'A'.charCodeAt(0)) + 1;
} else {
origCode = (newCode - x % 26);
}
origLetter = String.fromCharCode(origCode);
decipherMsg += origLetter;
} else if (newCode >= 'a'.charCodeAt(0) && newCode <= 'z'.charCodeAt(0)) {
if ((newCode - x % 26) < 'a'.charCodeAt(0)) {
origCode = 'z'.charCodeAt(0) - (x % 26) + (newCode - 'a'.charCodeAt(0)) + 1;
} else {
origCode = (newCode - x % 26);
}
origLetter = String.fromCharCode(origCode);
decipherMsg += origLetter;
} else {
decipherMsg += String.fromCharCode(newCode);
}
}
// Retornando mensaje descifrado
return alert('Mensaje descifrado:\n' + decipherMsg);
}

// Menú de opciones para cifrar o descifrar mensajes
do {
var menu = '1.Cifrar mensaje\n2.Descifrar mensaje\n3.Salir';
var option = prompt('Ingrese una opción:\n' + menu);
// Si la opción es 1
if (option === '1') {
var msg = prompt('Ingrese mensaje a cifrar:');
// Llamar función cipher
if (msg !== '') {
cipher(msg, 33);
}
// Si la opción es 2
} else if (option === '2') {
var msg = prompt('Ingrese mensaje a descifrar:');
// Llamar función decipher
if (msg !== '') {
decipher(msg, 33);
}
}
} while (option !== '3');