Last active
March 23, 2018 13:25
-
-
Save drmcarvalho/0488549f0b5d4b9c4933458138568dc1 to your computer and use it in GitHub Desktop.
Cifra de cesar.
This file contains hidden or 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
let nome = "vini! ¨¨%A%S&¨SA¨&SA ¨ vini"; | |
function mod(n, p) { | |
if (n < 0) | |
n = p - Math.abs(n) % p; | |
return n % p; | |
} | |
function encriptar(chave, str) { | |
let alpha = "abcdefghijklmnopqrstuvwxyz"; | |
let saida = ''; | |
if (chave <= 0) return ''; | |
if (!str) return ''; | |
for (let i = 0; i < str.length; i++) { | |
if (str[i].match(/[A-Za-z]/gi)) | |
saida += alpha[mod(alpha.indexOf(str[i]) + chave, 26)]; | |
else | |
saida += str[i]; | |
} | |
return saida; | |
} | |
function desencriptar(chave, str) { | |
let alpha = "abcdefghijklmnopqrstuvwxyz"; | |
let saida = ''; | |
if (chave <= 0) return ''; | |
if (!str) return ''; | |
for (let i = 0; i < str.length; i++) { | |
if (str[i].match(/[A-Za-z]/gi)) | |
saida += alpha[mod(alpha.indexOf(str[i]) - chave, 26)]; | |
else | |
saida += str[i]; | |
} | |
return saida; | |
} | |
let r = encriptar(8, nome); | |
alert(r); | |
alert(desencriptar(8, r)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Veja funcionando no JSFddle.