Home >php教程 >PHP源码 >自定义对称 加密和解密

自定义对称 加密和解密

PHP中文网
PHP中文网Original
2016-05-23 16:38:541165browse

代码

function selfEncode($str, $k) {
    $encoded = '';
    $len = strlen($str);
    $lk = strlen($k);
    for($i = 0; $i < $len; $i++) 
    {
        $mod = fmod($i, $lk);
        $encoded .= $str[$i. &#39;&#39;] ^ $k[$mod.&#39;&#39;];
    }
    $encoded = base64_encode($encoded);
    
    return $encoded;
}
function selfDecode($str, $k) {
    $str = base64_decode($str);
    $decoded = &#39;&#39;;
    $len = strlen($str);
    $lk = strlen($k);
    for($i = 0; $i < $len; $i++) 
    {
        $mod = fmod($i, $lk);
        $decoded .= $str[$i. &#39;&#39;] ^ $k[$mod.&#39;&#39;];
    }
    
    return $decoded;
}

// 注意: $str 和 $k 都是字符串类型
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn