Task

Implement a   Vigenère cypher,   both encryption and decryption.

The program should handle keys and text of unequal length,
and should capitalize everything and discard non-alphabetic characters. 

(If your program handles non-alphabetic characters in another way,
make a note of it.)



Related tasks

   Caesar cipher
   Rot-13
   Substitution Cipher


/* Vigenère cipher, in Jsish */
"use strict";
 
function ordA(a:string):number {
    return a.charCodeAt(0) - 65;
}
 
// vigenere
function vigenereCipher(text:string, key:string, decode:boolean=false):string {
    var i = 0, b;
    key = key.toUpperCase().replace(/[^A-Z]/g, '');
    return text.toUpperCase().replace(/[^A-Z]/g, '').replace(/[A-Z]/g,
        function(a:string, idx:number, str:string) {
            b = key[i++ % key.length];
            return String.fromCharCode(((ordA(a) + (decode ? 26 - ordA(b) : ordA(b))) % 26 + 65));
        });
}
 
provide('vigenereCipher', 1);
 
if (Interp.conf('unitTest')) {
    var text = "The quick brown fox Jumped over the lazy Dog the lazy dog lazy dog dog";
    var key = 'jsish';
    var enc = vigenereCipher(text, key);
;    text;
;    enc;
;    vigenereCipher(enc, key, true);
}
 
/*
=!EXPECTSTART!=
text ==> The quick brown fox Jumped over the lazy Dog the lazy dog lazy dog dog
enc ==> CZMIBRUSTYXOVXVGBCEWNVWNLALPWSJRGVVPLPWSJRGVVPDIRFMGOVVP
vigenere(enc, key, true) ==> THEQUICKBROWNFOXJUMPEDOVERTHELAZYDOGTHELAZYDOGLAZYDOGDOG
=!EXPECTEND!=
*/