?
Solved

Reverse expression

Posted on 2007-12-06
9
Medium Priority
?
143 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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 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 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

Enroll in August's Course of the Month

August's CompTIA IT Fundamentals course includes 19 hours of basic computer principle modules and prepares you for the certification exam. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

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

by Batuhan Cetin Regular expression is a language that we use to edit a string or retrieve sub-strings that meets specific rules from a text. A regular expression can be applied to a set of string variables. There are many RegEx engines for u…
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
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

765 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