I've written the following simple program using Crypt:CBC to encrypt/decrypt the contents of a file. The file has multiple lines of data (with a newline character terminating each line) The problem is that once we encrypt the data file and then decrypt it..... the last line in the data file is only partially reproduced. It basically truncates the last line in the file. Here is the code:
#!/usr/perl5/bin -w use Crypt::CBC; $inFile = "test.dat"; $cryptFile = "Crypt.dat"; $decrypFile = "DeCrypt.dat"; if ($#ARGV < 0) { print STDOUT "Invalid number of command line parameters! \n"; exit(1); } $mode = shift @ARGV; unless (($mode == 0) || ($mode == 1)) { print STDOUT "Invalid command line argument1 \n"; exit(2); } $cipher = Crypt::CBC->new( { 'key' => '18829298', 'cipher' => 'DES', 'iv' => '87654321', 'regenerate_key' => 0, 'prepend_iv' => 0 } ); if ($mode == 0) { open(INFILE, "<$inFile") || die "Error opening source file for inp +ut! \n"; open(CRYPTFILE, ">$cryptFile") || die "Error opening crypt file fo +r output! \n"; $cipher->start('encrypting'); while (<INFILE>) { print CRYPTFILE $cipher->crypt($_); } close INFILE; close CRYPTFILE; } else { open(CRYPTFILE, "<$cryptFile") || die "Error opening crypt file fo +r input! \n"; open(DECRYPFILE, ">$decrypFile") || die "Error opening decrypt fil +e for output! \n"; $cipher->start("decrypting"); while (<CRYPTFILE>) { print DECRYPFILE $cipher->crypt($_); } close CRYPTFILE; close DECRYPFILE; }
Any inputs as to why this is happening would be great to have! Thanks, KM

In reply to Crypt::CBC help! by Anonymous Monk

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.