分类: LINUX
2008-08-19 17:21:55
linux shell 里面竟然没有直接输出crypt加密字符串的命令,郁闷。本人又不喜欢 perl
encrypt.c
#include
#include
#include
#include
#include
// Password longest 8 bit , Key longest is 2 bit , salt( 0-9a-zA-Z./ ) is Encrypted string of front 2 bit .
char *get_rand_str(int bit)
{
static char s_str[65];
char *str,strall[64] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz./";
int i;
for(i=0;i
struct timeval tv;
struct timezone tz;
gettimeofday(&tv,&tz);
srand(tv.tv_usec);
s_str[i]=strall[rand()%64];
}
s_str[bit]='\0';
str=&s_str[0];
return(str);
}
main(int argc,char **argv)
{
if(argc != 3)
{
if(argc != 2)
{
printf("Print Input \x22%s password salt\x22\n",argv[0]);
exit(0);
}
char *Salt;
argv[2]=get_rand_str(2);
}
printf("%s\n",crypt(argv[1],argv[2]));
}
gcc -o encrypt ./encrypt.c -lcrypt
一个 perl 方法。
encrypt.pl
#!/usr/bin/perl
use strict;
my $pw=$ARGV[0];
print crypt($pw,$pw)."\n";