Skip to content

Instantly share code, notes, and snippets.

@khaosdoctor
Last active October 27, 2022 12:05
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save khaosdoctor/bec59da8ccc72390a21831b3376582b7 to your computer and use it in GitHub Desktop.
Save khaosdoctor/bec59da8ccc72390a21831b3376582b7 to your computer and use it in GitHub Desktop.
Caesar Cypher implementations in JavaScript
/**
* Esse código funciona usando o código de cada caractere digitado e realizando a modificação para outro caractere de acordo com o número da chave
* Para entender melhor de onde vem os números de cada alfabeto, veja o código abaixo
*/
// const alphabet = 'abcdefghijklmnpqrstuvwxyz'
// const capital = alphabet.toUpperCase()
// for (let i = 0; i < alphabet.length; i++) {
// console.log(alphabet[i], alphabet.charCodeAt(i))
// // a 97
// // b 98
// // c 99
// // d 100
// // e 101
// // f 102
// // g 103
// // h 104
// // i 105
// // j 106
// // k 107
// // l 108
// // m 109
// // n 110
// // p 112
// // q 113
// // r 114
// // s 115
// // t 116
// // u 117
// // v 118
// // w 119
// // x 120
// // y 121
// // z 122
// console.log(capital[i], capital.charCodeAt(i))
// // A 65
// // B 66
// // C 67
// // D 68
// // E 69
// // F 70
// // G 71
// // H 72
// // I 73
// // J 74
// // K 75
// // L 76
// // M 77
// // N 78
// // P 80
// // Q 81
// // R 82
// // S 83
// // T 84
// // U 85
// // V 86
// // W 87
// // X 88
// // Y 89
// // Z 90
function caesarCypher(message, key) {
if (key < 0) return caesarCypher(message, key + 26)
const alphabetCapitalMinIndex = 'A'.charCodeAt(0)
const alphabetMinIndex = 'a'.charCodeAt(0)
const isCapital = (char) => char.charCodeAt(0) >= alphabetCapitalMinIndex && char.charCodeAt(0) <= 'Z'.charCodeAt(0)
const getCharIndexInAlphabet = (char) =>
!isCapital(char)
? char.charCodeAt(0) - alphabetMinIndex
: isCapital(char)
? char.charCodeAt(0) - alphabetCapitalMinIndex
: null
let newMessage = ''
for (let messageIndex in message) {
const char = message[messageIndex]
const charIndexInAlphabet = getCharIndexInAlphabet(char)
if (!char.match(/[a-z]/i)) {
newMessage += char
continue
}
const sumBaseIndex = isCapital(char) ? alphabetCapitalMinIndex : alphabetMinIndex
const newCharCode = ((charIndexInAlphabet + key) % 26) + sumBaseIndex
newMessage += String.fromCharCode(newCharCode)
}
return newMessage
}
const key = 2
const message = 'Lucas Santos'
caesarCypher(message, key) // Nwecu Ucpvqu
caesarCypher('Nwecu Ucpvqu', -key) // Lucas Santos
/**
* Nesse outro modelo, o código funciona com um alfabeto pré definido, o que exige que a gente normalize a mensagem
* A vantagem é que o código é mais simples e mais fácil de ler, porém ele não cobre todos os casos como letras maíusculas
*/
function caesarCypher(message, key) {
if (key < 0) return caesarCypher(message, key + 26)
let newMessage = ''
const alphabet = 'abcdefghijklmnopqrstuvwxyz'
message = message.toLowerCase()
for (const char of message) {
let newChar = char
const charIndexAtAlphabet = alphabet.indexOf(char)
if (charIndexAtAlphabet >= 0) newChar = alphabet.charAt((charIndexAtAlphabet + key) %26)
newMessage += newChar
}
return newMessage
}
const key = 25
const message = 'Lucas Santos'
const encrypted = caesarCypher(message, key)
caesarCypher(encrypted, -key)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment