From 0ca9c8972736d961d55b7e1dc597026160fbfb98 Mon Sep 17 00:00:00 2001 From: Sutthinart Khunvadhana Date: Mon, 12 Oct 2020 14:35:09 +0700 Subject: [PATCH] Add ROT13 cipher (#463) * Add ROT13 cipher * Update ROT13.js Co-authored-by: Sutthinart Khunvadhana Co-authored-by: vinayak --- Ciphers/ROT13.js | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 Ciphers/ROT13.js diff --git a/Ciphers/ROT13.js b/Ciphers/ROT13.js new file mode 100644 index 000000000..24eeacf8e --- /dev/null +++ b/Ciphers/ROT13.js @@ -0,0 +1,21 @@ +/** + * Transcipher a ROT13 cipher + * @param {String} text - string to be encrypted + * @return {String} - decrypted string + */ +const transcipher = (text) => { + const originalCharacterList = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz' + const toBeMappedCharaterList = 'NOPQRSTUVWXYZABCDEFGHIJKLMnopqrstuvwxyzabcdefghijklm' + const index = x => originalCharacterList.indexOf(x) + const replace = x => index(x) > -1 ? toBeMappedCharaterList[index(x)] : x + return text.split('').map(replace).join('') +} + +(() => { + const messageToBeEncrypted = 'The quick brown fox jumps over the lazy dog' + console.log(`Original Text = "${messageToBeEncrypted}"`) + const rot13CipheredText = transcipher(messageToBeEncrypted) + console.log(`Ciphered Text = "${rot13CipheredText}"`) + const rot13DecipheredText = transcipher(rot13CipheredText) + console.log(`Deciphered Text = "${rot13DecipheredText}"`) +})()