擅长:python、mysql、java
<p>Python的crypt()是系统crypt()函数的包装器。从Linux crypt()手册页:</p>
<pre>
char *crypt(const char *key, const char *salt);
key is a user’s typed password.
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.
</pre>
<p>重点是“<em>两个字符</em>字符串”。现在,如果看一下crypt()在Python中的行为:</p>
<pre><code>>>> crypt.crypt("Hello", "World")
'Wo5pEi/H5/mxU'
>>> crypt.crypt("Hello", "ABCDE")
'AB/uOsC7P93EI'
</code></pre>
<p>您会发现,结果的前两个字符总是与原始盐的前两个字符重合,这确实形成了真正的两个字符盐本身。
也就是说,crypt()的结果的形式是2char salt+encrypted pass。
因此,如果不传递两个字符的salt或原始的多个字符的salt,而是传递整个加密密码,则结果没有区别。</p>
<p>注:集合[a–zA–Z0–9./]包含64个字符,64*64=4096。以下是两个<em>字符与“<em>4096</em>不同方式”的关系。</p>