################################################################ # proc encrypt {txt}-- # encrypt the input text. # Arguments # txt The text to be converted. # # Results # No side effects # set plainChars {a b c d e f g h i j k l m n o p q r s t u v w x y z} set encryptChars {z y x w v u t s r q p o n m l k j i h g f e d c b a} proc encrypt {txt} { global plainChars global encryptChars foreach char [split $txt {}] { set pos [lsearch $plainChars $char] # If character is in list, encrypt it, else keep the character if {$pos >= 0} { set char2 [lindex $encryptChars $pos] } else { set char2 $char } append encrypt $char2 } return $encrypt } ################################################################ # proc decrypt {txt}-- # decrypt the input text. # Arguments # txt The text to be converted. # # Results # No Side effects # proc decrypt {txt} { global plainChars global encryptChars foreach char [split $txt {}] { set pos [lsearch $encryptChars $char] # If character is in list, decrypt it, else keep the character if {$pos >= 0} { set char2 [lindex $plainChars $pos] } else { set char2 $char } append plain $char2 } return $plain } ################################################################ # proc encryptWin {inWin outWin}-- # Read characters from inWin and insert encrypted characters # into outWin # Arguments # inWin: Name of a text widget to read input from # outWin: Name of a text widget to write encrypted text to # Results # outWin widget contents are modified. # proc encryptWin {inWin outWin} { set txt [$inWin get 0.0 end] $outWin delete 0.0 end $outWin insert 0.0 [encrypt $txt] } ################################################################ # proc decryptWin {inWin outWin}-- # Read characters from inWin and insert plaintext characters # into outWin # Arguments # inWin: Name of a text widget to read input from # outWin: Name of a text widget to write encrypted text to # Results # outWin widget contents are modified. # proc decryptWin {inWin outWin} { set txt [$inWin get 0.0 end] $outWin delete 0.0 end $outWin insert 0.0 [decrypt $txt] } set w [text .plain] grid $w -columnspan 5 set w [text .encrypt] grid $w -columnspan 5 set w1 [button .b_decrypt -text "Decrypt" -command {decryptWin .encrypt .plain}] set w2 [button .b_encrypt -text "Encrypt" -command {encryptWin .plain .encrypt}] set w3 [button .b_exit -text "Exit" -command exit] grid $w1 $w2 $w3