一个简单的php加密解密函数(动态加密)

yipeiwu_com5年前PHP代码库
复制代码 代码如下:

function encode_pass($tex,$key,$type="encode"){
    $chrArr=array('a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z',
                  'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z',
                  '0','1','2','3','4','5','6','7','8','9');
    if($type=="decode"){
        if(strlen($tex)<14)return false;
        $verity_str=substr($tex, 0,8);
        $tex=substr($tex, 8);
        if($verity_str!=substr(md5($tex),0,8)){
            //完整性验证失败
            return false;
        }   
    }
    $key_b=$type=="decode"?substr($tex,0,6):$chrArr[rand()%62].$chrArr[rand()%62].$chrArr[rand()%62].$chrArr[rand()%62].$chrArr[rand()%62].$chrArr[rand()%62];
    $rand_key=$key_b.$key;
    $rand_key=md5($rand_key);
    $tex=$type=="decode"?base64_decode(substr($tex, 6)):$tex;
    $texlen=strlen($tex);
    $reslutstr="";
    for($i=0;$i<$texlen;$i++){
        $reslutstr.=$tex{$i}^$rand_key{$i%32};
    }
    if($type!="decode"){
        $reslutstr=trim($key_b.base64_encode($reslutstr),"==");
        $reslutstr=substr(md5($reslutstr), 0,8).$reslutstr;
    }
    return $reslutstr;
}
$psa=encode_pass("phpcode","taintainxousad");
echo $psa;
echo encode_pass($psa,"taintainxousad",'decode');

相关文章

CentOS安装php v8js教程

CentOS release 5.11 (Final),CentOS release 6.6 (Final) x64测试通过。 gcc版本,glibc版本,libstdc++.so.6版...

php自定义函数br2nl实现将html中br换行符转换为文本输入中换行符的方法【与函数nl2br功能相反】

本文实例讲述了php自定义函数br2nl实现将html中br换行符转换为文本输入中换行符的方法。分享给大家供大家参考,具体如下: 下面这几个方法将能够帮你解决这个问题。 PHP版将htm...

PHP匿名函数和use子句用法实例

本文实例讲述了PHP匿名函数和use子句用法。分享给大家供大家参考,具体如下: 下面方法输出的是hello world $param1和$param2是闭包变量 function t...

PHP生成zip压缩包的常用方法示例

本文实例讲述了PHP生成zip压缩包的常用方法。分享给大家供大家参考,具体如下: 压缩一个文件 我们将一个文件生成一个压缩包。 <?php $path = "c:/wa...

详解WordPress中的头像缓存和代理中的缓存更新方法

详解WordPress中的头像缓存和代理中的缓存更新方法

wordpress评论中的头像是使用Gravatar的头像服务(Gravatar官方注册地址:http://en.gravatar.com),用户的缓存头像一般都是固定不变的,所以我们可...