解密算法

2020-10-24  本文已影响0人  PHPer_

<?php
$num = 20;

class Mybase64
{
private static $map = array("0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "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", "[", "]");

public static function encode($n)
{
    if (!is_int($n)) return false;
    $a = (int)floor($n / 64);
    $b = $n % 64;
    $s = $a ? self::encode($a) : '';
    $s1 = self::$map[$b];
    return ($s). $s1;
}

public static function decode($code)
{
    // 写一个 解密 上面 加密的
    $arr = str_split($code);
    $key = [];
    foreach ($arr as $v) {
        $key[] = array_search($v,self::$map);
    }
    global $value;
    $i = 0;
    $count = count($key)-1;
    if ($count == 0) {
        return $key[0];
    }
    foreach ($key as $k => $val) {
        $i++;
        if (!$value) {
            $num = $val * 64;
            $value = $num + $key[++$k];
        }elseif ($i <= $count) {
            $num = $value * 64;
            $value = $num + $key[++$k];
        }
    }
    return $value;
}

public static function decodee($code){
    $map = self::$map;
    $map_f = array_flip($map);
    $b = 0;
    $long = strlen($code);
    if($long>0){
        $pow = pow(64,$long-1);
        $new_code = substr($code,1);
        $c = $map_f[$code[0]]*$pow;
        $b = $c+self::decodee($new_code);
    }
    return $b;
}

}

echo 'summer方法';
echo '
';
a = new Mybase64();b = a->encode(num);
echo '要加密的数字:'.num; echo '<br>'; echo '密码:'.b;
echo '
';
de =a->decode(b); echo '解密后:'.de;
echo '
';
if (num==de) {
echo "结果OK";
}else{
echo "错误";
}

echo '
';
echo '
';
echo 'kevin方法';
echo '
';

b =a->encode(num); echo '要加密的数字:'.num;
echo '
';
echo '密码:'.b; echo '<br>';de = a->decodee(b);
echo '解密后:'.de; echo '<br>'; if (num==$de) {
echo "结果OK";
}else{
echo "错误";
}

上一篇下一篇

猜你喜欢

热点阅读