?
Solved

Reverse expression

Posted on 2007-12-06
9
Medium Priority
?
146 Views
Last Modified: 2010-03-05
I am trying to reverse my regular expression..I have it encrpyt then store in the database..then print it, but before it prints it decrypt it before it prints.

Any suggestions? i thought I had it but I guess not.

ENCRYPT:

      # encrypt then store in database
      # replace a-k with 0-9
      $password  =~ tr/[a-k]/[0-9]/;

DECRYPT:

      # decrypt then print
      # replace a-k with 0-9
      my $password  =~ tr/[0-9]/[a-k]/;

#!/usr/bin/perl
use CGI qw(:standard);
use CGI::Carp qw(fatalsToBrowser); # errors will fall to browser and be printed
use DBI;
use strict;
 
print "Content-type:text/html \n\n";
 
# username and password for the user
my $acc_num   = param('acc_num');
my $password  = param('password');
my $action    = param('action');
 
# login information
my $database  = "rcoughlin32001";
my $dbuser    = "rcoughlin32001";
my $dbpass    = "lacrosse";
 
# select, type, database, server
my $dsn       = "DBI:mysql:$database:localhost";
 
# connect
my $dbh       = DBI -> connect($dsn, $dbuser, $dbpass, {RaiseError => 1}) or die "Error connecting: $DBI::errstr\n";
 
# if create account was pushed
if($action eq 'Create Account'){
 
	# encrypt then store in database
	# replace a-k with 0-9
	$password  =~ tr/[a-k]/[0-9]/;
	
	# store data in database
	my $query_create = $dbh -> prepare("INSERT INTO bookaccounts (ID, acc_num, password) VALUES (null, '$acc_num', '$password')");
	my $done  = $query_create -> execute();
	
	if($done){
		print "Added to database.<br />";
	}else{
		print "Didn't add to database. <a href=\"../booksonline.html\">Click</a> to go back.<br /><br />";
	}
	# for debug
	print "<b>OUTPUT</b><br /><br />";
	print "<b>Account Number:</b> " . $acc_num . "<br/>";
	print "<b>Password:</b> " . $password;
	
# if retrieve password was pushed
}elsif($action eq 'Retrieve Password'){
	
	# decrypt then print
	# replace a-k with 0-9
	my $password  =~ tr/[0-9]/[a-k]/;
	
	# store data in database
	my $query_retrieve="SELECT password,acc_num FROM bookaccounts WHERE acc_num=\"$acc_num\"";
	my $sth = $dbh->prepare($query_retrieve);
	   $sth-> execute;
	   
	# generate field in to array and print
	my @data = $sth->fetchrow_array(); 
	
	print "Password: ". $data[0];
}

Open in new window

0
Comment
  • 5
  • 4
9 Comments
 
LVL 85

Expert Comment

by:ozo
ID: 20424719
try removing the my from  my $password  =~ tr/[0-9]/[a-k]/;

BTW, the [] are not needed here
0
 
LVL 85

Expert Comment

by:ozo
ID: 20424732
#Or did you mean

  $data[0] =~ tr/[0-9]/[a-k]/; # or  tr/0-9/a-k/;
  print "Password: ". $data[0];
0
 
LVL 1

Author Comment

by:catonthecouchproductions
ID: 20424735
I did that and I have this now:

       $password  =~ tr/0-9/a-k/;


Still when I ran it outputted the encrypted one.
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 1

Author Comment

by:catonthecouchproductions
ID: 20424747
Well here is the one for encrypt:

      $password  =~ tr/[a-k]/[0-9]/;

Decrypt:

       $password  =~ tr/0-9/a-k/;

I want to reverse it on output..so

T4est becomes Test on output.

Ryan
0
 
LVL 85

Expert Comment

by:ozo
ID: 20424774
If you want $password to become Test after doing

       $password  =~ tr/0-9/a-k/;

you should have set $password to T4est before
0
 
LVL 85

Accepted Solution

by:
ozo earned 2000 total points
ID: 20424781
In any case, you never print  $password  after the tr/0-9/a-k/ or  tr/[0-9]/[a-k]/
you only print $data[0]
0
 
LVL 1

Author Comment

by:catonthecouchproductions
ID: 20424793
Ahh.I see..how would I print that out? Just print $password?

I have this:

      # generate field in to array and print
      my @data = $sth->fetchrow_array();
      
      print "Password: ". $data[0];
0
 
LVL 85

Expert Comment

by:ozo
ID: 20424815
If param('password') contains T4est, then
   $password  = param('password');
   $password  =~ tr/0-9/a-k/;
   print  $password;
should print
Teest

If $data[0] contains T4st, then
   $data[0] =~ tr/[0-9]/[a-k]/; # or  tr/0-9/a-k/;
  print $data[0];
should print
Test
0
 
LVL 1

Author Comment

by:catonthecouchproductions
ID: 20424839
I got it! Its printing the right output..thanks for your help!

I appreciate it.

Thanks,
Ryan
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Six Sigma Control Plans
Suggested Courses

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question