I'm quite new in perl. Trying a password changing form through perl with encrypt / decrypt. Below is my code -
#!/usr/bin/perl local ($buffer, @pairs, $pair, $name, $value, %FORM); # Read in text $ENV{'REQUEST_METHOD'} =~ tr/a-z/A-Z/; if ($ENV{'REQUEST_METHOD'} eq "POST") { read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'}); }else { $buffer = $ENV{'QUERY_STRING'}; } # Split information into name/value pairs @pairs = split(/&/, $buffer); foreach $pair (@pairs) { ($name, $value) = split(/=/, $pair); $value =~ tr/+/ /; $value =~ s/%(..)/pack("C", hex($1))/eg; $FORM{$name} = $value; } my $old_pass = $FORM{oldpass}; my $new_pass = $FORM{newpass1}; my $new_pass2 = $FORM{newpass2}; print "Content-type:text/html\r\n\r\n"; use CGI; use warnings; use strict; use Crypt::CBC; my $KEY = 'secrect_foo'; my $readfile ="pass.txt"; open (my $passfile, '<', $readfile) or die "could not open fil +e"; my $readpass = do{ local $/; <$passfile> }; # print "Reading pass from file: $readpass\n"; close ($passfile); my $dec = decryptString($readpass); if($dec eq $old_pass) { print "<h2>Password Matched<h2>\n"; if($new_pass eq $new_pass2) { my $enc = encrypString($new_pass); print "encrypted binary: $enc\n"; my $storedpassfile = 'pass.txt'; open(my $fh, '>', $storedpassfile) or die "Cou +ld not open the pass file"; print $fh "$enc"; close $fh; print "New Password Updated\n"; } else { print "New pass & Confirm Pass is not same"; } } else{ print "Old Pass & Stored Pass mismatch\n"; } sub encrypString { my $string = shift; my $cipher = Crypt::CBC->new( -key => $KEY, -cipher => 'Blowfish', -padding => 'space', -add_header => 1 ); my $enc = $cipher->encrypt($string); return $enc; } sub decryptString { my $string = shift; my $cipher = Crypt::CBC->new( -key => $KEY, -cipher => 'Blowfish', -padding => 'space', -add_header => 1 ); my $dec = $cipher->decrypt($string); return $dec; }
Problem is while everything alright - oldpass and both newpass matched then program endup at " print "encrypted binary: $enc\n"; " and not updating the pass.txt file. Where possible I've made mistake ? Any advise is highly appreciable.

In reply to Getting issue with encrypt / decrypt by merafiq

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.