Encryption with the Crypt Function in Perl
To encrypt with the crypt function in Perl:
$ji="12Fr7";
$ji=crypt($ji,"AP");
print "$ji";
-We created a variable
ji and stored a password there.
-We called the built-in crypt function.
-Inside the parenthesis, we first state what is to be crypted ($ji)
-The second part in the parenthesis, after the comma, is the
salt which consists of any 2 letters or numbers.
-We print the results.
NOTE: There is no decrypt. The crypt function is used for checking crypted input against crypted stored...
In other words:
-a user types their password in a form
-you crypt it
-you check it against their previously stored crypted password
Another example that makes for more varied encryption:
$ji="12Fr7";
$ssalt=$ji;
$ji=crypt($ji,$ssalt);
print "$ji";
-Again, we created $ji.
-We stored $ji into a variable named
$ssalt
-We used our new $ssalt variable to crypt $ji
-Essentially, we used $ji to crypt $ji, but that might be confusing to look at, codewise :)
IN CASE YOU'RE WONDERING: Although our new variable $ssalt is five characters long, the crypt function uses only the first two letters when making the salt. *
* Thanks to Randal L. Schwartz for correcting me about this on the iLTC Google Group.
Copy and Paste Perl Code:
#!/usr/bin/perl
print "Content-type:text/html\n\n";
$ji="12Fr7";
$ji=crypt($ji,"12");
print "$ji<br>";
$ji="12Fr7";
$ssalt=$ji;
$ji=crypt($ji,$ssalt);
print "$ji<br>";
$ji="12Fr7";
$ji=crypt($ji,$ji);
print "$ji";
exit;
Resources:
Substitute in Perl
Matching with a Regular Expression, REGEX, in Perl
Getting What is Matched in Perl
Matching at the Beginning and End
Matching at the Beginning and End 2
Click HERE to comment or discuss at iLoveTheCode GOOGLE Group
| | | |
| | | |
CSS David McFarland | | |