Created
September 24, 2021 09:15
-
-
Save tobiasroeder/0ccadd924bf5f5b9c06419a091f76186 to your computer and use it in GitHub Desktop.
Vigenère-Chiffre
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
/* | |
* source: https://rosettacode.org/wiki/Vigen%C3%A8re_cipher#JavaScript | |
*/ | |
// vigenere | |
function vigenere(text, key, decode) { | |
// helper | |
const ordA = a => a.charCodeAt(0) - 65; | |
// main | |
let i = 0, b; | |
key = key.toUpperCase().replace(/[^A-Z]/g, ''); | |
return text.toUpperCase().replace(/[^A-Z]/g, '').replace(/[A-Z]/g, function (a) { | |
b = key[i++ % key.length]; | |
return String.fromCharCode(((ordA(a) + (decode ? 26 - ordA(b) : ordA(b))) % 26 + 65)); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment