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


Key = VIGENERECIPHER
Text= Beware the Jabberwock, my son! The jaws that bite, the claws that catch!
 
out := "Input      =" text "`nkey        =" key "`nCiphertext =" (c := VigenereCipher(Text, Key)) "`nDecrypted  =" VigenereDecipher(c, key)
MsgBox % clipboard := out
 
VigenereCipher(Text, Key){
   StringUpper, Text, Text
   Text := RegExReplace(Text, "[^A-Z]")
   Loop Parse, Text
   {
      a   := Asc(A_LoopField)                                   - Asc("A")
      b   := Asc(SubStr(Key, 1+Mod(A_Index-1, StrLen(Key)), 1)) - Asc("A")
      out .= Chr(Mod(a+b,26)+Asc("A"))
   }
   return out
}
 
VigenereDecipher(Text, key){
   Loop Parse, key
      decoderKey .= Chr(26-(Asc(A_LoopField)-65)+65)
   return VigenereCipher(Text, decoderKey)
}