Solved

Reverse expression

Posted on 2007-12-06
9
137 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 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
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

 
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

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Basic Regex for use in webservice 1 64
Regex or operator problems 8 39
Need some help with grep 7 96
regex code to filter this ip's? 2 37
Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
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…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
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…

772 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