################################################################ # 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] } ################################################################ # proc loadFileToWin {win}-- # Load data to a text widget # Arguments # win The text widget to load data into # # Results # A text window emptied and then filled. # proc loadFileToWin {win} { set infile [tk_getOpenFile] if {$infile eq ""} {return} set if [open $infile r] $win delete 0.0 end $win insert 0.0 [read $if] close $if } ################################################################ # proc saveFileFromWin {win}-- # Save data from a text widget # Arguments # win The name of a text widget to save from # # Results # File is created or modified. # proc saveFileFromWin {win} { set outfile [tk_getSaveFile] if {$outfile eq ""} {return} set of [open $outfile w] puts $of [$win get 0.0 end] close $of } ################################################################ # proc buildGUI {}-- # Create the GUI # Arguments # NONE # # Results # Creates new commands/widgets # proc buildGUI {} { set row 1 set w [text .plain -height 5 -yscrollcommand {.pl_ysb set}] grid $w -columnspan 5 -rowspan 2 -row $row -column 2 set w [scrollbar .pl_ysb -orient vertical -command {.plain yview}] grid $w -row $row -column 7 -sticky ns -rowspan 2 set w1 [button .b_loadPln -text "Load" -command "loadFileToWin .plain"] set w2 [button .b_savePln -text "Save" -command "saveFileFromWin .plain"] grid $w1 -row $row -column 1 incr row grid $w2 -row $row -column 1 incr row set w [text .encrypt -height 5 -yscrollcommand {.en_ysb set}] grid $w -columnspan 5 -rowspan 2 -row $row -column 2 set w [scrollbar .en_ysb -orient vertical -command {.encrypt yview}] grid $w -row $row -column 7 -sticky ns -rowspan 2 set w1 [button .b_loadEnc -text "Load" -command "loadFileToWin .encrypt"] set w2 [button .b_saveEnc -text "Save" -command "saveFileFromWin .encrypt"] grid $w1 -row $row -column 1 incr row grid $w2 -row $row -column 1 incr row 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] set w4 [button .b_clear -text "Clear Windows" \ -command {.plain delete 0.0 end; .encrypt delete 0.0 end}] set col 2 foreach w [list $w1 $w2 $w3 $w4] { grid $w -row $row -column [incr col] } } buildGUI