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


with javascript_semantics
constant ENCRYPT = +1,
         DECRYPT = -1
 
function Vigenere(string s, key, integer mode)
    string res = ""
    integer k = 1, ch
    s = upper(s)
    for i=1 to length(s) do
        ch = s[i]
        if ch>='A' and ch<='Z' then
            res &= 'A'+mod(ch+mode*(key[k]+26),26)
            k = mod(k,length(key))+1
        end if
    end for
    return res
end function
 
constant key = "LEMON",
         s = "ATTACK AT DAWN",
         e = Vigenere(s,key,ENCRYPT),
         d = Vigenere(e,key,DECRYPT)
 
printf(1,"Original: %s\nEncrypted: %s\nDecrypted: %s\n",{s,e,d})
