Core Coding®

PHP Encryption

  PHP Encryption
Very secure php encrypt and decrypt function. Uses gzip to have a little fun with the algorithm as well.
<?php
function encrypt_string($input, $key) {
$key = substr(md5($key),0,24);
$td = mcrypt_module_open ('tripledes', '', 'ecb', '');
$iv = mcrypt_create_iv (mcrypt_enc_get_iv_size ($td), MCRYPT_RAND);
mcrypt_generic_init ($td, $key, $iv);
$encrypted_data = mcrypt_generic ($td, gzdeflate($input, 9));
mcrypt_generic_deinit ($td);
mcrypt_module_close ($td);
return
trim(str_rot13(base64_encode($encrypted_data)));
}

function
decrypt_string($input, $key) {
$input = trim(base64_decode(str_rot13($input)));
$td = mcrypt_module_open ('tripledes', '', 'ecb', '');
$key = substr(md5($key),0,24);
$iv = mcrypt_create_iv (mcrypt_enc_get_iv_size ($td), MCRYPT_RAND);
mcrypt_generic_init ($td, $key, $iv);
$decrypted_data = mdecrypt_generic ($td, $input);
mcrypt_generic_deinit ($td);
mcrypt_module_close ($td);
return
stripslashes(trim(@gzinflate($decrypted_data)));
}
?>

Go back to resources
Home Mail GitHub