前言
最近搞一点东西要用到对称加密,于是在网上找到一个不错的对称加密方法,分享给大家。
/**
* 简单对称加密算法之加密
* @param String $string 需要加密的字串
* @param String $skey 加密EKY
* @author Anyon Zou <zoujingli@qq.com>
* @date 2013-08-13 19:30
* @update 2014-10-10 10:10
* @return String
*/ function encode($string = '', $skey = 'cxphp') {
$strArr = str_split(base64_encode($string));
$strCount = count($strArr); foreach (str_split($skey) as $key => $value)
$key < $strCount && $strArr[$key].=$value; return str_replace(array('=', '+', '/'), array('O0O0O', 'o000o', 'oo00o'), join('', $strArr));
} /**
* 简单对称加密算法之解密
* @param String $string 需要解密的字串
* @param String $skey 解密KEY
* @author Anyon Zou <zoujingli@qq.com>
* @date 2013-08-13 19:30
* @update 2014-10-10 10:10
* @return String
*/ function decode($string = '', $skey = 'cxphp') {
$strArr = str_split(str_replace(array('O0O0O', 'o000o', 'oo00o'), array('=', '+', '/'), $string), 2);
$strCount = count($strArr); foreach (str_split($skey) as $key => $value)
$key <= $strCount && isset($strArr[$key]) && $strArr[$key][1] === $value && $strArr[$key] = $strArr[$key][0]; return base64_decode(join('', $strArr));
} echo '<pre>';
$str = '56,15123365247,54,四大古典风格'; echo "string : " . $str . " <br />"; echo "encode : " . ($enstring = encode($str)) . '<br />'; echo "decode : " . decode($enstring); die();
安全URL解码
/**
* 安全URL编码
* @param string $data
* @author Anyon <zoujingli@qq.com>
* @date 2017/06/26
* @return string
*/ function encode($data) { return str_replace(array('+', '/', '='), array('-', '_', ''), base64_encode(serialize($data)));
} /**
* 安全URL解码
* @param string $string
* @author Anyon <zoujingli@qq.com>
* @date 2017/06/26
* @return string
*/ function decode($string) {
$data = str_replace(array('-', '_'), array('+', '/'), $string);
$mod4 = strlen($data) % 4;
($mod4) && $data .= substr('====', $mod4); return unserialize(base64_decode($data));
}
UTF8三十六进制
/**
* UTF8字符串加密
* @param string $string
* @author Anyon <zoujingli@qq.com>
* @date 2017/06/26
* @return string
*/ function encode($string) {
$chars = '';
$len = strlen($string = iconv('utf-8', 'gbk', $string)); for ($i = 0; $i < $len; $i++) {
$chars .= str_pad(base_convert(ord($string[$i]), 10, 36), 2, 0, 0);
} return strtoupper($chars);
} /**
* UTF8字符串解密
* @param string $string
* @author Anyon <zoujingli@qq.com>
* @date 2017/06/26
* @return string
*/ function decode($string) {
$chars = ''; foreach (str_split($string, 2) as $char) {
$chars .= chr(intval(base_convert($char, 36, 10)));
} return iconv('gbk', 'utf-8', $chars);
}
2021年1月14日
看到了一个新的对称加密函数
//字符串加密 function lockString($txt,$key) {
$chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-=+";
$nh = rand(0,64);
$ch = $chars[$nh];
$mdKey = md5($key.$ch);
$mdKey = substr($mdKey,$nh%8, $nh%8+7);
$txt = base64_encode($txt);
$tmp = '';
$i=0;$j=0;$k = 0; for ($i=0; $i<strlen($txt); $i++) {
$k = $k == strlen($mdKey) ? 0 : $k;
$j = ($nh+strpos($chars,$txt[$i])+ord($mdKey[$k++]))%64;
$tmp .= $chars[$j];
} return urlencode($ch.$tmp);
} /**对字符串进行解密。
* @param $txt
* @param string $key
* @return bool|string
*/ function unlockString($txt,$key) {
$txt = urldecode($txt);
$chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-=+";
$ch = $txt[0];
$nh = strpos($chars,$ch);
$mdKey = md5($key.$ch);
$mdKey = substr($mdKey,$nh%8, $nh%8+7);
$txt = substr($txt,1);
$tmp = '';
$i=0;$j=0; $k = 0; for ($i=0; $i<strlen($txt); $i++) {
$k = $k == strlen($mdKey) ? 0 : $k;
$j = strpos($chars,$txt[$i])-$nh - ord($mdKey[$k++]); while ($j<0) $j+=64;
$tmp .= $chars[$j];
} return base64_decode($tmp);
发表评论