PHP头条
热点:

phpcms v9 sys_auth函数加密原理


基本原理
 
 
假设变量$a,$b,$c=$a^$b(变量a异或变量b),
 
所以我们有$a=$b^$c   ,  $b=$a^$c
 
以上是异或逻辑的应用,(题外话:如何在不使用第3个变量的情况下,交换变量$a,$b的值呢?)
 
 
 
 
回归正题:
 
了解了下的内容后,我们可以把变量$a看成是明文(需加密的字符串),变量$b看成是密钥(自己定义),变量$c看成是密文(发送到客户端在网上传输的)。
 
 
 
 
当然上面只是基本原理,加密绝不是就简单的异或字符串就行。
 
 
 
 
为了使算法更不易破解sys_auth函数对密钥进行了一个加密:
 
 
 
$key_length = 4;  
$key = md5($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));  

$key_length = 4;
$key = md5($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));

 

 
对明文也插入一些片段,并用base64进行了编码。
 
 
 
function sys_auth($string, $operation = 'ENCODE', $key = 'hi', $expiry = 0) {  
    $key_length = 4;  
    $key = md5($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));  
    //10位密文过期信息+16位明文和密钥生成的密文验证信息+明文   
      
    $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 . str_replace('=', '', 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 substr($result, 26);  
        } else {  
            return '';  
        }  
    }  
}  

function sys_auth($string, $operation = 'ENCODE', $key = 'hi', $expiry = 0) {
$key_length = 4;
$key = md5($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));
//10位密文过期信息+16位明文和密钥生成的密文验证信息+明文

$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 . str_replace('=', '', 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 substr($result, 26);
} else {
return '';
}
}
}

 

 
 

www.phpzy.comtrue/phprm/4324.htmlTechArticlephpcms v9 sys_auth函数加密原理 基本原理 假设变量$a,$b,$c=$a^$b(变量a异或变量b), 所以我们有$a=$b^$c , $b=$a^$c 以上是异或逻辑的应用,(题外话:如何在不使用第3个变量的情况下,交换变量$...

相关文章

    暂无相关文章
相关频道:

PHP之友评论

今天推荐