diff --git a/Ciphers/AtBash.js b/Ciphers/AtBash.js deleted file mode 100644 index d07700e59..000000000 --- a/Ciphers/AtBash.js +++ /dev/null @@ -1,26 +0,0 @@ -const enAtbash = (message) => { - const alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' - const tebahpla = 'ZYXWVUTSRQPONMLKJIHGFEDCBA' - const alphabet1 = 'abcdefghijklmnopqrstuvwxyz' - const tebahpla1 = 'zyxwvutsrqponmlkjihgfedcba' - let decodedString = '' - - for (let i = 0; i < message.length; i++) { - const codedLetra = message.charAt(i) - if (/[^a-zA-Z]/.test(message[i])) { - decodedString = decodedString + message[i] - } else if (message[i] === message[i].toUpperCase()) { - const letraPosMayus = alphabet.indexOf(codedLetra) - const tebLetraPosMayus = tebahpla.charAt(letraPosMayus) - decodedString = decodedString + tebLetraPosMayus - } else { - const letraPosMinus1 = alphabet1.indexOf(codedLetra) - const tebLetraPosMinus1 = tebahpla1.charAt(letraPosMinus1) - decodedString = decodedString + tebLetraPosMinus1 - } - } - return decodedString -} - -// testing code -console.log(enAtbash('Hello World!')) diff --git a/Ciphers/Atbash.js b/Ciphers/Atbash.js new file mode 100644 index 000000000..4151904cb --- /dev/null +++ b/Ciphers/Atbash.js @@ -0,0 +1,34 @@ +/* +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 + */ + +const 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