. <
lmn...@gmail.com> wrote:
> Problem Statement:
> There is a tcl file (myProcs.tcl) that I need to encrypt, decrypt,
> then source in a tclsh shell.
>
> However, I find at the end of the decrypted file there are extra
> "encrypted chars" such that when it is sourced in a tclsh shell the
> last line in the file throws an error. (i.e. "source myProcs.tcl")
> (testcase below)
Yes, block ciphers (of which blowfish is one) always operate on data
that is an exact multiple of the block cipher's primitive size (8 bytes
for blowfish).
In cryptogolgy, the handling of arbitrary size inputs while encrypting
with a block cipher is a task that is to be taken care of at a
higher level than the block cipher itself (i.e., the block cipher
always sees a multiple of 8 bytes, and some other code handles the
size missmatch).
You have a couple options:
1) store the length of your input prior to encryption somewhere, use
that stored length to truncate the decrypted output back to the
original size
2) add code to provide one of the standard cryptographic padding
primitives prior to passing the data to blowfish, and remove the
standard paddding after decryption (this does not require a separate
length storage. A description of several standard crypto padding
primitives can be found here:
https://en.wikipedia.org/wiki/Padding_(cryptography)
Please note that some of these padding primitives also introduce
weaknesses that allow for decryption in a time less than brute force
(although don't read that as "allow decryption without having key",
for a cryptographer, less than brute force can still mean
"impossible to do in a reasonable time").
3) assuming the -pad option functions properly, and as long as your
input will always be a Tcl script, provide a control-z (EOF)
characters as the parameter to -pad so that the blowfish proc will
pad with ASCII EOF characters. Tcl's source command considers ASCII
EOF to indicate the end of the input file for sourcing Tcl code
(also clearly documented in the 'source' manual page). So when you
source the result of the decrypt, the EOF padding characters will be
legal "end of input" terminators and everything should work.
Option 3, assuming the -pad option functions as documented, and
assuming you always plan to encrypt Tcl scripts, is likely to be the
easiest alternative for you.