Solved

Reverse expression

Posted on 2007-12-06
9
141 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
9 Comments
 
LVL 84

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 84

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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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 84

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 84

Accepted Solution

by:
ozo earned 500 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 84

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

[Webinar] Learn How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

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…
There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
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

728 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