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


# Simple helper since charCodeAt is quite long to write.
code = (char) -> char.charCodeAt()
 
encrypt = (text, key) ->
	res = []
	j = 0
 
	for c in text.toUpperCase()
		continue if c < 'A' or c > 'Z'
 
		res.push ((code c) + (code key[j]) - 130) % 26 + 65
		j = ++j % key.length
 
	String.fromCharCode res...
 
decrypt = (text, key) ->
	res = []
	j = 0
 
	for c in text.toUpperCase()
		continue if c < 'A' or c > 'Z'
 
		res.push ((code c) - (code key[j]) + 26) % 26 + 65
		j = ++j % key.length
 
	String.fromCharCode res...
 
# Trying it out
key       = "VIGENERECIPHER"
original  = "Beware the Jabberwock, my son! The jaws that bite, the claws that catch!"
encrypted = encrypt original, key
 
console.log "Original  : #{original}"
console.log "Encrypted : #{encrypted}"
console.log "Decrypted : #{decrypt encrypted, key}"