php加密、解密函数

发布于 2023-05-09 17:34:34

function sys_auth($string, $operation = 'ENCODE', $key = 'amh_key', $expiry = 0)

{
    if($operation == 'ENCODE')
        $string = urlencode( $string );
    $key_length = 4;
    $key = md5($key != '' ? $key : 'amh_key');
    $fixedkey = md5($key);
    $egiskeys = md5(substr($fixedkey, 16, 16));
    $runtokey = $key_length ? ($operation == 'ENCODE' ? substr(md5(microtime(true)), -$key_length) : substr($string, 0, $key_length)) : '';
    $keys = md5(substr($runtokey, 0, 16) . substr($fixedkey, 0, 16) . substr($runtokey, 16) . substr($fixedkey, 16));
    $string = $operation == 'ENCODE' ? sprintf('%010d', $expiry ? $expiry + time() : 0).substr(md5($string.$egiskeys), 0, 16) . $string : base64_decode(substr($string, $key_length));
    $i = 0; $result = '';
    $string_length = strlen($string);
    for ($i = 0; $i < $string_length; $i++){
        $result .= chr(ord($string{$i}) ^ ord($keys{$i % 32}));
    }
    if($operation == 'ENCODE') {
        return  $runtokey . base64_encode($result) ;
    } else {
        if((substr($result, 0, 10) == 0 || substr($result, 0, 10) - time() > 0) && substr($result, 10, 16) == substr(md5(substr($result, 26).$egiskeys), 0, 16)) {
            return urldecode( substr($result, 26));
        } else {
            return '';
        }
    }
}

加密:$keydata = urlencode(functions::sys_auth($keydata, 'ENCODE', $_SESSION'amh_config'['config_value'], _APIt));
解密:$keydata = json_decode(Functions::sys_auth($keydata, 'DECODE', $_SESSION'amh_config'['config_value']));

Bug天天改,头发日日疏,码字不易,如果有帮助到你,就点击"下方感谢"支持一下把.

©声明:本站所有文章,如无特殊说明或标注,均为izhnagbo.cn原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。本文链接 https://www.izhangbo.cn/article/59.html
0 条评论

学习
记录

发布
问题