Decrypts data (original) (raw)
mdecrypt_generic
(PHP 4 >= 4.0.2, PHP 5, PHP 7 < 7.2.0, PECL mcrypt >= 1.0.0)
mdecrypt_generic — Decrypts data
Warning
This function has been_DEPRECATED_ as of PHP 7.1.0 and_REMOVED_ as of PHP 7.2.0. Relying on this function is highly discouraged.
Description
mdecrypt_generic ( resource $td
, string $data
) : string
Examples
Example #1 mdecrypt_generic() Example
`<?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=mcryptmoduleopen(′des′,′′,′ecb′,′′);<spanclass="katex"><spanclass="katex−mathml"><mathxmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>k</mi><mi>e</mi><mi>y</mi><mo>=</mo><mi>s</mi><mi>u</mi><mi>b</mi><mi>s</mi><mi>t</mi><mi>r</mi><mostretchy="false">(</mo></mrow><annotationencoding="application/x−tex">key=substr(</annotation></semantics></math></span><spanclass="katex−html"aria−hidden="true"><spanclass="base"><spanclass="strut"style="height:0.8889em;vertical−align:−0.1944em;"></span><spanclass="mordmathnormal"style="margin−right:0.03148em;">k</span><spanclass="mordmathnormal"style="margin−right:0.03588em;">ey</span><spanclass="mspace"style="margin−right:0.2778em;"></span><spanclass="mrel">=</span><spanclass="mspace"style="margin−right:0.2778em;"></span></span><spanclass="base"><spanclass="strut"style="height:1em;vertical−align:−0.25em;"></span><spanclass="mordmathnormal">s</span><spanclass="mordmathnormal">u</span><spanclass="mordmathnormal">b</span><spanclass="mordmathnormal">s</span><spanclass="mordmathnormal">t</span><spanclass="mordmathnormal"style="margin−right:0.02778em;">r</span><spanclass="mopen">(</span></span></span></span>key,0,mcryptencgetkeysize(td = mcrypt_module_open('des', '', 'ecb', ''); <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>k</mi><mi>e</mi><mi>y</mi><mo>=</mo><mi>s</mi><mi>u</mi><mi>b</mi><mi>s</mi><mi>t</mi><mi>r</mi><mo stretchy="false">(</mo></mrow><annotation encoding="application/x-tex">key = substr(</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.8889em;vertical-align:-0.1944em;"></span><span class="mord mathnormal" style="margin-right:0.03148em;">k</span><span class="mord mathnormal" style="margin-right:0.03588em;">ey</span><span class="mspace" style="margin-right:0.2778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2778em;"></span></span><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mord mathnormal">s</span><span class="mord mathnormal">u</span><span class="mord mathnormal">b</span><span class="mord mathnormal">s</span><span class="mord mathnormal">t</span><span class="mord mathnormal" style="margin-right:0.02778em;">r</span><span class="mopen">(</span></span></span></span>key, 0, mcrypt_enc_get_key_size(td=mcryptmoduleopen(′des′,′′,′ecb′,′′);<spanclass="katex"><spanclass="katex−mathml"><mathxmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>k</mi><mi>e</mi><mi>y</mi><mo>=</mo><mi>s</mi><mi>u</mi><mi>b</mi><mi>s</mi><mi>t</mi><mi>r</mi><mostretchy="false">(</mo></mrow><annotationencoding="application/x−tex">key=substr(</annotation></semantics></math></span><spanclass="katex−html"aria−hidden="true"><spanclass="base"><spanclass="strut"style="height:0.8889em;vertical−align:−0.1944em;"></span><spanclass="mordmathnormal"style="margin−right:0.03148em;">k</span><spanclass="mordmathnormal"style="margin−right:0.03588em;">ey</span><spanclass="mspace"style="margin−right:0.2778em;"></span><spanclass="mrel">=</span><spanclass="mspace"style="margin−right:0.2778em;"></span></span><spanclass="base"><spanclass="strut"style="height:1em;vertical−align:−0.25em;"></span><spanclass="mordmathnormal">s</span><spanclass="mordmathnormal">u</span><spanclass="mordmathnormal">b</span><spanclass="mordmathnormal">s</span><spanclass="mordmathnormal">t</span><spanclass="mordmathnormal"style="margin−right:0.02778em;">r</span><spanclass="mopen">(</span></span></span></span>key,0,mcryptencgetkeysize(td)); ivsize=mcryptencgetivsize(iv_size = mcrypt_enc_get_iv_size(ivsize=mcryptencgetivsize(td); iv=mcryptcreateiv(iv = mcrypt_create_iv(iv=mcryptcreateiv(iv_size, MCRYPT_RAND);/ Initialize encryption handle /
if (mcrypt_generic_init($td, key,key, key,iv) != -1) {/ Encrypt data / ct=mcryptgeneric(c_t = mcrypt_generic(ct=mcryptgeneric(td, $plain_text);
mcrypt_generic_deinit($td);/ Reinitialize buffers for decryption /
mcrypt_generic_init($td, key,key, key,iv); pt=mdecryptgeneric(p_t = mdecrypt_generic(pt=mdecryptgeneric(td, $c_t);/ Clean up */
mcrypt_generic_deinit($td);
mcrypt_module_close($td);
}
if (
strncmp($p_t, plaintext,strlen(plain_text, strlen(plaintext,strlen(plain_text)) == 0) {
echo "ok\n";
} else {
echo "error\n";
}
?> `
The example above 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 withmcrypt_generic_init() before you try to decrypt the data.
The decryption handle should always be initialized withmcrypt_generic_init() with a key and an IV before calling this function. Where the encryption is done, you should free the encryption buffers by calling mcrypt_generic_deinit(). See mcrypt_module_open() for an example.
See Also
- mcrypt_generic() - This function encrypts data
- mcrypt_generic_init() - This function initializes all buffers needed for encryption
- mcrypt_generic_deinit() - This function deinitializes an encryption module