Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>We use mcrypt in our projects to achieve encryption. Below is a code sample based on content found on the internet:</p> <pre><code>&lt;?php class MyProjCrypt { private $td; private $iv; private $ks; private $salt; private $encStr; private $decStr; /** * The constructor initializes the cryptography library * @param $salt string The encryption key * @return void */ function __construct($salt) { $this-&gt;td = mcrypt_module_open('rijndael-256', '', 'ofb', ''); // algorithm $this-&gt;ks = mcrypt_enc_get_key_size($this-&gt;td); // key size needed for the algorithm $this-&gt;salt = substr(md5($salt), 0, $this-&gt;ks); } /** * Generates a hex string of $src * @param $src string String to be encrypted * @return void */ function encrypt($src) { srand(( double) microtime() * 1000000); //for sake of MCRYPT_RAND $this-&gt;iv = mcrypt_create_iv($this-&gt;ks, MCRYPT_RAND); mcrypt_generic_init($this-&gt;td, $this-&gt;salt, $this-&gt;iv); $tmpStr = mcrypt_generic($this-&gt;td, $src); mcrypt_generic_deinit($this-&gt;td); mcrypt_module_close($this-&gt;td); //convert the encrypted binary string to hex //$this-&gt;iv is needed to decrypt the string later. It has a fixed length and can easily //be seperated out from the encrypted String $this-&gt;encStr = bin2hex($this-&gt;iv.$tmpStr); } /** * Decrypts a hex string * @param $src string String to be decrypted * @return void */ function decrypt($src) { //convert the hex string to binary $corrected = preg_replace("[^0-9a-fA-F]", "", $src); $binenc = pack("H".strlen($corrected), $corrected); //retrieve the iv from the encrypted string $this-&gt;iv = substr($binenc, 0, $this-&gt;ks); //retrieve the encrypted string alone(minus iv) $binstr = substr($binenc, $this-&gt;ks); /* Initialize encryption module for decryption */ mcrypt_generic_init($this-&gt;td, $this-&gt;salt, $this-&gt;iv); /* Decrypt encrypted string */ $decrypted = mdecrypt_generic($this-&gt;td, $binstr); /* Terminate decryption handle and close module */ mcrypt_generic_deinit($this-&gt;td); mcrypt_module_close($this-&gt;td); $this-&gt;decStr = trim($decrypted); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload