几种PHP对称加密解密源码

发布时间:2024年01月17日

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

/**
 * 简单对称加密算法之加密
 * @param String $string 需要加密的字串
 * @param String $skey 加密EKY
 * @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
 * @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
 * @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
 * @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));
}

文章来源:https://blog.csdn.net/wushengyong1996/article/details/135657966
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。