Perl Tutorial : Perl data types
Perl Tutorial : Perl Operators
Perl Tutorial : Perl loops
Perl Tutorial : Perl Conditionals
Perl Tutorial : Perl Subroutines
Perl Tutorial : Perl regex
Perl Tutorial : Perl Files
Perl Tutorial : Perl & Databases
Perl Tutorial : Perl OOPs
Perl Tutorial : Perl signals
Perl Tutorial : Perl threads
Perl Tutorial : Perl Debugging

Introcution to Perl CGI

Introduction to modperl
Home >> perlfunctions >>

Perl function: crypt

Syntax:
crypt STR, SALT
Encrypts STR based on the encrypted version specified by SALT. It's a one way function, meaning there is no corresponding function to decrypt STR once it has been encrypted. Hence, though it cannot be used for cryptography, it can be used to store and check password.

When a user enters a password for storage, it is encrypted using a specific salt and stored. When the user enters the password again for authentication, it is again encrypted using the same salt and matched with the previously stored password. If both match, the system recognises the password as correct.

Example:

print "Enter the string \n";
#read string and remove new line
chomp(my $str = );
print "Enter two random alphanumerics as salt \n";
#read salt and remove new line
chomp(my $salt = );
my $enc_str = crypt($str,$salt);
print "Encrypted string is $enc_str";
The above example takes the input string and salt from the user. The SALT can be any two alphanumerics which are stored in the encrypted string as the first two characters to compare later on. To check if a user entered string matches enc_str, we add the following code to the above example:
print "Enter the string to check \n";
chomp(my $chk_str = );
if ($enc_str eq crypt ($chk_str, $enc_str))
{
    print "Strings match";
else
    print "Strings don't match";
}
The crypt functions above uses chk_str and enc_str because SALT is only two characters and the first two characters of enc_str, previously encrypted string are the SALT.

A little change in the value of STR or SALT will result in large changes in the resulting encrypted string and decoding it is next to impossible.


Perl functions

scalars and strings

lc
ord

regex (regular expressions)

arrays

shift
split

More..

eval
Share |

List of all perl functions
Perl function : split
Perl function: abs
Perl function: chomp
Perl function: chop
Perl function: chr
Perl function: eval
Perl function: hex
Perl function: lc
Perl function: lcfirst
Perl function: length
Perl function: oct
Perl function: ord
Perl function: shift

Perl examples

  • How to use Data::Dumper?
  • How to parse config file?
  • Include external module in perl..
  • Download perl

    download stable version of perl
    Perl 5.10.1
    Perl interview Questions
    sed tutorial
    awk tutorial

    Join us on Google Groups

    Subscribe to perl
    Email:
    Visit this group
    Perl Jobs


    privacy | sitemap | disclaim | contact us
    © 2009 perlhome.com