几种PHP对称加密解密源码

最近搞一点东西要用到对称加密,于是在网上找到一个不错的对称加密方法,分享给大家。

/**
 * 简单对称加密算法之加密
 * @param String $string 需要加密的字串
 * @param String $skey 加密EKY
 * @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>';
echo "encode : " . ($enstring = encode($str)) . '<br />';
echo "decode : " . decode($enstring);
die();

安全URL解码

/**
 * 安全URL编码
 * @param string $data
 * @date 2017/06/26
 * @return string
 */
function encode($data) {
    return str_replace(array('+', '/', '='), array('-', '_', ''), base64_encode(serialize($data)));
}

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
 * @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);
}

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);
}

别的方式

//字符串加密
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);
}

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);
}

auth_code优化版

/**
 * @param string $string  加密字符串内容
 * @param string $operation 加密或者解密
 * @param string $key 私钥
 * @param int $expiry 过期时间/秒 0为永不过期
 * @return false|string
 */
function auth_code(string $string, $operation = 'DECODE', $key = '', $expiry = 0)
{
    $cakey_length = 4;
    $key = md5($key);
    $keys = md5(substr($key, 0, 16));
    $keytab = md5(substr($key, 16, 16));
    if ($operation === 'DECODE') {
        $key_str = substr($string, 0, $cakey_length);
    } else {
        $key_str = substr(md5(microtime()), -$cakey_length);
    }
    $keycode = $cakey_length ? $key_str : '';
    $cryptic = $keys . md5($keys . $keycode);
    $key_length = strlen($cryptic);
    $string = $operation === 'DECODE' ? base64_decode(substr($string, $cakey_length)) : sprintf('%010d', $expiry ? $expiry + time() : 0) . substr(md5($string . $keytab), 0, 16) . $string;
    $string_length = strlen($string);
    $result = '';
    $box = range(0, 255);
    $render = array();
    for ($i = 0; $i <= 255; $i++) {
        $render[$i] = ord($cryptic[$i % $key_length]);
    }
    for ($j = $i = 0; $i < 256; $i++) {
        $j = ($j + $box[$i] + $render[$i]) % 256;
        $tmp = $box[$i];
        $box[$i] = $box[$j];
        $box[$j] = $tmp;
    }
    for ($a = $j = $i = 0; $i < $string_length; $i++) {
        $a = ($a + 1) % 256;
        $j = ($j + $box[$a]) % 256;
        $tmp = $box[$a];
        $box[$a] = $box[$j];
        $box[$j] = $tmp;
        $result .= chr(ord($string[$i]) ^ ($box[($box[$a] + $box[$j]) % 256]));
    }
    if ($operation === 'DECODE') {
        if ((strpos($result, 0) === 0 || substr($result, 0, 10) - time() > 0) && strpos(md5(substr($result, 26) . $keytab), substr($result, 10, 16)) === 0) {
            return substr($result, 26);
        }
        return '';
    }
    return $keycode . str_replace('=', '', base64_encode($result));
}

给TA买糖
共{{data.count}}人
人已赞赏
源码分享

ICO Crypto V1.9.3比特币和加密货币ICO登陆页面HTML模板+用户仪表板

2021-7-19 18:49:24

源码分享

PHP如何检测字符串里有没有繁体字

2021-9-9 1:29:00

0 条回复 A文章作者 M管理员
    暂无讨论,说说你的看法吧
个人中心
有新私信 私信列表
搜索