<HTML ><HEAD ><TITLE >Mcrypt Encryption Functions</TITLE ><META NAME="GENERATOR" CONTENT="Modular DocBook HTML Stylesheet Version 1.7"><LINK REL="HOME" TITLE="PHP Manual" HREF="index.html"><LINK REL="UP" TITLE="Function Reference" HREF="funcref.html"><LINK REL="PREVIOUS" TITLE="mcal_week_of_year" HREF="function.mcal-week-of-year.html"><LINK REL="NEXT" TITLE="mcrypt_cbc" HREF="function.mcrypt-cbc.html"><META HTTP-EQUIV="Content-type" CONTENT="text/html; charset=ISO-8859-1"></HEAD ><BODY CLASS="reference" 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 Manual</TH ></TR ><TR ><TD WIDTH="10%" ALIGN="left" VALIGN="bottom" ><A HREF="function.mcal-week-of-year.html" ACCESSKEY="P" >Prev</A ></TD ><TD WIDTH="80%" ALIGN="center" VALIGN="bottom" ></TD ><TD WIDTH="10%" ALIGN="right" VALIGN="bottom" ><A HREF="function.mcrypt-cbc.html" ACCESSKEY="N" >Next</A ></TD ></TR ></TABLE ><HR ALIGN="LEFT" WIDTH="100%"></DIV ><DIV CLASS="reference" ><A NAME="ref.mcrypt" ></A ><DIV CLASS="TITLEPAGE" ><H1 CLASS="title" >LIV. Mcrypt Encryption Functions</H1 ><DIV CLASS="PARTINTRO" ><A NAME="AEN48522" ></A ><DIV CLASS="section" ><H1 CLASS="section" ><A NAME="mcrypt.intro" ></A >Introduction</H1 ><P > This is an interface to the mcrypt library, which supports a wide variety of block algorithms such as DES, TripleDES, Blowfish (default), 3-WAY, SAFER-SK64, SAFER-SK128, TWOFISH, TEA, RC2 and GOST in CBC, OFB, CFB and ECB cipher modes. Additionally, it supports RC6 and IDEA which are considered "non-free". </P ></DIV ><DIV CLASS="section" ><H1 CLASS="section" ><A NAME="mcrypt.requirements" ></A >Requirements</H1 ><P > These functions work using <A HREF="http://mcrypt.hellug.gr/" TARGET="_top" >mcrypt</A >. To use it, download libmcrypt-x.x.tar.gz from <A HREF="http://mcrypt.hellug.gr/" TARGET="_top" >here</A > and follow the included installation instructions. Windows users will find all the needed compiled mcrypt binaries <A HREF="http://ftp.proventum.net/pub/php/win32/misc/mcrypt/" TARGET="_top" >here</A >. </P ><P > If you linked against libmcrypt 2.4.x or higher, the following additional block algorithms are supported: CAST, LOKI97, RIJNDAEL, SAFERPLUS, SERPENT and the following stream ciphers: ENIGMA (crypt), PANAMA, RC4 and WAKE. With libmcrypt 2.4.x or higher another cipher mode is also available; nOFB. </P ></DIV ><DIV CLASS="section" ><H1 CLASS="section" ><A NAME="mcrypt.installation" ></A >Installation</H1 ><P > You need to compile PHP with the <TT CLASS="option" >--with-mcrypt[=DIR]</TT > parameter to enable this extension. DIR is the mcrypt install directory. Make sure you compile libmcrypt with the option <TT CLASS="option" >--disable-posix-threads</TT >. </P ></DIV ><DIV CLASS="section" ><H1 CLASS="section" ><A NAME="mcrypt.configuration" ></A >Runtime Configuration</H1 ><P > The behaviour of these functions is affected by settings in <TT CLASS="filename" >php.ini</TT >. </P ><P > <DIV CLASS="table" ><A NAME="AEN48543" ></A ><P ><B >Table 1. Mcrypt configuration options</B ></P ><TABLE BORDER="1" CLASS="CALSTABLE" ><THEAD ><TR ><TH ALIGN="LEFT" VALIGN="MIDDLE" >Name</TH ><TH ALIGN="LEFT" VALIGN="MIDDLE" >Default</TH ><TH ALIGN="LEFT" VALIGN="MIDDLE" >Changeable</TH ></TR ></THEAD ><TBODY ><TR ><TD ALIGN="LEFT" VALIGN="MIDDLE" >mcrypt.algorithms_dir</TD ><TD ALIGN="LEFT" VALIGN="MIDDLE" >NULL</TD ><TD ALIGN="LEFT" VALIGN="MIDDLE" >PHP_INI_ALL</TD ></TR ><TR ><TD ALIGN="LEFT" VALIGN="MIDDLE" >mcrypt.modes_dir</TD ><TD ALIGN="LEFT" VALIGN="MIDDLE" >NULL</TD ><TD ALIGN="LEFT" VALIGN="MIDDLE" >PHP_INI_ALL</TD ></TR ></TBODY ></TABLE ></DIV > For further details and definition of the PHP_INI_* constants see <A HREF="function.ini-set.html" ><B CLASS="function" >ini_set()</B ></A >. </P ></DIV ><DIV CLASS="section" ><H1 CLASS="section" ><A NAME="mcrypt.resources" ></A >Resource Types</H1 ><P >This extension has no resource types defined.</P ></DIV ><DIV CLASS="section" ><H1 CLASS="section" ><A NAME="mcrypt.constants" ></A >Predefined Constants</H1 ><P > The constants below are defined by this extension, and will only be available when the extension has either been compiled into PHP or dynamically loaded at runtime. </P ><P > Mcrypt can operate in four block cipher modes (CBC, OFB, CFB, and ECB). If linked against libmcrypt-2.4.x or higher the functions can also operate in the block cipher mode nOFB and in STREAM mode. Below you find a list with all supported encryption modes together with the constants that are defines for the encryption mode. For a more complete reference and discussion see Applied Cryptography by Schneier (ISBN 0-471-11709-9). <P ></P ><UL ><LI ><P > MCRYPT_MODE_ECB (electronic codebook) is suitable for random data, such as encrypting other keys. Since data there is short and random, the disadvantages of ECB have a favorable negative effect. </P ></LI ><LI ><P > MCRYPT_MODE_CBC (cipher block chaining) is especially suitable for encrypting files where the security is increased over ECB significantly. </P ></LI ><LI ><P > MCRYPT_MODE_CFB (cipher feedback) is the best mode for encrypting byte streams where single bytes must be encrypted. </P ></LI ><LI ><P > MCRYPT_MODE_OFB (output feedback, in 8bit) is comparable to CFB, but can be used in applications where error propagation cannot be tolerated. It's insecure (because it operates in 8bit mode) so it is not recommended to use it. </P ></LI ><LI ><P > MCRYPT_MODE_NOFB (output feedback, in nbit) is comparable to OFB, but more secure because it operates on the block size of the algorithm. </P ></LI ><LI ><P > MCRYPT_MODE_STREAM is an extra mode to include some stream algorithms like WAKE or RC4. </P ></LI ></UL > </P ><P > Some other mode and random device constants: <P ></P ><DIV CLASS="variablelist" ><DL ><DT ><TT CLASS="constant" ><B >MCRYPT_ENCRYPT</B ></TT > (<A HREF="language.types.integer.html" >integer</A >)</DT ><DD ><P > </P ></DD ><DT ><TT CLASS="constant" ><B >MCRYPT_DECRYPT</B ></TT > (<A HREF="language.types.integer.html" >integer</A >)</DT ><DD ><P > </P ></DD ><DT ><TT CLASS="constant" ><B >MCRYPT_DEV_RANDOM</B ></TT > (<A HREF="language.types.integer.html" >integer</A >)</DT ><DD ><P > </P ></DD ><DT ><TT CLASS="constant" ><B >MCRYPT_DEV_URANDOM</B ></TT > (<A HREF="language.types.integer.html" >integer</A >)</DT ><DD ><P > </P ></DD ><DT ><TT CLASS="constant" ><B >MCRYPT_RAND</B ></TT > (<A HREF="language.types.integer.html" >integer</A >)</DT ><DD ><P > </P ></DD ></DL ></DIV > </P ></DIV ><DIV CLASS="section" ><H1 CLASS="section" ><A NAME="mcrypt.ciphers" ></A >Mcrypt ciphers</H1 ><P > Here is a list of ciphers which are currently supported by the mcrypt extension. For a complete list of supported ciphers, see the defines at the end of <TT CLASS="filename" >mcrypt.h</TT >. The general rule with the mcrypt-2.2.x API is that you can access the cipher from PHP with MCRYPT_ciphername. With the libmcrypt-2.4.x and libmcrypt-2.5.x API these constants also work, but it is possible to specify the name of the cipher as a string with a call to <A HREF="function.mcrypt-module-open.html" ><B CLASS="function" >mcrypt_module_open()</B ></A >. <P ></P ><UL ><LI ><P >MCRYPT_3DES</P ></LI ><LI ><P >MCRYPT_ARCFOUR_IV (libmcrypt > 2.4.x only)</P ></LI ><LI ><P >MCRYPT_ARCFOUR (libmcrypt > 2.4.x only)</P ></LI ><LI ><P >MCRYPT_BLOWFISH</P ></LI ><LI ><P >MCRYPT_CAST_128</P ></LI ><LI ><P >MCRYPT_CAST_256</P ></LI ><LI ><P >MCRYPT_CRYPT</P ></LI ><LI ><P >MCRYPT_DES</P ></LI ><LI ><P >MCRYPT_DES_COMPAT (libmcrypt 2.2.x only)</P ></LI ><LI ><P >MCRYPT_ENIGMA (libmcrypt > 2.4.x only, alias for MCRYPT_CRYPT)</P ></LI ><LI ><P >MCRYPT_GOST</P ></LI ><LI ><P >MCRYPT_IDEA (non-free)</P ></LI ><LI ><P >MCRYPT_LOKI97 (libmcrypt > 2.4.x only)</P ></LI ><LI ><P >MCRYPT_MARS (libmcrypt > 2.4.x only, non-free)</P ></LI ><LI ><P >MCRYPT_PANAMA (libmcrypt > 2.4.x only)</P ></LI ><LI ><P >MCRYPT_RIJNDAEL_128 (libmcrypt > 2.4.x only)</P ></LI ><LI ><P >MCRYPT_RIJNDAEL_192 (libmcrypt > 2.4.x only)</P ></LI ><LI ><P >MCRYPT_RIJNDAEL_256 (libmcrypt > 2.4.x only)</P ></LI ><LI ><P >MCRYPT_RC2</P ></LI ><LI ><P >MCRYPT_RC4 (libmcrypt 2.2.x only)</P ></LI ><LI ><P >MCRYPT_RC6 (libmcrypt > 2.4.x only)</P ></LI ><LI ><P >MCRYPT_RC6_128 (libmcrypt 2.2.x only)</P ></LI ><LI ><P >MCRYPT_RC6_192 (libmcrypt 2.2.x only)</P ></LI ><LI ><P >MCRYPT_RC6_256 (libmcrypt 2.2.x only)</P ></LI ><LI ><P >MCRYPT_SAFER64</P ></LI ><LI ><P >MCRYPT_SAFER128</P ></LI ><LI ><P >MCRYPT_SAFERPLUS (libmcrypt > 2.4.x only)</P ></LI ><LI ><P >MCRYPT_SERPENT(libmcrypt > 2.4.x only)</P ></LI ><LI ><P >MCRYPT_SERPENT_128 (libmcrypt 2.2.x only)</P ></LI ><LI ><P >MCRYPT_SERPENT_192 (libmcrypt 2.2.x only)</P ></LI ><LI ><P >MCRYPT_SERPENT_256 (libmcrypt 2.2.x only)</P ></LI ><LI ><P >MCRYPT_SKIPJACK (libmcrypt > 2.4.x only)</P ></LI ><LI ><P >MCRYPT_TEAN (libmcrypt 2.2.x only)</P ></LI ><LI ><P >MCRYPT_THREEWAY</P ></LI ><LI ><P >MCRYPT_TRIPLEDES (libmcrypt > 2.4.x only)</P ></LI ><LI ><P >MCRYPT_TWOFISH (for older mcrypt 2.x versions, or mcrypt > 2.4.x )</P ></LI ><LI ><P >MCRYPT_TWOFISH128 (TWOFISHxxx are available in newer 2.x versions, but not in the 2.4.x versions)</P ></LI ><LI ><P >MCRYPT_TWOFISH192</P ></LI ><LI ><P >MCRYPT_TWOFISH256</P ></LI ><LI ><P >MCRYPT_WAKE (libmcrypt > 2.4.x only)</P ></LI ><LI ><P >MCRYPT_XTEA (libmcrypt > 2.4.x only)</P ></LI ></UL > </P ><P > You must (in CFB and OFB mode) or can (in CBC mode) supply an initialization vector (IV) to the respective cipher function. The IV must be unique and must be the same when decrypting/encrypting. With data which is stored encrypted, you can take the output of a function of the index under which the data is stored (e.g. the MD5 key of the filename). Alternatively, you can transmit the IV together with the encrypted data (see chapter 9.3 of Applied Cryptography by Schneier (ISBN 0-471-11709-9) for a discussion of this topic). </P ></DIV ><DIV CLASS="section" ><H1 CLASS="section" ><A NAME="mcrypt.examples" ></A >Examples</H1 ><P > Mcrypt can be used to encrypt and decrypt using the above mentioned ciphers. If you linked against libmcrypt-2.2.x, the four important mcrypt commands (<A HREF="function.mcrypt-cfb.html" ><B CLASS="function" >mcrypt_cfb()</B ></A >, <A HREF="function.mcrypt-cbc.html" ><B CLASS="function" >mcrypt_cbc()</B ></A >, <A HREF="function.mcrypt-ecb.html" ><B CLASS="function" >mcrypt_ecb()</B ></A >, and <A HREF="function.mcrypt-ofb.html" ><B CLASS="function" >mcrypt_ofb()</B ></A >) can operate in both modes which are named MCRYPT_ENCRYPT and MCRYPT_DECRYPT, respectively. <TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN48709" ></A ><P ><B >Example 1. Encrypt an input value with TripleDES under 2.2.x in ECB mode</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><?php $key = "this is a secret key"; $input = "Let us meet at 9 o'clock at the secret place."; $encrypted_data = mcrypt_ecb (MCRYPT_3DES, $key, $input, MCRYPT_ENCRYPT); ?></PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > This example will give you the encrypted data as a string in <TT CLASS="literal" >$encrypted_data</TT >. </P ><P > If you linked against libmcrypt 2.4.x or 2.5.x, these functions are still available, but it is recommended that you use the advanced functions. <TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN48714" ></A ><P ><B >Example 2. Encrypt an input value with TripleDES under 2.4.x and higher in ECB mode</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><?php $key = "this is a secret key"; $input = "Let us meet at 9 o'clock at the secret place."; $td = mcrypt_module_open ('tripledes', '', 'ecb', ''); $iv = mcrypt_create_iv (mcrypt_enc_get_iv_size ($td), MCRYPT_RAND); mcrypt_generic_init ($td, $key, $iv); $encrypted_data = mcrypt_generic ($td, $input); mcrypt_generic_deinit ($td); mcrypt_module_close ($td); ?></PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > This example will give you the encrypted data as a string in <TT CLASS="literal" >$encrypted_data</TT >. For a full example see <A HREF="function.mcrypt-module-open.html" ><B CLASS="function" >mcrypt_module_open()</B ></A >. </P ></DIV ></DIV ><DIV CLASS="TOC" ><DL ><DT ><B >Table of Contents</B ></DT ><DT ><A HREF="function.mcrypt-cbc.html" >mcrypt_cbc</A > -- Encrypt/decrypt data in CBC mode</DT ><DT ><A HREF="function.mcrypt-cfb.html" >mcrypt_cfb</A > -- Encrypt/decrypt data in CFB mode</DT ><DT ><A HREF="function.mcrypt-create-iv.html" >mcrypt_create_iv</A > -- Create an initialization vector (IV) from a random source </DT ><DT ><A HREF="function.mcrypt-decrypt.html" >mcrypt_decrypt</A > -- Decrypts crypttext with given parameters</DT ><DT ><A HREF="function.mcrypt-ecb.html" >mcrypt_ecb</A > -- Encrypt/decrypt data in ECB mode</DT ><DT ><A HREF="function.mcrypt-enc-get-algorithms-name.html" >mcrypt_enc_get_algorithms_name</A > -- Returns the name of the opened algorithm</DT ><DT ><A HREF="function.mcrypt-enc-get-block-size.html" >mcrypt_enc_get_block_size</A > -- Returns the blocksize of the opened algorithm</DT ><DT ><A HREF="function.mcrypt-enc-get-iv-size.html" >mcrypt_enc_get_iv_size</A > -- Returns the size of the IV of the opened algorithm</DT ><DT ><A HREF="function.mcrypt-enc-get-key-size.html" >mcrypt_enc_get_key_size</A > -- Returns the maximum supported keysize of the opened mode</DT ><DT ><A HREF="function.mcrypt-enc-get-modes-name.html" >mcrypt_enc_get_modes_name</A > -- Returns the name of the opened mode</DT ><DT ><A HREF="function.mcrypt-enc-get-supported-key-sizes.html" >mcrypt_enc_get_supported_key_sizes</A > -- Returns an array with the supported keysizes of the opened algorithm</DT ><DT ><A HREF="function.mcrypt-enc-is-block-algorithm-mode.html" >mcrypt_enc_is_block_algorithm_mode</A > -- Checks whether the encryption of the opened mode works on blocks</DT ><DT ><A HREF="function.mcrypt-enc-is-block-algorithm.html" >mcrypt_enc_is_block_algorithm</A > -- Checks whether the algorithm of the opened mode is a block algorithm</DT ><DT ><A HREF="function.mcrypt-enc-is-block-mode.html" >mcrypt_enc_is_block_mode</A > -- Checks whether the opened mode outputs blocks</DT ><DT ><A HREF="function.mcrypt-enc-self-test.html" >mcrypt_enc_self_test</A > -- This function runs a self test on the opened module</DT ><DT ><A HREF="function.mcrypt-encrypt.html" >mcrypt_encrypt</A > -- Encrypts plaintext with given parameters</DT ><DT ><A HREF="function.mcrypt-generic-deinit.html" >mcrypt_generic_deinit</A > -- This function deinitializes an encryption module </DT ><DT ><A HREF="function.mcrypt-generic-end.html" >mcrypt_generic_end</A > -- This function terminates encryption</DT ><DT ><A HREF="function.mcrypt-generic-init.html" >mcrypt_generic_init</A > -- This function initializes all buffers needed for encryption</DT ><DT ><A HREF="function.mcrypt-generic.html" >mcrypt_generic</A > -- This function encrypts data</DT ><DT ><A HREF="function.mcrypt-get-block-size.html" >mcrypt_get_block_size</A > -- Get the block size of the specified cipher</DT ><DT ><A HREF="function.mcrypt-get-cipher-name.html" >mcrypt_get_cipher_name</A > -- Get the name of the specified cipher</DT ><DT ><A HREF="function.mcrypt-get-iv-size.html" >mcrypt_get_iv_size</A > -- Returns the size of the IV belonging to a specific cipher/mode combination</DT ><DT ><A HREF="function.mcrypt-get-key-size.html" >mcrypt_get_key_size</A > -- Get the key size of the specified cipher</DT ><DT ><A HREF="function.mcrypt-list-algorithms.html" >mcrypt_list_algorithms</A > -- Get an array of all supported ciphers</DT ><DT ><A HREF="function.mcrypt-list-modes.html" >mcrypt_list_modes</A > -- Get an array of all supported modes</DT ><DT ><A HREF="function.mcrypt-module-close.html" >mcrypt_module_close</A > -- Close the mcrypt module </DT ><DT ><A HREF="function.mcrypt-module-get-algo-block-size.html" >mcrypt_module_get_algo_block_size</A > -- Returns the blocksize of the specified algorithm</DT ><DT ><A HREF="function.mcrypt-module-get-algo-key-size.html" >mcrypt_module_get_algo_key_size</A > -- Returns the maximum supported keysize of the opened mode</DT ><DT ><A HREF="function.mcrypt-module-get-supported-key-sizes.html" >mcrypt_module_get_supported_key_sizes</A > -- Returns an array with the supported keysizes of the opened algorithm</DT ><DT ><A HREF="function.mcrypt-module-is-block-algorithm-mode.html" >mcrypt_module_is_block_algorithm_mode</A > -- This function returns if the the specified module is a block algorithm or not</DT ><DT ><A HREF="function.mcrypt-module-is-block-algorithm.html" >mcrypt_module_is_block_algorithm</A > -- This function checks whether the specified algorithm is a block algorithm</DT ><DT ><A HREF="function.mcrypt-module-is-block-mode.html" >mcrypt_module_is_block_mode</A > -- This function returns if the the specified mode outputs blocks or not</DT ><DT ><A HREF="function.mcrypt-module-open.html" >mcrypt_module_open</A > -- This function opens the module of the algorithm and the mode to be used</DT ><DT ><A HREF="function.mcrypt-module-self-test.html" >mcrypt_module_self_test</A > -- This function runs a self test on the specified module</DT ><DT ><A HREF="function.mcrypt-ofb.html" >mcrypt_ofb</A > -- Encrypt/decrypt data in OFB mode</DT ><DT ><A HREF="function.mdecrypt-generic.html" >mdecrypt_generic</A > -- This function decrypts data</DT ></DL ></DIV ></DIV ></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.mcal-week-of-year.html" ACCESSKEY="P" >Prev</A ></TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="index.html" ACCESSKEY="H" >Home</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" ><A HREF="function.mcrypt-cbc.html" ACCESSKEY="N" >Next</A ></TD ></TR ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" >mcal_week_of_year</TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="funcref.html" ACCESSKEY="U" >Up</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" >mcrypt_cbc</TD ></TR ></TABLE ></DIV ></BODY ></HTML >