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

I have an html form which upon submission, process the submitted variables and builds a fixed-length string that is then written into a flatfile. A plain text solution works fine, but I am not attempting to encrypt the data.

This is the code I put together to prove my method for encrypting the data:

#!/usr/bin/perl use strict; use warnings; use Crypt::Rijndael; use Crypt::CBC; use Fcntl ":flock"; #-------------------------------------------------------------------- # Parameters #-------------------------------------------------------------------- my $my_key; my $plain_text; my $cipher; my $buffer; my $asr_file = "wd_file"; my $decrypted = "$asr_file.txt"; #line below is an example of the 1850 character line the larger cgi sc +ript will build my $asr_line = '2AD11280700007211/28/07 16:27:42TTLASTNAME + TTFIRSTNAME T123457895 01011983TT_ADDRESS + TTCITY ALTT_ZIP 00106160304F + + + + + + + + + + + + + + + + + + + + + 02450V444433 +33222211110112WENDY XXXXXX LASTNAME FIRSTNAMEMADDRE +SS CITY AL12345 001319 +3412287WXXXXXX@GMAIL.COM + '; $my_key = `openssl enc -bf-cbc -d -in key_file.txt -k encrypt`; $cipher = Crypt::CBC->new( {'key' => $my_key, 'cipher' => 'Rijndael', 'regenerate_key' => 1, 'padding' => 'standard', }); #-------------------------------------------------------------------- # Decryption #-------------------------------------------------------------------- + open(FH_crypted, "<$asr_file.enc"); #open original encrypted file flock FH_crypted, LOCK_EX; #lock original encrypted file binmode FH_crypted; open(FH_decrypted, ">$decrypted"); #open plain textfile to hold decryp +ted data flock FH_decrypted, LOCK_EX; #lock original encrypted file binmode FH_decrypted; $cipher->start('decrypting'); while (read(FH_crypted,$buffer,1024)) { print FH_decrypted $cipher->crypt($buffer); #write the decrypte +d lines to plain text } print FH_decrypted $cipher->finish; close FH_crypted; #close the original encrypted file print FH_decrypted "$asr_line\n"; #add the new line to the plain text +file close FH_decrypted; #close the new plain text file #-------------------------------------------------------------------- # Encryption #-------------------------------------------------------------------- open(FH_plain,"./$decrypted"); flock FH_plain, LOCK_EX; #lock plain text file binmode FH_plain; open(FH_crypted, ">$asr_file.enc"); flock FH_crypted, LOCK_EX; #lock new encrypted file binmode FH_crypted; $cipher->start('encrypting'); while (read(FH_plain,$buffer,1024)) { print FH_crypted $cipher->crypt($buffer); } print FH_crypted $cipher->finish; close FH_plain; unlink $decrypted; #delete the plain text file close FH_crypted;

I have a separate decrypt-only script because the data will ultimately be decrypted on a different box.

#!/usr/bin/perl use strict; use warnings; use Crypt::Rijndael; use Crypt::CBC; #-------------------------------------------------------------------- # Parameters #-------------------------------------------------------------------- my $my_key; my $plain_text; my $cipher; my $buffer; my $plainfile = "act"; my $decrypted = "act_decrypted.txt"; $my_key = `openssl enc -bf-cbc -d -in key_file.txt -k encrypt`; $cipher = Crypt::CBC->new( {'key' => $my_key, 'cipher' => 'Rijndael', 'regenerate_key' => 1, 'padding' => 'standard', }); #-------------------------------------------------------------------- # Decryption #-------------------------------------------------------------------- open(FH_crypted, "<$plainfile.enc"); binmode FH_crypted; open(FH_decrypted, ">$decrypted"); binmode FH_decrypted; $cipher->start('decrypting'); while (read(FH_crypted,$buffer,1024)) { print FH_decrypted $cipher->crypt($buffer); } print FH_decrypted $cipher->finish; close FH_crypted; close FH_decrypted;

Running both these scripts from the command line produces the results I want: a successfully decrypt-able file. However, when I attempt to integrate the code into my larger script that formats the variables and returns an html page, the file will not decrypt. It appears to encrypt correctly (the byte size is what I expect it to be), but trying to decrypt it results in different, but still unreadable text.

The only difference I can see between the command line created file and the cgi created file is that the cgi script version ends up belonging to the 'apache' user, while all the other files are owned by the 'webmster' user. Does this seem like it would cause the problem I am describing? If so, how do I deal with it?

Thanks!

SOLUTION:
$my_key = `openssl enc -bf-cbc -d -in key_file.txt -k encrypt`; was the problem. Specifically key_file.txt didn't have enough read permissions to allow a different user to read from it. D'oh!


I learn more and more about less and less until eventually I know everything about nothing.

Replies are listed 'Best First'.
Re: File ownership issues causing file decryption problems?
by ikegami (Patriarch) on Dec 05, 2007 at 16:48 UTC
    Did openssl run successfully? Does $my_key contain the same thing in the CGI version as it does in the command line version?
      That is it. It was a permissions issue on the key_file.txt file which wasn't written to throw an error if it wasn't opened. How embarrassing. But at least there is a solution.

      I learn more and more about less and less until eventually I know everything about nothing.
Re: File ownership issues causing file decryption problems?
by glide (Pilgrim) on Dec 05, 2007 at 15:29 UTC
    Hi,
    change the user to the apache, and run your test script again. Probable the problem it's not there, but ...
    And in the CGI, save the file before encryption and check if it's correct.
    And the last, but not the least debug. If you hare using the CGI you can do the debuging easily in the command line.
    ps. this node it's interesting Debugging a CGI.