PHP中加密解密函数与DES加密解密的应用实例:
<?php define('SMS_KEY', 'K0e5293b'); class DesUtil{ public function encrypt($string, $key){ $iv = mcrypt_create_iv(mcrypt_get_iv_size(MCRYPT_DES, MCRYPT_MODE_ECB), MCRYPT_RAND); $decrypted = mcrypt_encrypt(MCRYPT_DES, $key, $string, MCRYPT_MODE_ECB, $iv); $encode = base64_encode($decrypted); return $encode; } public function decrypt($string, $key){ $decoded = base64_decode($string); $iv = mcrypt_create_iv(mcrypt_get_iv_size(MCRYPT_DES, MCRYPT_MODE_ECB), MCRYPT_RAND); $decrypted = mcrypt_decrypt(MCRYPT_DES, $key, $decoded, MCRYPT_MODE_ECB, $iv); return $this->trimEnd($decrypted); } private function trimEnd($text){ $len = strlen($text); $c = $text[$len - 1]; if(ord($c) == 0) return rtrim($text, $c); if(ord($c) < $len){ for($i = $len - ord($c); $i < $len; $i++){ if($text[$i] != $c){ return $text; } } return substr($text, 0, $len - ord($c)); } return $text; } } // 加密需通过get方式在url中传递的参数 // 加密解密手机号 $des = new DesUtil(); $res = $des->encrypt('18955556666', SMS_KEY); echo $res; echo '<br/>'; echo $des->decrypt($res, SMS_KEY); /* 【输出如下:】 anfCRSWbnVbYabIPBXmizw== 18955556666 */ ?>
转载请注明来源地址:小川编程 » https://www.youhutong.com/index.php/article/index/188.html