|
|
OverviewEncrypting a word by the crypt function of Perl. This can be used for password encrypting. Most of my CGIs use this routine.
Process
A sample code
my $crypted_text = &makecrypt("plain_text");
sub makecrypt {
my $plain = shift;
my $salt = join "", ('.', '/', 0..9, 'A'..'Z', 'a'..'z')[rand 64, rand 64];
my $result = crypt($plain,$salt) or crypt($plain,'$1$'.$salt.'$');
return $result;
}
Description of the code
my $crypted_text = &makecrypt("plain_text");
This code encrypts the word
sub makecrypt {
my $plain = shift;
my $salt = join "", ('.', '/', 0..9, 'A'..'Z', 'a'..'z')[rand 64, rand 64];
my $result = crypt($plain,$salt) or crypt($plain,'$1$'.$salt.'$');
return $result;
}
A encryption routine. First, generating a salt consisting of two characters. This routine encrypts a given word with the salt value. If your server only supports MD5 and does not support DES, the salt format has to be a word between $1$ and $1. In this case, the first crypt function returns a false and retry crypt using a salft value of $1$..$. Servers in these days ususally support DES. Therefore the first crypt function would work in most of cases and the second crypt funtion would not be executed. The result of the encryption will differ even though original plain text is the same because the salt value is generated randomly every time. |