Hi, I've been trying to write a simple script to encrypt and decrypt data from a file. I was trying to use the RC4.pm module. I'm running Redhat Enterprise on an Intel P4 machine. The version of Perl on my machine is 5.8.0. Here's the problem. I've written some code that will correctly encrypt and decrypt a message when the message is a string that is defined in the program, but fails to work properly with the string is read in from a file. Here's the code
#!/usr/bin/perl -w use strict; use diagnostics; use Crypt::RC4; #grab command line args and get keyphrase my $key_phrase = $ARGV[0]; my $cleartext_file = $ARGV[1]; my $cyphertext_file = $ARGV[2]; chomp($key_phrase); #define some variables my $plaintext; my $cyphertext; my $plaintext2; #complain if mis-used if ($#ARGV <2) { die "Usage crypt_file.pl <key phrase> <cleartext file> <cyphertext f +ile> "; } #open the plaintext file open INFILE, "<$cleartext_file" or die "Can't open input file: ",$cleartext_file,"\n"; my $cph_in = new Crypt::RC4($key_phrase); undef $/; #$plaintext = <INFILE>; close(INFILE); $plaintext = "This is a test\n with a line-break in it\n"; print the plaintext to check it print "+++++++++++++++plain text+++++++++++++++++++++++\n"; print $plaintext; $cyphertext = $cph_in->RC4($plaintext); my $cph_out = new Crypt::RC4($key_phrase); $plaintext2 = $cph_out->RC4($cyphertext); open OUTFILE, ">$cyphertext_file"; print OUTFILE $cyphertext; print "\n----------------cypher text---------------------\n"; print $cyphertext; close(OUTFILE); print "\n++++++++++++++plain text again +++++++++++++++++\n"; print $plaintext2;
Looking at the line that says $plaintext = "This is a test \n with a line-break in it\n";. If I leave this line in, the script works fine, prints out the plain text, then the encrypted text and then the correct plaintext again. If I now comment out this line and uncomment out the file read statement, and then read from a file containing exactly the same text, the program does not create the same encryption, and cannot decrypt what it produces back to clear text. I looked at the output of RC4.pm and it looks to me like it inserts some ascii codes into the output string when the string is read from a file, but not when the string is defined as a variable in the program. At this point, I am stumped and am likely missing something very obvious. Any insight would be greatly appreciated. Could it be something in my perl installation is hosed? thanks j.

In reply to Help with RC4 by jmamer

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.