<HTML ><HEAD ><TITLE >mdecrypt_generic</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="Mcrypt Encryption Functions" HREF="ref.mcrypt.html"><LINK REL="PREVIOUS" TITLE="mcrypt_ofb" HREF="function.mcrypt-ofb.html"><LINK REL="NEXT" TITLE="MCVE Payment Functions" HREF="ref.mcve.html"><META HTTP-EQUIV="Content-type" CONTENT="text/html; charset=ISO-8859-1"></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 Manual</TH ></TR ><TR ><TD WIDTH="10%" ALIGN="left" VALIGN="bottom" ><A HREF="function.mcrypt-ofb.html" ACCESSKEY="P" >Prev</A ></TD ><TD WIDTH="80%" ALIGN="center" VALIGN="bottom" ></TD ><TD WIDTH="10%" ALIGN="right" VALIGN="bottom" ><A HREF="ref.mcve.html" ACCESSKEY="N" >Next</A ></TD ></TR ></TABLE ><HR ALIGN="LEFT" WIDTH="100%"></DIV ><H1 ><A NAME="function.mdecrypt-generic" ></A >mdecrypt_generic</H1 ><DIV CLASS="refnamediv" ><A NAME="AEN49655" ></A ><P > (PHP 4 >= 4.0.2)</P >mdecrypt_generic -- This function decrypts data</DIV ><DIV CLASS="refsect1" ><A NAME="AEN49658" ></A ><H2 >Description</H2 >string <B CLASS="methodname" >mdecrypt_generic</B > ( resource td, string data)<BR ></BR ><P > This function decrypts data. Note that the length of the returned string can in fact be longer then the unencrypted string, due to the padding of the data. </P ><P > <TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN49671" ></A ><P ><B >Example 1. <B CLASS="function" >mdecrypt_generic()</B > Example</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><?php /* Data */ $key = 'this is a very long key, even too long for the cipher'; $plain_text = 'very important data'; /* Open module, and create IV */ $td = mcrypt_module_open ('des', '', 'ecb', ''); $key = substr ($key, 0, mcrypt_enc_get_key_size ($td)); $iv_size = mcrypt_enc_get_iv_size ($td); $iv = mcrypt_create_iv ($iv_size, MCRYPT_RAND); /* Initialize encryption handle */ if (mcrypt_generic_init ($td, $key, $iv) != -1) { /* Encrypt data */ $c_t = mcrypt_generic ($td, $plain_text); mcrypt_generic_deinit ($td); /* Reinitialize buffers for decryption */ mcrypt_generic_init ($td, $key, $iv); $p_t = mdecrypt_generic ($td, $c_t); /* Clean up */ mcrypt_generic_deinit ($td); mcrypt_module_close ($td); } if (strncmp ($p_t, $plain_text, strlen($plain_text)) == 0) { echo "ok\n"; } else { echo "error\n"; } ?></PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > </P ><P > The above example shows how to check if the data before the encryption is the same as the data after the decryption. It is very important to reinitialize the encryption buffer with <A HREF="function.mcrypt-generic-init.html" ><B CLASS="function" >mcrypt_generic_init()</B ></A > before you try to decrypt the data. </P ><P > The decryption handle should alwayws be initialized with <A HREF="function.mcrypt-generic-init.html" ><B CLASS="function" >mcrypt_generic_init()</B ></A > with a key and an IV before calling this function. Where the encryption is done, you should free the encryption buffers by calling <A HREF="function.mcrypt-generic-deinit.html" ><B CLASS="function" >mcrypt_generic_deinit()</B ></A >. See <A HREF="function.mcrypt-module-open.html" ><B CLASS="function" >mcrypt_module_open()</B ></A > for an example. </P ><P > See also <A HREF="function.mcrypt-generic.html" ><B CLASS="function" >mcrypt_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-ofb.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="ref.mcve.html" ACCESSKEY="N" >Next</A ></TD ></TR ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" >mcrypt_ofb</TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="ref.mcrypt.html" ACCESSKEY="U" >Up</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" >MCVE Payment Functions</TD ></TR ></TABLE ></DIV ></BODY ></HTML >