Created
August 26, 2016 19:28
-
-
Save jecxjo/f3591aef5260cf8f889e0c7cca927a66 to your computer and use it in GitHub Desktop.
tcl/tk example AES Encryption with channels and transforms
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# crypto.tcl -- | |
# | |
# This file implements and example Tk applicaiton that uses | |
# AES encryption, Channels and Transforms. | |
# | |
# Copyright (c) 2016 Jeff Parent | |
# | |
# This code is licensed under the Creative Commons Attribution-ShareAlike 4.0 | |
# International license (CC BY-SA 4.0). | |
# | |
# Author: Jeff Parent <[email protected]> | |
# Date: 2016/08/26 14:25:09 | |
package provide app-crypto 1.0 | |
package require Tk | |
package require BWidget | |
package require sha256 | |
package require aes | |
package require tcl::chan::string | |
package require tcl::chan::textwindow | |
package require tcl::transform::base64 | |
wm title . "Encode / Decode" | |
wm resizable . 0 0 | |
pack [frame .fr] | |
grid [ScrolledWindow .fr.input] -row 0 -column 0 -columnspan 2 | |
text .fr.input.text -wrap none -width 80 -height 10 | |
.fr.input setwidget .fr.input.text | |
grid [frame .fr.btns] -row 1 -column 0 -columnspan 2 | |
grid [ttk::label .fr.btns.lbl -text "Password"] -row 0 -column 0 -stick w | |
grid [ttk::entry .fr.btns.pass -show "*" -textvariable password -width 20] -row 0 -column 1 | |
grid [ttk::button .fr.btns.encrypt -command { process encrypt } -text "Encrypt"] -row 0 -column 2 | |
grid [ttk::button .fr.btns.decrypt -command { process decrypt } -text "Decrypt"] -row 0 -column 3 | |
grid [ttk::button .fr.btns.clear -command clear -text "Clear"] -row 0 -column 4 | |
grid [ttk::button .fr.btns.exit -command exit -text "Exit"] -row 0 -column 5 | |
grid [ScrolledWindow .fr.output] -row 2 -column 0 -columnspan 2 | |
text .fr.output.text -wrap char -width 80 -height 10 -state disabled | |
.fr.output setwidget .fr.output.text | |
proc clear {} { | |
.fr.input.text delete 0.0 end | |
.fr.output.text configure -state normal | |
.fr.output.text delete 0.0 end | |
.fr.output.text configure -state disable | |
.fr.btns.pass delete 0 end | |
} | |
proc process {dir} { | |
if {[info exists ::password] && [string length $::password] > 0} { | |
.fr.output.text configure -state normal | |
.fr.output.text delete 0.0 end | |
set key [string range [sha2::sha256 $::password] 0 31] | |
set in [tcl::chan::string [.fr.input.text get 0.0 end]] | |
set out [tcl::chan::textwindow .fr.output.text] | |
fconfigure $in -translation binary | |
fconfigure $out -translation binary | |
if {$dir == {encrypt}} { | |
set iv [string range [sha2::sha256 [clock clicks]:[pid]:[expr {rand()}]] 0 15] | |
tcl::transform::base64 $out | |
puts -nonewline $out $iv | |
flush $out | |
} else { | |
set dir decrypt | |
tcl::transform::base64 $in | |
set iv [chan read $in 16] | |
} | |
aes::aes -mode cbc -dir $dir -key $key -iv $iv -out $out -in $in | |
flush $out | |
close $in | |
close $out | |
.fr.output.text configure -state disable | |
} else { | |
tk_messageBox -title "ERROR" -message "Must supply password" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment