2 way encryption / decryption in PHP w/ mcrypt


/ Published in: PHP
Save to your folder(s)

Here are some functions I found on the net that will allow you to easily do encryption / decryption as long as you have the mcrypt extension loaded with PHP.


Copy this code and paste it in your HTML
  1. <?
  2. // Encrypt Function
  3. function mc_encrypt($encrypt, $mc_key) {
  4. $iv = mcrypt_create_iv(mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB), MCRYPT_RAND);
  5. $passcrypt = trim(mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $mc_key, trim($encrypt), MCRYPT_MODE_ECB, $iv));
  6. $encode = base64_encode($passcrypt);
  7. return $encode;
  8. }
  9.  
  10. // Decrypt Function
  11. function mc_decrypt($decrypt, $mc_key) {
  12. $decoded = base64_decode($decrypt);
  13. $iv = mcrypt_create_iv(mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB), MCRYPT_RAND);
  14. $decrypted = trim(mcrypt_decrypt(MCRYPT_RIJNDAEL_256, $mc_key, trim($decoded), MCRYPT_MODE_ECB, $iv));
  15. return $decrypted;
  16. }
  17. ?>

URL: http://www.phpsnaps.com/snaps/view/rijndael-256-bit-encryption-using-mcrypt/

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.