Skip to content

Instantly share code, notes, and snippets.

@tobiasroeder
Created September 24, 2021 09:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tobiasroeder/0ccadd924bf5f5b9c06419a091f76186 to your computer and use it in GitHub Desktop.
Save tobiasroeder/0ccadd924bf5f5b9c06419a091f76186 to your computer and use it in GitHub Desktop.
Vigenère-Chiffre
/*
* 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