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.


In reply to Re: incorrect en/decryption when reading from file by fokat
in thread incorrect en/decryption when reading from file by common

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.