<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <HTML ><HEAD ><TITLE >crypt</TITLE ><META NAME="GENERATOR" CONTENT="Modular DocBook HTML Stylesheet Version 1.79"><LINK REL="HOME" TITLE="PHP 手册" HREF="index.html"><LINK REL="UP" TITLE="String 字符串处理函数" HREF="ref.strings.html"><LINK REL="PREVIOUS" TITLE="crc32" HREF="function.crc32.html"><LINK REL="NEXT" TITLE="echo" HREF="function.echo.html"><META HTTP-EQUIV="Content-type" CONTENT="text/html; charset=UTF-8"></HEAD ><BODY CLASS="refentry" BGCOLOR="#FFFFFF" TEXT="#000000" LINK="#0000FF" VLINK="#840084" ALINK="#0000FF" ><DIV CLASS="NAVHEADER" ><TABLE SUMMARY="Header navigation table" WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" ><TR ><TH COLSPAN="3" ALIGN="center" >PHP 手册</TH ></TR ><TR ><TD WIDTH="10%" ALIGN="left" VALIGN="bottom" ><A HREF="function.crc32.html" ACCESSKEY="P" >上一页</A ></TD ><TD WIDTH="80%" ALIGN="center" VALIGN="bottom" ></TD ><TD WIDTH="10%" ALIGN="right" VALIGN="bottom" ><A HREF="function.echo.html" ACCESSKEY="N" >下一页</A ></TD ></TR ></TABLE ><HR ALIGN="LEFT" WIDTH="100%"></DIV ><H1 ><A NAME="function.crypt" ></A >crypt</H1 ><DIV CLASS="refnamediv" ><A NAME="AEN224826" ></A ><P > (PHP 3, PHP 4, PHP 5)</P >crypt -- One-way string encryption (hashing)</DIV ><DIV CLASS="refsect1" ><A NAME="AEN224829" ></A ><H2 >Description</H2 >string <B CLASS="methodname" >crypt</B > ( string str [, string salt] )<BR ></BR ><P > <B CLASS="function" >crypt()</B > will return an encrypted string using the standard Unix <ABBR CLASS="abbrev" >DES</ABBR >-based encryption algorithm or alternative algorithms that may be available on the system. Arguments are a string to be encrypted and an optional salt string to base the encryption on. See the Unix man page for your crypt function for more information. </P ><P > If the salt argument is not provided, one will be randomly generated by PHP each time you call this function. </P ><P > Some operating systems support more than one type of encryption. In fact, sometimes the standard DES-based encryption is replaced by an MD5-based encryption algorithm. The encryption type is triggered by the salt argument. At install time, PHP determines the capabilities of the crypt function and will accept salts for other encryption types. If no salt is provided, PHP will auto-generate a standard two character salt by default, unless the default encryption type on the system is MD5, in which case a random MD5-compatible salt is generated. PHP sets a constant named CRYPT_SALT_LENGTH which tells you whether a regular two character salt applies to your system or the longer twelve character salt is applicable. </P ><P > If you are using the supplied salt, you should be aware that the salt is generated once. If you are calling this function repeatedly, this may impact both appearance and security. </P ><P > The standard DES-based encryption <B CLASS="function" >crypt()</B > returns the salt as the first two characters of the output. It also only uses the first eight characters of <CODE CLASS="parameter" >str</CODE >, so longer strings that start with the same eight characters will generate the same result (when the same salt is used). </P ><P > On systems where the crypt() function supports multiple encryption types, the following constants are set to 0 or 1 depending on whether the given type is available: </P ><P ></P ><UL ><LI ><P > CRYPT_STD_DES - Standard DES-based encryption with a two character salt </P ></LI ><LI ><P > CRYPT_EXT_DES - Extended DES-based encryption with a nine character salt </P ></LI ><LI ><P > CRYPT_MD5 - MD5 encryption with a twelve character salt starting with $1$ </P ></LI ><LI ><P > CRYPT_BLOWFISH - Blowfish encryption with a sixteen character salt starting with $2$ or $2a$ </P ></LI ></UL ><DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >注意: </B > There is no decrypt function, since <B CLASS="function" >crypt()</B > uses a one-way algorithm. </P ></BLOCKQUOTE ></DIV ><TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN224862" ></A ><P ><B >例 1. <B CLASS="function" >crypt()</B > examples</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><code><font color="#000000"> <font color="#0000BB"><?php<br />$password </font><font color="#007700">= </font><font color="#0000BB">crypt</font><font color="#007700">(</font><font color="#DD0000">'mypassword'</font><font color="#007700">); </font><font color="#FF8000">// let the salt be automatically generated<br /><br />/* You should pass the entire results of crypt() as the salt for comparing a<br /> password, to avoid problems when different hashing algorithms are used. (As<br /> it says above, standard DES-based password hashing uses a 2-character salt,<br /> but MD5-based hashing uses 12.) */<br /></font><font color="#007700">if (</font><font color="#0000BB">crypt</font><font color="#007700">(</font><font color="#0000BB">$user_input</font><font color="#007700">, </font><font color="#0000BB">$password</font><font color="#007700">) == </font><font color="#0000BB">$password</font><font color="#007700">) {<br /> echo </font><font color="#DD0000">"Password verified!"</font><font color="#007700">;<br />}<br /></font><font color="#0000BB">?></font> </font> </code></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE ><TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN224866" ></A ><P ><B >例 2. Using <B CLASS="function" >crypt()</B > with htpasswd</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><code><font color="#000000"> <font color="#0000BB"><?php<br /></font><font color="#FF8000">// Set the password<br /></font><font color="#0000BB">$password </font><font color="#007700">= </font><font color="#DD0000">'mypassword'</font><font color="#007700">;<br /><br /></font><font color="#FF8000">// Get the hash, letting the salt be automatically generated<br /></font><font color="#0000BB">$hash </font><font color="#007700">= </font><font color="#0000BB">crypt</font><font color="#007700">(</font><font color="#0000BB">$password</font><font color="#007700">);<br /></font><font color="#0000BB">?></font> </font> </code></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE ><TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN224870" ></A ><P ><B >例 3. Using <B CLASS="function" >crypt()</B > with different encryption types</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><code><font color="#000000"> <font color="#0000BB"><?php<br /></font><font color="#007700">if (</font><font color="#0000BB">CRYPT_STD_DES </font><font color="#007700">== </font><font color="#0000BB">1</font><font color="#007700">) {<br /> echo </font><font color="#DD0000">'Standard DES: ' </font><font color="#007700">. </font><font color="#0000BB">crypt</font><font color="#007700">(</font><font color="#DD0000">'rasmuslerdorf'</font><font color="#007700">, </font><font color="#DD0000">'rl'</font><font color="#007700">) . </font><font color="#DD0000">"\n"</font><font color="#007700">;<br />}<br /><br />if (</font><font color="#0000BB">CRYPT_EXT_DES </font><font color="#007700">== </font><font color="#0000BB">1</font><font color="#007700">) {<br /> echo </font><font color="#DD0000">'Extended DES: ' </font><font color="#007700">. </font><font color="#0000BB">crypt</font><font color="#007700">(</font><font color="#DD0000">'rasmuslerdorf'</font><font color="#007700">, </font><font color="#DD0000">'_J9..rasm'</font><font color="#007700">) . </font><font color="#DD0000">"\n"</font><font color="#007700">;<br />}<br /><br />if (</font><font color="#0000BB">CRYPT_MD5 </font><font color="#007700">== </font><font color="#0000BB">1</font><font color="#007700">) {<br /> echo </font><font color="#DD0000">'MD5: ' </font><font color="#007700">. </font><font color="#0000BB">crypt</font><font color="#007700">(</font><font color="#DD0000">'rasmuslerdorf'</font><font color="#007700">, </font><font color="#DD0000">'$1$rasmusle$'</font><font color="#007700">) . </font><font color="#DD0000">"\n"</font><font color="#007700">;<br />}<br /><br />if (</font><font color="#0000BB">CRYPT_BLOWFISH </font><font color="#007700">== </font><font color="#0000BB">1</font><font color="#007700">) {<br /> echo </font><font color="#DD0000">'Blowfish: ' </font><font color="#007700">. </font><font color="#0000BB">crypt</font><font color="#007700">(</font><font color="#DD0000">'rasmuslerdorf'</font><font color="#007700">, </font><font color="#DD0000">'$2a$07$rasmuslerd...........$'</font><font color="#007700">) . </font><font color="#DD0000">"\n"</font><font color="#007700">;<br />}<br /></font><font color="#0000BB">?></font> </font> </code></TD ></TR ></TABLE ><P >上例的输出类似于:</P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="screen" >Standard DES: rl.3StKT.4T8M Extended DES: _J9..rasmBYk8r9AiWNc MD5: $1$rasmusle$rISCgZzpwk3UhDidwXvin0 Blowfish: $2a$07$rasmuslerd............nIdrcHdxcUxWomQX9j6kvERCFjTg7Ra</PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE ><P > See also <A HREF="function.md5.html" ><B CLASS="function" >md5()</B ></A > and <A HREF="ref.mcrypt.html" >the Mcrypt extension</A >. </P ></DIV ><DIV CLASS="NAVFOOTER" ><HR ALIGN="LEFT" WIDTH="100%"><TABLE SUMMARY="Footer navigation table" WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" ><A HREF="function.crc32.html" ACCESSKEY="P" >上一页</A ></TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="index.html" ACCESSKEY="H" >起始页</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" ><A HREF="function.echo.html" ACCESSKEY="N" >下一页</A ></TD ></TR ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" >crc32</TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="ref.strings.html" ACCESSKEY="U" >上一级</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" >echo</TD ></TR ></TABLE ></DIV ></BODY ></HTML >