0

我正在尝试在给定另一个相对较大的字符串(由目录路径名组成)的情况下生成一个唯一的字符串/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
$

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

4

1 回答 1

1

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

通过取密钥的前 8 个字符中每个字符的最低 7 位,得到一个 56 位的密钥。这个 56 位密钥用于重复加密一个常量字符串(通常是由全零组成的字符串)。返回的值指向加密的密码,一系列 13 个可打印的 ASCII 字符(前两个字符代表盐本身)。

因此,您所看到的是设计使然-来自perlfunc

crypt PLAINTEXT,SALT
       Creates a digest string exactly like the crypt(3) function in the C library
于 2011-09-13T12:11:12.607 回答