Skip to content

Instantly share code, notes, and snippets.

@joseanpg
Created January 14, 2013 19:53
Show Gist options
  • Save joseanpg/4532804 to your computer and use it in GitHub Desktop.
Save joseanpg/4532804 to your computer and use it in GitHub Desktop.
Vigenère cipher
//http://en.wikipedia.org/wiki/Vigen%C3%A8re_cipher
//https://twitter.com/r0aTzdg73QLawWw/status/290907958118842368
//https://gist.github.com/1385585
var vigenere = (function() {
var map = Array.prototype.map;
var canon = function(str){ return map.call(str.toUpperCase(),function(c){return c.charCodeAt(0)-65})}
return function(source, key) {
source = canon(source);
key = canon(key);
keylen = key.length;
return map.call(map.call(source,function(c,j) { return (c+key[j%keylen])%26}),function(c){return String.fromCharCode(c+65)}).join('');
}
})();
//Test
alert(vigenere('ATTACKATDAWN','LEMON')+'\n'+'LXFOPVEFRNHR');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment