自己实现的php加密解密函数结果纯字母和数字

php自带的base64加密解密函数大部分人都知道,加密后很容易被解密,所以就自己实现的php加密解密函数,密文是字母和数字组合。

<?php
/*
*加密
*/
function encode($tex, $key = null) {
    $key = $key ? $key : "test";
    $md5str=preg_replace('|[0-9/]+|','',md5($key));
    $key = substr($md5str, 0, 2);
    $texlen = strlen($tex);
    $rand_key=md5($key);
    $reslutstr = "";
    for ($i = 0; $i < $texlen; $i++) {
        $reslutstr.=$tex{$i} ^ $rand_key{$i % 32};
    }
    $reslutstr = trim(base64_encode($reslutstr), "==");
    $reslutstr = $key.substr(md5($reslutstr), 0, 3) . $reslutstr;
    return $reslutstr;
}
/*
*解密
*/
function decode($tex) { 
$key = substr($tex, 0, 2); 
$tex = substr($tex, 2); 
$verity_str = substr($tex, 0, 3); 
$tex = substr($tex, 3); 
if ($verity_str != substr(md5($tex), 0, 3)) { 
  //完整性验证失败 
  return false; 
} 
$tex = base64_decode($tex); 
$texlen = strlen($tex); 
$reslutstr = ""; 
$rand_key=md5($key); 
for($i = 0; $i < $texlen; $i++) {
 $reslutstr.=$tex{$i} ^ $rand_key{$i % 32}; 
} 
return $reslutstr; 
}
echo encode("test");
echo decode(encode("test"));
?>

猜你喜欢

转载自www.cnblogs.com/yfq1/p/phpjiamijiemi.html