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


import strutils
 
proc encrypt(msg, key: string): string =
  var pos = 0
  for c in msg:
    if c in Letters:
      result.add chr(((ord(key[pos]) + ord(c.toUpperAscii)) mod 26) + ord('A'))
      pos = (pos + 1) mod key.len
 
proc decrypt(msg, key: string): string =
  var pos = 0
  for c in msg:
    result.add chr(((26 + ord(c) - ord(key[pos])) mod 26) + ord('A'))
    pos = (pos + 1) mod key.len
 
const text = "Beware the Jabberwock, my son! The jaws that bite, the claws that catch!"
const key = "VIGENERECIPHER"
 
let encr = encrypt(text, key)
let decr = decrypt(encr, key)
 
echo text
echo encr
echo decr