Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

mcrypt functions

Status
Not open for further replies.

axman505

Technical User
Jun 20, 2001
489
US
I am trying to code a function to encyrpt and one to decrypt, however im having problems on the decrypt.

Code:
<?php
function encrypt($stringIn) {
   /* Open the cipher */
   $td = mcrypt_module_open('tripledes', '', 'ofb', '');
   /* Create the IV and determine the keysize length, used MCRYPT_RAND
     * on Windows instead */
   $iv = mcrypt_create_iv(mcrypt_enc_get_iv_size($td), MCRYPT_DEV_RANDOM);
   $ks = mcrypt_enc_get_key_size($td);
   /* Create key */
   $key = substr(md5('monkey'), 0, $ks);
   /* Intialize encryption */
   mcrypt_generic_init($td, $key, $iv);
   /* Encrypt data */
   $encrypted = mcrypt_generic($td, $stringIn);
   /* Terminate encryption handler */
   mcrypt_generic_deinit($td);
   mcrypt_module_close($td);
   return $encrypted;   
}
 function decrypt($stringIn) {
    /* Open the cipher */
   $td = mcrypt_module_open('tripledes', '', 'ofb', '');
   /* Create the IV and determine the keysize length, used MCRYPT_RAND
     * on Windows instead */
   $iv = mcrypt_create_iv(mcrypt_enc_get_iv_size($td), MCRYPT_DEV_RANDOM);
   $ks = mcrypt_enc_get_key_size($td);
      /* Create key */
   $key = substr(md5('monkey'), 0, $ks);
    /* Initialize encryption module for decryption */
   mcrypt_generic_init($td, $key, $iv);
   /* Decrypt encrypted string */
   $decrypted = mdecrypt_generic($td, $stringIn);
   /* Terminate decryption handle and close module */
   mcrypt_generic_deinit($td);
   mcrypt_module_close($td);
   /* Show string */
   return $decrypted;
}
$test = encrypt("I love CHeese");
print "<p>$test</p>";
$test1 = decrypt($test);
print "<p>$test1</p>";
 

?>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top