分类: LINUX
2006-03-20 17:44:20
NAME crypt - password and data encryption SYNOPSIS #define _XOPEN_SOURCE #include char *crypt(const char *key, const char *salt); |
salt is a two-character string chosen from the set [a-zA-Z0-9./]. This string is used to perturb the algorithm in one of 4096 different ways. |
GNU EXTENSION The glibc2 version of this function has the following additional features. If salt is a character string starting with the three characters "$1$" followed by at most eight characters, and optionally terminated by "$", then instead of using the DES machine, the glibc crypt function uses an MD5-based algorithm, and outputs up to 34 bytes, namely "$1$ |
if(strcmp(encryt_passwd, crypt(user_input, encrypt_passwd)) == 0){ goto: login_ok; }else{ goto: login_failed; } |
#!/usr/bin/perl # use strict; use warnings; sub get_salt { my $valid_chars = "abcdefghijklmnopqrstuvwxyz" . "ABCDEFGHIJKLMNOPQRSTUVWXYZ" . "0123456789" . "./"; my $ret; foreach (0..7){ $ret .= substr($valid_chars, int(rand(length($valid_chars))), 1); } return $ret; } # Get the password from STDIN. sub get_passwd { my $pass; my $repass; system("stty -echo"); print "Please input your password: "; $pass = print "\nReinput your password for conform: "; $repass = if($pass ne $repass){ print "\nThe passwords do not match.\n"; system("stty echo"); return ""; } system("stty echo"); # method 1: # if($pass =~ /(.*)\n/){ # $pass = $1; # } # method 2: # ($pass, ) = ($pass =~ /(.*)\n/); # method 3: # ($pass, ) = split "\n", $pass; # method 4: chomp($pass); print "\n"; return $pass; } while(1){ my $passwd = get_passwd; if($passwd ne ""){ my $salt = get_salt; $salt = "\$1\$$salt\$"; print crypt($passwd, $salt) . "\n"; last; } } |