<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <HTML ><HEAD ><TITLE >mcrypt_create_iv</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_cfb" HREF="function.mcrypt-cfb.html"><LINK REL="NEXT" TITLE="mcrypt_decrypt" HREF="function.mcrypt-decrypt.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-cfb.html" ACCESSKEY="P" >上一页</A ></TD ><TD WIDTH="80%" ALIGN="center" VALIGN="bottom" ></TD ><TD WIDTH="10%" ALIGN="right" VALIGN="bottom" ><A HREF="function.mcrypt-decrypt.html" ACCESSKEY="N" >下一页</A ></TD ></TR ></TABLE ><HR ALIGN="LEFT" WIDTH="100%"></DIV ><H1 ><A NAME="function.mcrypt-create-iv" ></A >mcrypt_create_iv</H1 ><DIV CLASS="refnamediv" ><A NAME="AEN115349" ></A ><P > (PHP 3 >= 3.0.8, PHP 4, PHP 5)</P >mcrypt_create_iv -- Create an initialization vector (IV) from a random source </DIV ><DIV CLASS="refsect1" ><A NAME="AEN115352" ></A ><H2 >Description</H2 >string <B CLASS="methodname" >mcrypt_create_iv</B > ( int size [, int source] )<BR ></BR ><P > <B CLASS="function" >mcrypt_create_iv()</B > is used to create an IV. </P ><P > Parameter <CODE CLASS="parameter" >size</CODE > determines the size of the IV, parameter <CODE CLASS="parameter" >source</CODE > (defaults to random value) specifies the source of the IV. </P ><P > The source can be <TT CLASS="constant" ><B >MCRYPT_RAND</B ></TT > (system random number generator), <TT CLASS="constant" ><B >MCRYPT_DEV_RANDOM</B ></TT > (read data from <TT CLASS="filename" >/dev/random</TT >) and <TT CLASS="constant" ><B >MCRYPT_DEV_URANDOM</B ></TT > (read data from <TT CLASS="filename" >/dev/urandom</TT >). <TT CLASS="constant" ><B >MCRYPT_RAND</B ></TT > is the only one supported on Windows because Windows (of course) doesn't have <TT CLASS="filename" >/dev/random</TT > or <TT CLASS="filename" >/dev/urandom</TT >. </P ><DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >注意: </B > When using <TT CLASS="constant" ><B >MCRYPT_RAND</B ></TT >, remember to call <A HREF="function.srand.html" ><B CLASS="function" >srand()</B ></A > before <B CLASS="function" >mcrypt_create_iv()</B > to initialize the random number generator; it is not seeded automatically like <A HREF="function.rand.html" ><B CLASS="function" >rand()</B ></A > is. </P ></BLOCKQUOTE ></DIV ><P > <TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN115384" ></A ><P ><B >例 1. <B CLASS="function" >mcrypt_create_iv()</B > example</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><code><font color="#000000"> <font color="#0000BB"><?php<br /> $size </font><font color="#007700">= </font><font color="#0000BB">mcrypt_get_iv_size</font><font color="#007700">(</font><font color="#0000BB">MCRYPT_CAST_256</font><font color="#007700">, </font><font color="#0000BB">MCRYPT_MODE_CFB</font><font color="#007700">);<br /> </font><font color="#0000BB">$iv </font><font color="#007700">= </font><font color="#0000BB">mcrypt_create_iv</font><font color="#007700">(</font><font color="#0000BB">$size</font><font color="#007700">, </font><font color="#0000BB">MCRYPT_DEV_RANDOM</font><font color="#007700">);<br /></font><font color="#0000BB">?></font> </font> </code></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > </P ><P > The IV is only meant to give an alternative seed to the encryption routines. This IV does not need to be secret at all, though it can be desirable. You even can send it along with your ciphertext without losing security. </P ><P > More information can be found at <A HREF="http://www.ciphersbyritter.com/GLOSSARY.HTM#IV" TARGET="_top" >http://www.ciphersbyritter.com/GLOSSARY.HTM#IV</A >, <A HREF="http://fn2.freenet.edmonton.ab.ca/~jsavard/crypto/co0409.htm" TARGET="_top" >http://fn2.freenet.edmonton.ab.ca/~jsavard/crypto/co0409.htm</A > and in chapter 9.3 of Applied Cryptography by Schneier (ISBN 0-471-11709-9) for a discussion of this topic. </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-cfb.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-decrypt.html" ACCESSKEY="N" >下一页</A ></TD ></TR ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" >mcrypt_cfb</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_decrypt</TD ></TR ></TABLE ></DIV ></BODY ></HTML >