Solved

Reverse expression

Posted on 2007-12-06
9
134 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

I have been reconstructing a PHP-based application that has grown into a full blown interface system over the last ten years by a developer that has now gone into business for himself building websites. I am not incredibly fond of writing PHP code o…
Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
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…

895 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now