Obscure / encrypt an order number as another number: symmetrical, “random” appearance?

后端 未结 7 525
长情又很酷
长情又很酷 2020-12-13 02:44

Client has an simple increasing order number (1, 2, 3...). He wants end-users to receive an 8- or 9- digit (digits only -- no characters) \"random\" number. Obviously, this

7条回答
  •  孤街浪徒
    2020-12-13 03:00

    5,1=>3,2=>9,3=>2,4=>7,5=>6,6=>1,7=>8,8=>0,9=>4);
    
    function enc($e,$cry,$k){
        if(strlen($e)>10)die("max encrypt digits is 10");
        if(strlen($e) >= $k)die("Request encrypt must be lesser than its length");
        if(strlen($e) ==0)die("must pass some numbers");
    
        $ct =  $e;
        $jump = ($k-1)-strlen($e);
        $ency = $cry[(strlen($e))];
        $n = 0;
        for($a=0;$a<$k-1;$a++){
            if($jump > 0){
                if($a%2 == 1){
                    $ency .=rand(0,9);
                    $jump -=1;
                }else{
                    if(isset($ct[$n])){
                        $ency.=$cry[$ct[$n]];
                        $n++;
                    }else{
                        $ency .=rand(0,9);
                        $jump -=1;
                    }
                }
            }else{
                $ency.= $cry[$ct[$n]];
                $n++;
            }
        }
        return $ency;
    }
    
    function dec($e,$cry){
        //$decy = substr($e,6);
        $ar = str_split($e,1);
        $len = array_search($ar[0], $cry);
        $jump = strlen($e)-($len+1);
        $val = "";
        for($i=1;$i0){
                    //$val .=array_search($e[$i], $cry);
                    $jump--;
                }else{
                    $val .=array_search($e[$i], $cry);
                }
            }else{
                if($len > 0){
                    $val .=array_search($e[$i], $cry);
                    $len--;
                }else{
                    $jump--;
                }
            }
        }
        return $val;
    }
    if(isset($_GET["n"])){
        $n = $_GET["n"];
    }else{
        $n = 1000;
    }
    
    $str = 1253;
    $str = enc($str,$cry,15);
    echo "Encerypted Value : ".$str ."
    "; $str = dec($str,$cry); echo "Decrypted Value : ".$str ."
    "; ?>

提交回复
热议问题