################################################################ # 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] } ############################################################### # NOTE THESE PROCEDURES LOAD TO/SAVE FROM HARDCODED WINDOWS. # It only loads plaintext and saves encrypted. # New buttons and a procedure argument will make it possible to # load the plaintext or encrypted text. ############################################################### ################################################################ # proc loadFile {}-- # Load a plain text file # Arguments # NONE # # Results # .plain window is modified. # proc loadFile {} { set infile [tk_getOpenFile] if {$infile eq ""} {return} set if [open $infile r] .plain delete 0.0 end .plain insert 0.0 [read $if] close $if } ################################################################ # proc saveFile {}-- # Save an encrypted text file # Arguments # NONE # # Results # File is created or modified. # proc saveFile {} { set outfile [tk_getSaveFile] if {$outfile eq ""} {return} set of [open $outfile w] puts $of [.encrypt get 0.0 end] close $of } proc buildGUI {} { set w [text .plain -height 5] grid $w -columnspan 5 set w [text .encrypt -height 5] grid $w -columnspan 5 set w1 [button .b_load -text "Load" -command "loadFile"] set w2 [button .b_save -text "Save" -command "saveFile"] set w3 [button .b_decrypt -text "Decrypt" -command {decryptWin .encrypt .plain}] set w4 [button .b_encrypt -text "Encrypt" -command {encryptWin .plain .encrypt}] set w5 [button .b_exit -text "Exit" -command exit] grid $w1 $w2 $w3 $w4 $w5 } buildGUI