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.

In reply to File ownership issues causing file decryption problems? by hmbscully

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.