################################################################ # proc encrypt {txt}-- # encrypt the input text. # Arguments # txt The text to be converted. # # Results # No side effects # proc encrypt {txt} { set alpha {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} expr srand(0) foreach char [split $txt {}] { set pos [lsearch $alpha $char] if {$pos >= 0} { set pos2 [expr {($pos + int(rand()*10)) %26}] set char2 [lindex $alpha $pos2] } 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} { set alpha {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} expr srand(0) foreach char [split $txt {}] { set pos [lsearch $alpha $char] if {$pos >= 0} { set pos2 [expr {($pos - int(rand()*10)) %26}] set char2 [lindex $alpha $pos2] } 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 3 set w [text .encrypt] grid $w -columnspan 3 set w0 [button .b_decrypt -text "Decrypt" -command {decryptWin .encrypt .plain}] set w1 [button .b_encrypt -text "Encrypt" -command {encryptWin .plain .encrypt}] set w2 [button .b_exit -text "Exit" -command exit] grid $w0 $w1 $w2