forked from mouredev/retos-programacion-2023
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Jaime Rodríguez Moro
committed
Sep 29, 2023
1 parent
7e6e1df
commit 7936549
Showing
1 changed file
with
55 additions
and
0 deletions.
There are no files selected for viewing
55 changes: 55 additions & 0 deletions
55
Retos/Reto #24 - CIFRADO CÉSAR [Fácil]/javascript/minslaydev.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
/* | ||
* Crea un programa que realize el cifrado César de un texto y lo imprima. | ||
* También debe ser capaz de descifrarlo cuando así se lo indiquemos. | ||
* | ||
* Te recomiendo que busques información para conocer en profundidad cómo | ||
* realizar el cifrado. Esto también forma parte del reto. | ||
*/ | ||
// shift must be 3 and must incñude ascii ñ character | ||
|
||
function caesarCipher(plainText) { | ||
let cipherText = ""; | ||
for (let i = 0; i < plainText.length; i++) { | ||
let ascii = plainText.charCodeAt(i); | ||
if (ascii >= 65 && ascii <= 90) { | ||
ascii = ascii + 3; | ||
if (ascii > 90) { | ||
ascii = ascii - 26; | ||
} | ||
} else if (ascii >= 97 && ascii <= 122) { | ||
ascii = ascii + 3; | ||
if (ascii > 122) { | ||
ascii = ascii - 26; | ||
} | ||
} | ||
cipherText += String.fromCharCode(ascii); | ||
} | ||
return cipherText; | ||
} | ||
|
||
function caesarUncipher(cipherText) { | ||
let plainText = ""; | ||
for (let i = 0; i < cipherText.length; i++) { | ||
let ascii = cipherText.charCodeAt(i); | ||
if (ascii >= 65 && ascii <= 90) { | ||
ascii = ascii - 3; | ||
if (ascii < 65) { | ||
ascii = ascii + 26; | ||
} | ||
} else if (ascii >= 97 && ascii <= 122) { | ||
ascii = ascii - 3; | ||
if (ascii < 97) { | ||
ascii = ascii + 26; | ||
} | ||
} | ||
plainText += String.fromCharCode(ascii); | ||
} | ||
return plainText; | ||
} | ||
|
||
let text = "¡acbdxyz ABCDXYZ!"; | ||
console.log(text) | ||
let cipherText = caesarCipher(text); | ||
console.log(cipherText); | ||
let plainText = caesarUncipher(cipherText); | ||
console.log(plainText); |