From c2769cd63dac747e02d7d4680c395f1f605602d2 Mon Sep 17 00:00:00 2001 From: "Bambusek, David" Date: Fri, 2 Oct 2020 12:16:28 +0200 Subject: [PATCH 1/2] Add new Cipher > Keyword Shifted Alphabet --- Ciphers/KeywordShiftedAlphabet.js | 71 +++++++++++++++++++++++++++++++ 1 file changed, 71 insertions(+) create mode 100644 Ciphers/KeywordShiftedAlphabet.js diff --git a/Ciphers/KeywordShiftedAlphabet.js b/Ciphers/KeywordShiftedAlphabet.js new file mode 100644 index 0000000000..cd350b684b --- /dev/null +++ b/Ciphers/KeywordShiftedAlphabet.js @@ -0,0 +1,71 @@ +/** + * Keyword shifted alphabet is a simple cipher using a translation table created with a help of a keyword. + * Keyword must be a word where each character can occur only once. + * To create the translation table, we write all the alphabet characters to the first. + * Second row start with the keyword, then we continue with the rest of the characters that are missing in alphabetical order. + * + * |A|B|C|D|E|F|G|H|I|J|K|L|M|N|O|P|Q|R|S|T|U|V|W|X|Y|Z| + * |K|E|Y|W|O|R|D|A|B|C|F|G|H|I|J|L|M|N|P|Q|S|T|U|V|W|Z| + * + * Encryption is then just a matter of writing the matching (same index) letter from the second row instead of the first row: + * 'Hello world' -> 'Aoggj ujngw' + * + * Decryption is then just the reverse process of writing the matching (same index) letter from the first row instead of the second row + * 'Aogg ujngw' -> 'Hello world' + * + * Non alphabetical characters (space, exclamation mark, ...) are kept as they are + */ + +const alphabet = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']; + +function checkKeywordValidity(keyword) { + keyword.split('').forEach((char, index) => { + const rest = keyword.slice(0, index) + keyword.slice(index + 1); + if (rest.indexOf(char) !== -1) { + return false; + } + }) + return true; +} + +function getEncryptedAlphabet(keyword) { + const encryptedAlphabet = keyword.split(''); + alphabet.forEach((char) => { + if (encryptedAlphabet.indexOf(char) === -1) { + encryptedAlphabet.push(char); + } + }); + return encryptedAlphabet; +} + +function translate(sourceAlphabet, targetAlphabet, message) { + return message.split('').reduce((encryptedMessage, char) => { + const isUpperCase = char === char.toUpperCase(); + const encryptedCharIndex = sourceAlphabet.indexOf(char.toLowerCase()); + const encryptedChar = encryptedCharIndex !== -1 ? targetAlphabet[encryptedCharIndex] : char + return encryptedMessage += isUpperCase ? encryptedChar.toUpperCase() : encryptedChar; + }, ''); +} + +function checkInputs(keyword, message) { + if (!keyword || !message) { + throw new Error('Both keyword and message must be specified'); + } + + if (!checkKeywordValidity(keyword)) { + throw new Error('Invalid keyword!') + } +} + +function encrypt(keyword, message) { + checkInputs(keyword, message); + return translate(alphabet, getEncryptedAlphabet(keyword.toLowerCase()), message); +} + +function decrypt(keyword, message) { + checkInputs(keyword, message); + return translate(getEncryptedAlphabet(keyword.toLowerCase()), alphabet, message); +} + +console.log(encrypt('keyword', 'Hello world!')); // Prints 'Aoggj ujngw!' +console.log(decrypt('keyword', 'Aoggj ujngw!')); // Prints 'Hello world! \ No newline at end of file From 72d556ace2fec500c676519a616c04d20c777416 Mon Sep 17 00:00:00 2001 From: "Bambusek, David" Date: Fri, 2 Oct 2020 12:23:41 +0200 Subject: [PATCH 2/2] Add new Cipher > Keyword Shifted Alphabet --- Ciphers/KeywordShiftedAlphabet.js | 79 ++++++++++++++++--------------- 1 file changed, 40 insertions(+), 39 deletions(-) diff --git a/Ciphers/KeywordShiftedAlphabet.js b/Ciphers/KeywordShiftedAlphabet.js index cd350b684b..1322f13810 100644 --- a/Ciphers/KeywordShiftedAlphabet.js +++ b/Ciphers/KeywordShiftedAlphabet.js @@ -16,56 +16,57 @@ * Non alphabetical characters (space, exclamation mark, ...) are kept as they are */ -const alphabet = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']; +const alphabet = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'] -function checkKeywordValidity(keyword) { - keyword.split('').forEach((char, index) => { - const rest = keyword.slice(0, index) + keyword.slice(index + 1); - if (rest.indexOf(char) !== -1) { - return false; - } - }) - return true; +function checkKeywordValidity (keyword) { + keyword.split('').forEach((char, index) => { + const rest = keyword.slice(0, index) + keyword.slice(index + 1) + if (rest.indexOf(char) !== -1) { + return false + } + }) + return true } -function getEncryptedAlphabet(keyword) { - const encryptedAlphabet = keyword.split(''); - alphabet.forEach((char) => { - if (encryptedAlphabet.indexOf(char) === -1) { - encryptedAlphabet.push(char); - } - }); - return encryptedAlphabet; +function getEncryptedAlphabet (keyword) { + const encryptedAlphabet = keyword.split('') + alphabet.forEach((char) => { + if (encryptedAlphabet.indexOf(char) === -1) { + encryptedAlphabet.push(char) + } + }) + return encryptedAlphabet } -function translate(sourceAlphabet, targetAlphabet, message) { - return message.split('').reduce((encryptedMessage, char) => { - const isUpperCase = char === char.toUpperCase(); - const encryptedCharIndex = sourceAlphabet.indexOf(char.toLowerCase()); - const encryptedChar = encryptedCharIndex !== -1 ? targetAlphabet[encryptedCharIndex] : char - return encryptedMessage += isUpperCase ? encryptedChar.toUpperCase() : encryptedChar; - }, ''); +function translate (sourceAlphabet, targetAlphabet, message) { + return message.split('').reduce((encryptedMessage, char) => { + const isUpperCase = char === char.toUpperCase() + const encryptedCharIndex = sourceAlphabet.indexOf(char.toLowerCase()) + const encryptedChar = encryptedCharIndex !== -1 ? targetAlphabet[encryptedCharIndex] : char + encryptedMessage += isUpperCase ? encryptedChar.toUpperCase() : encryptedChar + return encryptedMessage + }, '') } -function checkInputs(keyword, message) { - if (!keyword || !message) { - throw new Error('Both keyword and message must be specified'); - } +function checkInputs (keyword, message) { + if (!keyword || !message) { + throw new Error('Both keyword and message must be specified') + } - if (!checkKeywordValidity(keyword)) { - throw new Error('Invalid keyword!') - } + if (!checkKeywordValidity(keyword)) { + throw new Error('Invalid keyword!') + } } -function encrypt(keyword, message) { - checkInputs(keyword, message); - return translate(alphabet, getEncryptedAlphabet(keyword.toLowerCase()), message); +function encrypt (keyword, message) { + checkInputs(keyword, message) + return translate(alphabet, getEncryptedAlphabet(keyword.toLowerCase()), message) } -function decrypt(keyword, message) { - checkInputs(keyword, message); - return translate(getEncryptedAlphabet(keyword.toLowerCase()), alphabet, message); +function decrypt (keyword, message) { + checkInputs(keyword, message) + return translate(getEncryptedAlphabet(keyword.toLowerCase()), alphabet, message) } -console.log(encrypt('keyword', 'Hello world!')); // Prints 'Aoggj ujngw!' -console.log(decrypt('keyword', 'Aoggj ujngw!')); // Prints 'Hello world! \ No newline at end of file +console.log(encrypt('keyword', 'Hello world!')) // Prints 'Aoggj ujngw!' +console.log(decrypt('keyword', 'Aoggj ujngw!')) // Prints 'Hello world!