/** * 简单对称加密算法之加密 * @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();
测试结果如下:
