A wierd one, this. I'm using AES to encode a binary file and then decode
it elsewhere, hoping it'll come out the other end unmangled. Alas, it
isn't happening that way.
In testing, I've swapped out AES in the encode/decode process and replaced
it with base64 and the exact same characters get mangled, so it looks like
I'm doing something wrong in the file IO rather than in encoding/decoding.
First up, the result of `cmp ./oldfile ./newfile` is:
110 232 77
667 202 77
675 205 77
873 12 15
874 164 12
875 145 164
876 170 145
(and so on..)
(In the above, the first number's the byte number, the second is the
byte for ./oldfile and the third is the byte for ./newfile.)
So almost everything's going right initially, just an occasional
character is screwed up. But in an encoding environ, that causes the
file to get progressively more broken.
The code I'm using for the IO and encode:
# Read unencoded raw data in.
set infile [open $dbfile "r"]
fconfigure $infile -encoding binary -translation binary -buffering none
set data [read $infile]
# Write data out.
set outfile [open $kfile "w"]
fconfigure $outfile -encoding binary -translation binary -buffering none
set cryptotext [::base64::encode $data]
puts -nonewline $outile $cryptotext
close $outfile
Then to decode:
set infile [open $kfile "r"]
fconfigure $infile -encoding binary -translation binary -buffering none
set cryptotext [read $infile] ; close $infile
set data [::base64::decode $cryptotext]
set filename "newfile"
set outfile [open $filename "w"]
puts -nonewline $outfile $data
close $outfile
For transmitting base64 encoded data you do not need to set the channel
to binary, but it doesn't hurt either.
Michael
Hi Synic,
you are reading the file with encoding binary, write and read to/from
an intermediate file (without changing the data) and write it out
with encoding system. See your first read and your last write
commands.
encoding binary on base64 encoded data is not necessary, base64
encodes to ASCII text.
Kind regards
Ulrich
This is good, correct so far...
> Then to decode:
>
> set infile [open $kfile "r"]
> fconfigure $infile -encoding binary -translation binary -buffering none
> set cryptotext [read $infile] ; close $infile
> set data [::base64::decode $cryptotext]
> set filename "newfile"
> set outfile [open $filename "w"]
Aha! You forgot to [fconfigure $outfile -translation binary] here.
Cheers for that. Amazing what your eyes miss when coding at 3AM :-).
Yep. I was mainly using base64 as a troubleshooting tool to isolate
whether it was anything to do with the AES encryption stuff or
whether it was the file IO.
As the errors were in the same places in the data no matter whether
using AES or base64 indicated that the issue was on the IO side.
In the code I'm working on, the AES is back in, the base64 is back
out, and all's working nicely. Cheers to everyone who posted in the
thread.