Sophie

Sophie

distrib > Mandriva > 2008.1 > x86_64 > by-pkgid > 05cd670d8a02b2b4a0ffb1756f2e8308 > files > 2518

php-manual-zh-5.2.4-1mdv2008.1.noarch.rpm

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<HTML
><HEAD
><TITLE
>mcrypt_module_open</TITLE
><META
NAME="GENERATOR"
CONTENT="Modular DocBook HTML Stylesheet Version 1.79"><LINK
REL="HOME"
TITLE="PHP 手册"
HREF="index.html"><LINK
REL="UP"
TITLE="Mcrypt Encryption Functions"
HREF="ref.mcrypt.html"><LINK
REL="PREVIOUS"
TITLE="mcrypt_module_is_block_mode"
HREF="function.mcrypt-module-is-block-mode.html"><LINK
REL="NEXT"
TITLE="mcrypt_module_self_test"
HREF="function.mcrypt-module-self-test.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.mcrypt-module-is-block-mode.html"
ACCESSKEY="P"
>上一页</A
></TD
><TD
WIDTH="80%"
ALIGN="center"
VALIGN="bottom"
></TD
><TD
WIDTH="10%"
ALIGN="right"
VALIGN="bottom"
><A
HREF="function.mcrypt-module-self-test.html"
ACCESSKEY="N"
>下一页</A
></TD
></TR
></TABLE
><HR
ALIGN="LEFT"
WIDTH="100%"></DIV
><H1
><A
NAME="function.mcrypt-module-open"
></A
>mcrypt_module_open</H1
><DIV
CLASS="refnamediv"
><A
NAME="AEN116109"
></A
><P
>    (PHP 4 &#62;= 4.0.2, PHP 5)</P
>mcrypt_module_open&nbsp;--&nbsp;Opens the module of the algorithm and the mode to be used</DIV
><DIV
CLASS="refsect1"
><A
NAME="AEN116112"
></A
><H2
>Description</H2
>resource <B
CLASS="methodname"
>mcrypt_module_open</B
> ( string algorithm, string algorithm_directory, string mode, string mode_directory )<BR
></BR
><P
>&#13;     This function opens the module of the algorithm and the mode to be used.
     The name of the algorithm is specified in algorithm, e.g. "twofish" or is
     one of the MCRYPT_ciphername constants.  The module is closed by calling
     <A
HREF="function.mcrypt-module-close.html"
><B
CLASS="function"
>mcrypt_module_close()</B
></A
>. Normally it returns an
     encryption descriptor, or <TT
CLASS="constant"
><B
>FALSE</B
></TT
> on error.
    </P
><P
>&#13;     The <CODE
CLASS="parameter"
>algorithm_directory</CODE
> and
     <CODE
CLASS="parameter"
>mode_directory</CODE
> are used to locate the encryption
     modules. When you supply a directory name, it is used.  When you set one
     of these to the empty string (""), the value set by the
     <CODE
CLASS="parameter"
>mcrypt.algorithms_dir</CODE
> or
     <CODE
CLASS="parameter"
>mcrypt.modes_dir</CODE
> ini-directive is used.  When
     these are not set, the default directories that are used are the ones
     that were compiled in into libmcrypt (usually /usr/local/lib/libmcrypt).
    </P
><P
>&#13;     <TABLE
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
CLASS="EXAMPLE"
><TR
><TD
><DIV
CLASS="example"
><A
NAME="AEN116138"
></A
><P
><B
>例 1. <B
CLASS="function"
>mcrypt_module_open()</B
> examples</B
></P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
CELLPADDING="5"
><TR
><TD
><code><font color="#000000">
<font color="#0000BB">&lt;?php<br />&nbsp;&nbsp;&nbsp;&nbsp;$td </font><font color="#007700">= </font><font color="#0000BB">mcrypt_module_open</font><font color="#007700">(</font><font color="#0000BB">MCRYPT_DES</font><font color="#007700">, </font><font color="#DD0000">''</font><font color="#007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#0000BB">MCRYPT_MODE_ECB</font><font color="#007700">, </font><font color="#DD0000">'/usr/lib/mcrypt-modes'</font><font color="#007700">);<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#0000BB">$td </font><font color="#007700">= </font><font color="#0000BB">mcrypt_module_open</font><font color="#007700">(</font><font color="#DD0000">'rijndael-256'</font><font color="#007700">, </font><font color="#DD0000">''</font><font color="#007700">, </font><font color="#DD0000">'ofb'</font><font color="#007700">, </font><font color="#DD0000">''</font><font color="#007700">);<br /></font><font color="#0000BB">?&gt;</font>
</font>
</code></TD
></TR
></TABLE
></DIV
></TD
></TR
></TABLE
>
    </P
><P
>&#13;     The first line in the example above will try to open the DES cipher from
     the default directory and the EBC mode from the directory
     <TT
CLASS="filename"
>/usr/lib/mcrypt-modes</TT
>. The second example uses
     strings as name for the cipher and mode, this only works when the
     extension is linked against libmcrypt 2.4.x or 2.5.x.
    </P
><P
>&#13;     <TABLE
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
CLASS="EXAMPLE"
><TR
><TD
><DIV
CLASS="example"
><A
NAME="AEN116145"
></A
><P
><B
>例 2. Using <B
CLASS="function"
>mcrypt_module_open()</B
> in encryption</B
></P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
CELLPADDING="5"
><TR
><TD
><code><font color="#000000">
<font color="#0000BB">&lt;?php<br />&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#FF8000">/* Open the cipher */<br />&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#0000BB">$td </font><font color="#007700">= </font><font color="#0000BB">mcrypt_module_open</font><font color="#007700">(</font><font color="#DD0000">'rijndael-256'</font><font color="#007700">, </font><font color="#DD0000">''</font><font color="#007700">, </font><font color="#DD0000">'ofb'</font><font color="#007700">, </font><font color="#DD0000">''</font><font color="#007700">);<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#FF8000">/* Create the IV and determine the keysize length, used MCRYPT_RAND<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;* on Windows instead */<br />&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#0000BB">$iv </font><font color="#007700">= </font><font color="#0000BB">mcrypt_create_iv</font><font color="#007700">(</font><font color="#0000BB">mcrypt_enc_get_iv_size</font><font color="#007700">(</font><font color="#0000BB">$td</font><font color="#007700">), </font><font color="#0000BB">MCRYPT_DEV_RANDOM</font><font color="#007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#0000BB">$ks </font><font color="#007700">= </font><font color="#0000BB">mcrypt_enc_get_key_size</font><font color="#007700">(</font><font color="#0000BB">$td</font><font color="#007700">);<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#FF8000">/* Create key */<br />&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#0000BB">$key </font><font color="#007700">= </font><font color="#0000BB">substr</font><font color="#007700">(</font><font color="#0000BB">md5</font><font color="#007700">(</font><font color="#DD0000">'very secret key'</font><font color="#007700">), </font><font color="#0000BB">0</font><font color="#007700">, </font><font color="#0000BB">$ks</font><font color="#007700">);<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#FF8000">/* Intialize encryption */<br />&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#0000BB">mcrypt_generic_init</font><font color="#007700">(</font><font color="#0000BB">$td</font><font color="#007700">, </font><font color="#0000BB">$key</font><font color="#007700">, </font><font color="#0000BB">$iv</font><font color="#007700">);<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#FF8000">/* Encrypt data */<br />&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#0000BB">$encrypted </font><font color="#007700">= </font><font color="#0000BB">mcrypt_generic</font><font color="#007700">(</font><font color="#0000BB">$td</font><font color="#007700">, </font><font color="#DD0000">'This is very important data'</font><font color="#007700">);<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#FF8000">/* Terminate encryption handler */<br />&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#0000BB">mcrypt_generic_deinit</font><font color="#007700">(</font><font color="#0000BB">$td</font><font color="#007700">);<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#FF8000">/* Initialize encryption module for decryption */<br />&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#0000BB">mcrypt_generic_init</font><font color="#007700">(</font><font color="#0000BB">$td</font><font color="#007700">, </font><font color="#0000BB">$key</font><font color="#007700">, </font><font color="#0000BB">$iv</font><font color="#007700">);<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#FF8000">/* Decrypt encrypted string */<br />&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#0000BB">$decrypted </font><font color="#007700">= </font><font color="#0000BB">mdecrypt_generic</font><font color="#007700">(</font><font color="#0000BB">$td</font><font color="#007700">, </font><font color="#0000BB">$encrypted</font><font color="#007700">);<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#FF8000">/* Terminate decryption handle and close module */<br />&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#0000BB">mcrypt_generic_deinit</font><font color="#007700">(</font><font color="#0000BB">$td</font><font color="#007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#0000BB">mcrypt_module_close</font><font color="#007700">(</font><font color="#0000BB">$td</font><font color="#007700">);<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#FF8000">/* Show string */<br />&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#007700">echo </font><font color="#0000BB">trim</font><font color="#007700">(</font><font color="#0000BB">$decrypted</font><font color="#007700">) . </font><font color="#DD0000">"\n"</font><font color="#007700">;<br /></font><font color="#0000BB">?&gt;</font>
</font>
</code></TD
></TR
></TABLE
></DIV
></TD
></TR
></TABLE
>
    </P
><P
>&#13;     The first line in the example above will try to open the DES cipher from
     the default directory and the EBC mode from the directory
     <TT
CLASS="filename"
>/usr/lib/mcrypt-modes</TT
>. The second example uses
     strings as name for the cipher and mode, this only works when the
     extension is linked against libmcrypt 2.4.x or 2.5.x.
    </P
><P
>&#13;     See also 
     <A
HREF="function.mcrypt-module-close.html"
><B
CLASS="function"
>mcrypt_module_close()</B
></A
>,
     <A
HREF="function.mcrypt-generic.html"
><B
CLASS="function"
>mcrypt_generic()</B
></A
>,
     <A
HREF="function.mdecrypt-generic.html"
><B
CLASS="function"
>mdecrypt_generic()</B
></A
>,
     <A
HREF="function.mcrypt-generic-init.html"
><B
CLASS="function"
>mcrypt_generic_init()</B
></A
>, and
     <A
HREF="function.mcrypt-generic-deinit.html"
><B
CLASS="function"
>mcrypt_generic_deinit()</B
></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.mcrypt-module-is-block-mode.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.mcrypt-module-self-test.html"
ACCESSKEY="N"
>下一页</A
></TD
></TR
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
>mcrypt_module_is_block_mode</TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
><A
HREF="ref.mcrypt.html"
ACCESSKEY="U"
>上一级</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
>mcrypt_module_self_test</TD
></TR
></TABLE
></DIV
></BODY
></HTML
>