<?php /** * 3des加密 */ class Encrypt{ public function pkcs5_pad($text, $blocksize) { $pad = $blocksize - (strlen($text) % $blocksize); return $text . str_repeat(chr($pad), $pad); } public function pkcs5_unpad($text) { $pad = ord($text{strlen($text) - 1}); if ($pad > strlen($text)){ return false; } return substr($text, 0, -1 * $pad); } /** * 加密 * @param $key * @param $input * @return string */ public function des3Encryption($key,$input){ $input = $this->pkcs5_pad($input,8); $td = mcrypt_module_open('tripledes', '', 'ecb', ''); $iv = mcrypt_create_iv (mcrypt_enc_get_iv_size($td), MCRYPT_RAND); mcrypt_generic_init($td, $key, $iv); $en_res = mcrypt_generic($td, $input); mcrypt_generic_deinit($td); mcrypt_module_close($td); return $en_res; } /** * 解密 * @param $key * @param $crypt * @return string */ public function des3Decrypt($key, $crypt){ $crypt = base64_decode($crypt); $td = mcrypt_module_open('tripledes', '', 'ecb', ''); $iv = mcrypt_create_iv (mcrypt_enc_get_iv_size($td), MCRYPT_RAND); mcrypt_generic_init($td, $key, $iv); $decrypted_data = mdecrypt_generic($td, $crypt); mcrypt_generic_deinit($td); mcrypt_module_close($td); $decrypted_data = $this->pkcs5_unpad($decrypted_data); return rtrim($decrypted_data); } } $obj = new Encrypt(); $key = "9oyKs7cVo1yYzkuisP9bhA=="; $input = '123456'; $enc_res = base64_encode($obj->des3Encryption($key,$input)); echo $enc_res; $crypt = 'KkljLXJH9jnUg4+UyiPjzs*AxRTi0Iybiub47dTySoEqIK2NKg/LgKzhXWRc3Eken0YW09xcApE6kJEUfoEIsrAWnsnSCeZjr62quFEMv51BlE7RmUALXIta+P3aVRl5D6fg+xpeIgsJAY9kkhmLmdQmbvB3hMqwF/Gh7X6ZYGr7dHo4DfjNFiHaSUnDQ6s8ik1I+ye85rr2uWIZ/yPuvrRuTw05+gFe1z01v/kWzVLfKXmsxZOrPIpNSPsnv+08mckR2nsNT8ljOr9ggcDQovf3FgybIw3w2OraFKhnrOebzsGV0Wt5gwesKGbg8s6wam1d8qGLxLyUno74iLNRa/Kaz6LjoKWmMmpol1pEDQnM/UNCKc3Sdp34yenHj1Fr8prPouOgVBMTlBqWLtrDTl1R6apVi+ZUOnw/cKexsmJiCsHEAHK7oy6vj/44R1QGg6H8mF5tPIfZtvp8e2i+l0eC4yveATbjJ7LcZ4+qAEbveQC8x/eix3PBjmhYeKANzeJqNEflTuvIHQIQWuKeT9Vw0nFrR3Dt+MBQrsMV/lWs1H3w4NrgQdQEtR/7L2wxNR2tMHM64EHUBLUf+y6+VuMP83CbwQurJQ6l0LGSKAunFwvRbJM5CYrr8qIitU/JTokRlYdPl0yDuNqjqyQe9nS8WymTachpKe1ZmP6jIOu6rxeC0F6qI0ZH/KagceLZRBiz/R7Bf3sT15jsfqBfSiXwSROhvsa2PsLRoFz8i4eMTiLfbu0xP+Mrs65coYec+gMJK7hzpQnc5I/UJjSmjL9slAVpJc74hRGJ3dwlV/zmnmZeyeQzX3qsF3rMnopsHrYLniK+Xu/Cwl7G55Su1pGekbno2'; $crypt = base64_decode($crypt); $desc_res = $obj->des3Decrypt($key,$crypt); echo '<pre>'; echo $desc_res; ?>