Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Binary file reading issues (after translation)

21 views
Skip to first unread message

Synic

unread,
Apr 16, 2006, 3:32:00 PM4/16/06
to
Hi guys.

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

Michael Schlenker

unread,
Apr 16, 2006, 5:52:33 PM4/16/06
to
Synic schrieb:
You don't set your output channel to -translation binary (-translation
binary takes care of the encoding binary too).

For transmitting base64 encoded data you do not need to set the channel
to binary, but it doesn't hurt either.

Michael

Ulrich Schöbel

unread,
Apr 16, 2006, 5:56:23 PM4/16/06
to

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

sleb...@gmail.com

unread,
Apr 16, 2006, 11:44:46 PM4/16/06
to
Synic wrote:
> # 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

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.

Synic

unread,
Apr 17, 2006, 8:59:27 AM4/17/06
to
sleb...@yahoo.com <sleb...@gmail.com> wrote:
> Aha! You forgot to [fconfigure $outfile -translation binary] here.

Cheers for that. Amazing what your eyes miss when coding at 3AM :-).

Synic

unread,
Apr 17, 2006, 9:08:01 AM4/17/06
to
Ulrich Schöbel <ulr...@outvert.com> wrote:
> encoding binary on base64 encoded data is not necessary, base64
> encodes to ASCII text.

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.

0 new messages