PHP加密解密类

<?php
class Mypass {

    static function encrypt($data, $key){
        $key = md5($key);
        $x  = 0;
        $len = strlen($data);
        $l  = strlen($key);
        for ($i = 0; $i < $len; $i++){
            if ($x == $l){
                $x = 0;
            }
            $char .= $key{$x};
            $x++;
        }
        for ($i = 0; $i < $len; $i++){
            $str .= chr(ord($data{$i}) + (ord($char{$i})) % 256);
        }
        return base64_encode($str);
    }


    static function decrypt($data, $key){
     $key = md5($key);
        $x = 0;
        $data = base64_decode($data);
        $len = strlen($data);
        $l = strlen($key);
        for ($i = 0; $i < $len; $i++)
        {
            if ($x == $l) 
            {
             $x = 0;
            }
            $char .= substr($key, $x, 1);
            $x++;
        }
        for ($i = 0; $i < $len; $i++)
        {
            if (ord(substr($data, $i, 1)) < ord(substr($char, $i, 1)))
            {
                $str .= chr((ord(substr($data, $i, 1)) + 256) - ord(substr($char, $i, 1)));
            }
            else
            {
                $str .= chr(ord(substr($data, $i, 1)) - ord(substr($char, $i, 1)));
            }
        }
        return $str;
    }

}

/*
$data = 'PHP加密解密算法';  // 被加密信息
$key = '123';     // 密钥
$encrypt = encrypt($data, $key);
$decrypt = decrypt($encrypt, $key);
echo $encrypt, "n", $decrypt;

*/
?>
/**
  * 加密函数
  *
  * @return string
*/
function wuEncrypt($sTxt,$sKey='PiihcIictI'){
    $iKey = '_vr0Bo.psIFJ2flX3sgfk6g6Z2sG9gkRm';
    $sChars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_.';
    $nh1 = rand(0,64);
    $nh2 = rand(0,64);
    $nh3 = rand(0,64);
    $ch1 = $sChars{$nh1};
    $ch2 = $sChars{$nh2};
    $ch3 = $sChars{$nh3};
    $nhnum = $nh1 + $nh2 + $nh3;
    $knum = 0;$i = 0;
    while(isset($sKey{$i}))
    {
        $knum += ord($sKey{$i++});
        $mdKey = substr(md5(md5(md5($sKey.$ch1).$ch2.$iKey).$ch3),$nhnum%8,$knum%8 + 16);
    }
    $sTxt = base64_encode($sTxt);
    $sTxt = str_replace(array('+','/','='),array('-','_','.'),$sTxt);
    $tmp = '';
    $j=0;$k = 0;
    $tlen = strlen($sTxt);
    $klen = strlen($mdKey);
    for ($i=0; $i<$tlen; $i++)
    {
        $k = $k == $klen ? 0 : $k;
        $j = ($nhnum+strpos($sChars,$sTxt{$i})+ord($mdKey{$k++}))%64;
        $tmp .= $sChars{$j};
    }
    $tmplen = strlen($tmp);
    $tmp = substr_replace($tmp,$ch3,$nh2 % ++$tmplen,0);
    $tmp = substr_replace($tmp,$ch2,$nh1 % ++$tmplen,0);
    $tmp = substr_replace($tmp,$ch1,$knum % ++$tmplen,0);
    return $tmp;
}

/**
* 解密函数
*
* @param string $sString  需要解密的字符串
* @return string
*/
function wuDecrypt($sString,$sKey='PiihcIictI'){
    $iKey = '_vr0Bo.psIFJ2flX3sgfk6g6Z2sG9gkRm';
    $sChars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_.';
    $knum = 0;
    $i = 0;
    $tlen = strlen($sString);
    while(isset($sKey{$i}))
    {
        $knum +=ord($sKey{$i++});
    }
    $ch1 = $sString{$knum % $tlen};
    $nh1 = strpos($sChars,$ch1);
    $sString = substr_replace($sString,'',$knum % $tlen--,1);
    $ch2 = $sString{$nh1 % $tlen};
    $nh2 = strpos($sChars,$ch2);
    $sString = substr_replace($sString,'',$nh1 % $tlen--,1);
    $ch3 = $sString{$nh2 % $tlen};
    $nh3 = strpos($sChars,$ch3);
    $sString = substr_replace($sString,'',$nh2 % $tlen--,1);
    $nhnum = $nh1 + $nh2 + $nh3;
    $mdKey = substr(md5(md5(md5($sKey.$ch1).$ch2.$iKey).$ch3),$nhnum % 8,$knum % 8 + 16);
    $tmp = '';
    $j=0; $k = 0;
    $tlen = strlen($sString);
    $klen = strlen($mdKey);
    for ($i=0; $i<$tlen; $i++)
    {
        $k = $k == $klen ? 0 : $k;
        $j = strpos($sChars,$sString{$i})-$nhnum - ord($mdKey{$k++});
        while ($j<0) $j+=64;
        $tmp .= $sChars{$j};
    }
    $tmp = str_replace(array('-','_','.'),array('+','/','='),$tmp);
    return trim(base64_decode($tmp));
}
原文地址:https://www.cnblogs.com/ahwu/p/4098232.html