Created
July 7, 2022 20:28
-
-
Save NightScript370/0f05add2294cb92faf0f8e9dfb52be82 to your computer and use it in GitHub Desktop.
Calculating Vigenere Cipher's (Gravity Falls) via a TS script
This file contains 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
function vigenere(text: string, key: string, DecryptMode: boolean, alphabet: string) { | |
const textMask = getTextMask(text, alphabet); | |
const normalizedText = getNormalizedText(text, textMask); | |
if (!(normalizedText.length > 0 && key.length > 0)) | |
return; | |
const transformedText = DecryptMode ? decrypt(normalizedText, alphabet, key) : encrypt(normalizedText, alphabet, key); | |
return restoreText(text, transformedText, textMask); | |
} | |
function getTextMask(text:string, alphabet:string) { | |
const alphabetUpper = alphabet.toUpperCase(); | |
const alphabetLower = alphabet.toLowerCase(); | |
const arr = text.split('').map((currentValue) => { | |
const isUpperLetter = alphabetUpper.includes(currentValue); | |
const isLowerLetter = alphabetLower.includes(currentValue); | |
let l = '-'; | |
if (isUpperLetter) | |
l = 'A'; | |
else if (isLowerLetter) | |
l = 'a'; | |
else | |
l = '+'; | |
return l; | |
}); | |
return arr.join(''); | |
} | |
function getNormalizedText(text:string, textMask:string) { | |
const arr = []; | |
const length = textMask.length; | |
for (let i = 0; i < length; i++) { | |
if (textMask[i] === 'a' || textMask[i] === 'A') | |
arr.push(text[i]); | |
} | |
return arr.join('').toLowerCase(); | |
} | |
function restoreText(originalText:string, normalizedText:string, textMask:string) { | |
const arr = originalText.split(''); | |
let j = 0; | |
const length = textMask.length; | |
for (let i = 0; i < length; i++) { | |
switch (textMask[i]) { | |
case 'a': | |
arr[i] = normalizedText[j++]; break; | |
case 'A': | |
arr[i] = normalizedText[j++].toUpperCase(); break; | |
case '-': | |
arr[i] = ''; | |
} | |
} | |
return arr.join(''); | |
} | |
const encrypt = (src:string, alphabet:string, key:string) => transform(src, alphabet, getKeyShifts(key, alphabet), false); | |
const decrypt = (src:string, alphabet:string, key:string) => transform(src, alphabet, getKeyShifts(key, alphabet), true); | |
function transform(src:string, alphabet:string, shifts:number[], invert:boolean) { | |
let result = ''; | |
const keyLength = shifts.length; | |
if (shifts.length == 0) | |
return src; | |
for (let i = 0; i < src.length; i++) { | |
let shift = 0; | |
if (i < keyLength) { | |
shift = invert ? -shifts[i % keyLength] : shifts[i % keyLength]; | |
} else { | |
shift = invert ? -alphabet.indexOf(result[i - keyLength]) : alphabet.indexOf(src[i - keyLength]); | |
} | |
result += transformChar(src[i], alphabet, shift); | |
} | |
return result; | |
} | |
function getKeyShifts(key:string, keyAlphabet:string) { | |
const arr = new Array(key.length); | |
for (let i = 0; i < key.length; i++) { | |
const p = keyAlphabet.indexOf(key[i]); | |
arr[i] = p; | |
} | |
return arr; | |
} | |
function transformChar(ch:string, alphabet:string, shift:number) { | |
const p = (alphabet.indexOf(ch) + shift + alphabet.length) % alphabet.length; | |
return alphabet[p]; | |
} | |
console.log(vigenere('hi', 'die', false, 'abcdefghijklmnopqrstuvwxyz')) | |
console.log(vigenere('kq', 'die', true, 'abcdefghijklmnopqrstuvwxyz')) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment