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


package require Tcl 8.6
 
oo::class create Vigenere {
    variable key
    constructor {protoKey} {
	foreach c [split $protoKey ""] {
	    if {[regexp {[A-Za-z]} $c]} {
		lappend key [scan [string toupper $c] %c]
	    }
	}
    }
 
    method encrypt {text} {
	set out ""
	set j 0
	foreach c [split $text ""] {
	    if {[regexp {[^a-zA-Z]} $c]} continue
	    scan [string toupper $c] %c c
	    append out [format %c [expr {($c+[lindex $key $j]-130)%26+65}]]
	    set j [expr {($j+1) % [llength $key]}]
	}
	return $out
    }
 
    method decrypt {text} {
	set out ""
	set j 0
	foreach c [split $text ""] {
	    if {[regexp {[^A-Z]} $c]} continue
	    scan $c %c c
	    append out [format %c [expr {($c-[lindex $key $j]+26)%26+65}]]
	    set j [expr {($j+1) % [llength $key]}]
	}
	return $out
    }
}