Click here to Skip to main content
15,884,425 members
Please Sign up or sign in to vote.
2.50/5 (2 votes)
See more:
I need help in a program I'm trying to run. I am new to Perl and thought this was right but it's not working. This form was to allow a user to enter a 5 digit account number & a passwd that must include 6-10 characters. I have to use the tr/// operator to encrypt the passwed before saving it to the database, however if they click "show me my password" it should display it unecrypted. Does anyone see my error? When I run it, my program thinks I'm trying to save another account and giving me the error message that's in my add() function. But it shouldn't even be going there if the user has clicked "Tell Me My Password".

PERL
print "Content-type: text/html\n\n";
use CGI qw(:standard);
use SDBM_File;
use Fcntl;

#prevent perl from creating undeclared variables
use strict;

#declare variables
my ($button, $account, $password);

#assign values to variables
$button = param('Button');
$account = param('Account');
$password = param('Pass');

if(length($account) == 5){
   if ($button == "Create Account") {
        add();
   }
   elsif ($button == "Tell Me My Password") {
     retrieve();
   }
}else{
      print "<HTML>\n";
     print "<HEAD><TITLE>Books Online</TITLE></HEAD>\n";
     print "<h1>Please return to the form.</h1>\n";
     print "Enter a valid account number. \n";
     print "\n";
}
exit;
#*****user-defined functions*****

sub add {
  my %accountinfo;

  if(length($password) >= 6 && length($password) <= 10){
     tie(%accountinfo, "SBDM_File", "c09case2", O_CREAT|O_RDWR, 0666)
           or die "Error opening c09case2. $!, stopped";
     $password =~ tr/abcdefghij/1234567890/;
     $accountinfo{$account} = $password;
     untie(%accountinfo);

      #create Web page
      print "<HTML>\n";
      print "<HEAD><TITLE>Books Online</TITLE></HEAD>\n";
      print "<h1>Books Online</h1>\n";
      print "Your information has been recorded.\n";
      print "\n";
   } else {
     print "<HTML>\n";
     print "<HEAD><TITLE>Books Online</TITLE></HEAD>\n";
     print "<h1>Please return to the form.</h1>\n";
     print "Enter a valid password. \n";
     print "\n";
   }
  }# end add

sub retrieve{
   my %accountinfo;
   my $pass_info;
  tie(%accountinfo, "SBDM_File", "c09case2", O_CREAT|O_RDWR, 0666)
        or die "Error opening c09case2. $!, stopped";
  $pass_info = $accountinfo{$account};
  untie(%accountinfo);
  $pass_info =~ tr/1234567890/abcdefghij/;
   #create Web page
   print "<HTML>\n";
   print "<HEAD><TITLE>Books Online</TITLE></HEAD>\n";
   print "<h1>Your password is</h1>\n";
   print "$pass_info \n";
   print "\n";
}
Posted
Updated 11-Apr-11 4:33am
v3

1 solution

Have you tried logging the value of $button that is received by this code (e.g. I notice in your posting you call it both "show me my password" and "Tell Me My Password")? That might help you debug it.

I should also note that tr certainly isn't any kind of encryption and would only stop the most casual viewer, if that. Do you really need to tell people their passwords? It is not good design to store passwords in any retrievable way (as many large companies can attest!). Rather you should use a one-way hash, and if someone forgets their password, you re-set it to a random string and e-mail that string to their contact address for confirmation and re-setting (generally within a time limit).

cheers,
Don
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900