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
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:$file->open("+<log.log") or die "$!";
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.IO::File=0x7863454859
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:
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.$data = join("", $file->getlines);
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:
Which will wipe the file and insure that the print starts at the beginning of the file.$file->truncate(0);
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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |