perl crypt函数需要帮助

时间:2011-09-13 12:05:02

标签: string perl unique crypt

我试图生成一个唯一的字符串/ id给定另一个相对较大的字符串(由目录路径名称组成),考虑使用crypt函数。但是,它没有按预期工作,很可能是由于我无法理解。

这里的代码&输出:

#!/usr/bin/perl 

print "Enter a string:"; 
chomp(my $string = <STDIN>); 

my $encrypted_string = crypt($string,'di'); 

print "\n  the encrypted string is:$encrypted_string";

输出:

$ perl crypt_test
 Enter a string:abcdefghi

 the encrypted string is:dipcn0ADeg0Jc
$
$ perl crypt_test
 Enter a string:abcdefgh

 the encrypted string is:dipcn0ADeg0Jc
$
$
$ perl crypt_test
 Enter a string:abcde

 the encrypted string is:diGyhSp4Yvj4M
$

我无法理解为什么它为前两个字符串返回相同的加密字符串,而第三个字符串则不同。请注意,盐对所有人来说都是一样的。

1 个答案:

答案 0 :(得分:1)

crypt(3)函数仅考虑输入字符串的前八个字符:

  

通过获取密钥的前八个字符中每个字符的最低7位,获得56位密钥。这个56位密钥用于重复加密一个常量字符串(通常是一个字符串con          全部归零)。返回值指向加密密码,一系列13打印 -          能够使用ASCII字符(前两个字符代表盐本身)。

所以你看到的是设计 - 来自perlfunc

crypt PLAINTEXT,SALT
       Creates a digest string exactly like the crypt(3) function in the C library
相关问题