Title / Description
Code <?php function getkeyiv($key_size, $iv_size, $key, $iv='') { if ($iv == '') { $iv = 'OpenSSL for Ruby rulez!'; } $iv = substr($iv,0,$iv_size); $gen = ''; do { $gen = $gen.md5($gen.$key.$iv,true); } while (strlen($gen)<($iv_size+$key_size)); $o[0] = substr($gen,0,$key_size); $o[1] = $iv; //this is not the generated IV, //just the original "salt" cut to the required size return $o; } function trimpadding($str) { if (ord($str[strlen($str)-1]) < 9) { return rtrim($str,$str[strlen($str)-1]); } else { return $str; } } function encipher($data_to_encode){ $key = 'secretkey'; $td = mcrypt_module_open(MCRYPT_TRIPLEDES, '', MCRYPT_MODE_CBC, ''); $iv_size = mcrypt_enc_get_iv_size($td); $key_size = mcrypt_enc_get_key_size($td); $genkey = getkeyiv($key_size, $iv_size,$key); if (mcrypt_generic_init($td,$genkey[0],$genkey[1]) != -1) { $encoded_data = mcrypt_generic($td, $data_to_encode); } mcrypt_generic_deinit($td); mcrypt_module_close($td); return base64_encode($encoded_data); } function decipher($encoded) { $key = 'secretkey'; $decrypted=''; $enc = base64_decode($encoded); $td = mcrypt_module_open(MCRYPT_TRIPLEDES, '', MCRYPT_MODE_CBC, ''); $iv_size = mcrypt_enc_get_iv_size($td); $key_size = mcrypt_enc_get_key_size($td); $genkey = getkeyiv($key_size, $iv_size,$key); if (mcrypt_generic_init($td,$genkey[0],$genkey[1]) != -1) { $decrypted = mdecrypt_generic($td, $enc); } mcrypt_generic_deinit($td); mcrypt_module_close($td); return trimpadding($decrypted); } ?>
Author
Highlight as C C++ CSS Clojure Delphi ERb Groovy (beta) HAML HTML JSON Java JavaScript PHP Plain text Python Ruby SQL XML YAML diff code