common has asked for the wisdom of the Perl Monks concerning the following question:

this is my first post so i hope i'm doing this right well, i'm trying to read from a file and encrypt and decrypt it, it seens to encrypt fine but upon decrypting it returns garbled text. i'm useing IO::File to do filehanding and use Crypt::Rijndael_PP ':all' for encryption on win2k pro.
sub encrypt { print "enter key: "; $key=<>; $file->open("+>log.log") or die "$!"; $data = $file; $c_txt = rijndael_encrypt($key, MODE_CBC, $data, 256, 128); $file->print($c_txt);}
lets say i put 'blah' in log.log with a key of 'mo' i get: ð@R`óÎÞèDÓ³83WPõgç×¥^DPÄ“°CCô" which seems fine but when i decrypt it:
sub decrypt { print "enter key: "; $key=<>; $file->open("+>log.log") or die "$!"; $c_txt = $file; $p_txt = rijndael_decrypt($key, MODE_CBC, $c_txt, 256, 128); $file->print($p_txt);}
i get: Ü¥¢Ãs´÷µ|˜+Ûûˆ<g what i'm thinking is that it's the dos line feeds but i don't know how to get rid of them i tried:
$file =~ s/\r$//; and $file =~ s/\n$//;
but that doesn't work, so basically what i'm asking is how to get it to read and write correctly. any ideas? thanks.

Replies are listed 'Best First'.
Re: incorrect en/decryption when reading from file
by fokat (Deacon) on Jul 26, 2002 at 01:54 UTC
    Well, take a look at this:

    sub decrypt {
    print "enter key: ";
    $key=<>;
    $file->open("+>log.log") or die "$!";
    $c_txt = $file;
    $p_txt = rijndael_decrypt($key, MODE_CBC, $c_txt, 256, 128);
    $file->print($p_txt);}

    The line in bold should probably read

    $file->open("+<log.log") or die "$!";
    So that it is not clobbered (looks like this is where clear/cypher text comes from). I am not familiar with these Crypt modules, but I guess you're not actually reading the file but simply encypting and decrypting a string that looks like:

    IO::File=0x7863454859
    Which is the way references stringify. You can also use ->seek() to go back to the begining of the file and read from it, but this seems more counter-intuitive, more inefficient and obscures the purpose of your code.

    The line breaks will be handled transparently for you by Perl, so you do not need to worry about them. You could as well treat the file as binary so that it is OS-neutral.

    For reading from the file, you might do something like:

    $data = join("", $file->getlines);
    But beware, af this might exhaust your memory if you try to encrypt a huge file, as it will try to read it all at once and stuff it into a scalar. Also, this won't necesarilly work well with a binary file.

    Additionally, you seem to want to leave the encrypted or decrypted file in-place. To do this, you should truncate it prior to printing it. I don't know if truncate is supported in Windows, but if it is, add this before your print:

    $file->truncate(0);
    Which will wipe the file and insure that the print starts at the beginning of the file.

    Regards.

Re: incorrect en/decryption when reading from file
by common (Acolyte) on Jul 26, 2002 at 02:31 UTC
    you're quite correct, i'm surprised i missed somthing so simple as <$file>. i got it working though, so thank you.
Re: incorrect en/decryption when reading from file
by gmpassos (Priest) on Jul 26, 2002 at 06:46 UTC
    You need to read/write it as binary, specially on Win2k! just use this after the open:

    binmode($file) ;

    "The creativity is the expression of the liberty".