Add new Cipher > Keyword Shifted Alphabet

This commit is contained in:
Bambusek, David
2020-10-02 12:23:41 +02:00
parent c2769cd63d
commit 72d556ace2

View File

@ -16,56 +16,57 @@
* Non alphabetical characters (space, exclamation mark, ...) are kept as they are * 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) { function checkKeywordValidity (keyword) {
keyword.split('').forEach((char, index) => { keyword.split('').forEach((char, index) => {
const rest = keyword.slice(0, index) + keyword.slice(index + 1); const rest = keyword.slice(0, index) + keyword.slice(index + 1)
if (rest.indexOf(char) !== -1) { if (rest.indexOf(char) !== -1) {
return false; return false
} }
}) })
return true; return true
} }
function getEncryptedAlphabet(keyword) { function getEncryptedAlphabet (keyword) {
const encryptedAlphabet = keyword.split(''); const encryptedAlphabet = keyword.split('')
alphabet.forEach((char) => { alphabet.forEach((char) => {
if (encryptedAlphabet.indexOf(char) === -1) { if (encryptedAlphabet.indexOf(char) === -1) {
encryptedAlphabet.push(char); encryptedAlphabet.push(char)
} }
}); })
return encryptedAlphabet; return encryptedAlphabet
} }
function translate(sourceAlphabet, targetAlphabet, message) { function translate (sourceAlphabet, targetAlphabet, message) {
return message.split('').reduce((encryptedMessage, char) => { return message.split('').reduce((encryptedMessage, char) => {
const isUpperCase = char === char.toUpperCase(); const isUpperCase = char === char.toUpperCase()
const encryptedCharIndex = sourceAlphabet.indexOf(char.toLowerCase()); const encryptedCharIndex = sourceAlphabet.indexOf(char.toLowerCase())
const encryptedChar = encryptedCharIndex !== -1 ? targetAlphabet[encryptedCharIndex] : char const encryptedChar = encryptedCharIndex !== -1 ? targetAlphabet[encryptedCharIndex] : char
return encryptedMessage += isUpperCase ? encryptedChar.toUpperCase() : encryptedChar; encryptedMessage += isUpperCase ? encryptedChar.toUpperCase() : encryptedChar
}, ''); return encryptedMessage
}, '')
} }
function checkInputs(keyword, message) { function checkInputs (keyword, message) {
if (!keyword || !message) { if (!keyword || !message) {
throw new Error('Both keyword and message must be specified'); throw new Error('Both keyword and message must be specified')
} }
if (!checkKeywordValidity(keyword)) { if (!checkKeywordValidity(keyword)) {
throw new Error('Invalid keyword!') throw new Error('Invalid keyword!')
} }
} }
function encrypt(keyword, message) { function encrypt (keyword, message) {
checkInputs(keyword, message); checkInputs(keyword, message)
return translate(alphabet, getEncryptedAlphabet(keyword.toLowerCase()), message); return translate(alphabet, getEncryptedAlphabet(keyword.toLowerCase()), message)
} }
function decrypt(keyword, message) { function decrypt (keyword, message) {
checkInputs(keyword, message); checkInputs(keyword, message)
return translate(getEncryptedAlphabet(keyword.toLowerCase()), alphabet, message); return translate(getEncryptedAlphabet(keyword.toLowerCase()), alphabet, message)
} }
console.log(encrypt('keyword', 'Hello world!')); // Prints 'Aoggj ujngw!' console.log(encrypt('keyword', 'Hello world!')) // Prints 'Aoggj ujngw!'
console.log(decrypt('keyword', 'Aoggj ujngw!')); // Prints 'Hello world! console.log(decrypt('keyword', 'Aoggj ujngw!')) // Prints 'Hello world!