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
1、本站发布的内容仅限用于学习和研究目的.请勿用于商业或非法用途,下载后请24小时内删除。
2、本站所有内容均不能保证其完整性,不能接受请勿购买或下载,如需完整程序,请去其官方购买正版使用
3、本站联系方式Email:admin@youhutong.com ,收到邮件会第一时间处理。
4、如侵犯到任何版权问题,请立即告知本站(立即在线告知),本站将及时删除并致以最深的歉意
