Created
August 16, 2021 05:58
-
-
Save luisenriquecorona/198559dfb57d3d6ed3db050876a2df1f to your computer and use it in GitHub Desktop.
Atbash.js
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
/* | |
The Atbash cipher is a particular type of monoalphabetic cipher | |
formed by taking the alphabet and mapping it to its reverse, | |
so that the first letter becomes the last letter, | |
the second letter becomes the second to last letter, and so on. | |
*/ | |
/** | |
* Decrypt a Atbash cipher | |
* @param {String} str - string to be decrypted/encrypt | |
* @return {String} decrypted/encrypted string | |
*/ | |
function Atbash (message) { | |
let decodedString = '' | |
for (let i = 0; i < message.length; i++) { | |
if (/[^a-zA-Z]/.test(message[i])) { | |
decodedString += message[i] | |
} else if (message[i] === message[i].toUpperCase()) { | |
decodedString += String.fromCharCode(90 + 65 - message.charCodeAt(i)) | |
} else { | |
decodedString += String.fromCharCode(122 + 97 - message.charCodeAt(i)) | |
} | |
} | |
return decodedString | |
} | |
// Atbash Example | |
const encryptedString = 'HELLO WORLD' | |
const decryptedString = Atbash(encryptedString) | |
console.log(decryptedString) // SVOOL DLIOW |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment