ActiveState Community

Tcl/Tk Encryption

Posted by phi on 2009-03-26 11:41

Hi,

I'm trying to encrypt a basic .txt file with blowfish, i'm also open to other encryption techniques if they are easy to implement.
I have gotten an example from a person on this forum, But it isnt working. The example is below.
[code]
package require blowfish

set iv [binary format H* FEDCBA9876543210]
set key [binary format H* 0123456789ABCDEFF0E1D2C3B4A59687]

file stat plain.txt stats
set remainder [expr $stats(size) % 8]
if {$remainder != 0} {
set padnum [expr 8 - $remainder]
} else {
set padnum 0
}
set ifid [open "plain.txt" "r"]
fconfigure $ifid -translation binary
set plain [read $ifid $stats(size)] ;# read whole file into string
close $ifid
# pad up to a multiple of 8 bytes
append plain [string repeat "\000" $padnum]
set cipher [blowfish::blowfish -mode cbc -key $key -dir \
encrypt -iv $iv $plain]
set ofid [open "cipher.dat" "w"]
puts -nonewline $ofid $cipher
close $ofid

#DECRYPTION

file stat cipher.dat stats
set ifid [open "cipher.dat" "r"]
fconfigure $ifid -translation binary
set cipher [read $ifid $stats(size)]
close $ifid
set ofid [open "plain2.txt" "w"]
puts -nonewline $ofid [string trim [blowfish::blowfish -mode cbc \
-key $key -dir decrypt -iv $iv $cipher] "\000"]
close $ofid
[/code]

This code, seems like it is encrypting ok. But when i decrypt it is not done correctly, the result's are varied, sometimes 1 word would be readable and the others not.
Can anyone assist me on this problem.

Thanks,
Philip.

patthoyts | Thu, 2009-04-09 15:46

When you write out the encrypted data you just use [open $filename "w"] and fail to specify the encoding of the file. The blowfish output will be binary but you will have automatic translation and default system code-page encoding on this file.
Instead - you should read the text file input as text and specify -encoding binary -translation binary -eofchar {} for the output channel. What is likely occurring at the moment is you are getting CRLF translation that breaks the encrypted stream when it is read back as binary.
I would further suggest that you specify the encoding of the data before you encrypt eg: encoding convertto utf-8 $plaintext
Initialization vectors do not need to be secret. The key is the secret part. So the general usage is to prepend the iv to the crypto-stream. There is an example illustrating this for encrypting a packet stream at http://paste.tclers.tk/163

phi | Mon, 2009-04-13 11:08

patthoyts thank you for your reply.

I am very new to blowfish. Is it possible that you or someone else could edit my code to reflect the changes you mentioned. In the meantime, i will keep looking for an answer to do the changes you specified. I will reply back if im successful.

Thanks again.

phi | Mon, 2009-04-13 11:17

Ok... Patthoyts... Thank you sir...
Even thought what you said was difficult to understand i got it to work fairly simple afterwards...
I paid attention to the binary point...
So adding this line
"fconfigure $ofid -translation binary"
after the file wrights the data. And hey presto... it works...
:D
Thanks again.

To anyone who wants to get this working... Here is the code>>

[code]
package require blowfish

set iv [binary format H* FEDCBA9876543210]
set key [binary format H* 0123456789ABCDEFF0E1D2C3B4A59687]

file stat plain.txt stats
set remainder [expr $stats(size) % 8]
if {$remainder != 0} {
set padnum [expr 8 - $remainder]
} else {
set padnum 0
}
set ifid [open "plain.txt" "r"]

fconfigure $ifid -translation binary
set plain [read $ifid $stats(size)] ;
close $ifid
append plain [string repeat "\000" $padnum]
set cipher [blowfish::blowfish -mode cbc -key $key -dir \
encrypt -iv $iv $plain]
set ofid [open "cipher.dat" "w"]
fconfigure $ofid -translation binary
puts -nonewline $ofid $cipher
close $ofid

file stat cipher.dat stats
set ifid [open "cipher.dat" "r"]
fconfigure $ifid -translation binary
set cipher [read $ifid $stats(size)]
close $ifid
#Write back to file
set ofid [open "plain2.txt" "w"]
fconfigure $ofid -translation binary
puts -nonewline $ofid [string trim [blowfish::blowfish -mode cbc \
-key $key -dir decrypt -iv $iv $cipher] "\000"]
close $ofid
[/code]