加密的原理异或&置换&位移

1.异或代码

    function encrypt($data,$key){
    $content = $data ^ $key;
    
    return $content; 
  }

  function decrypt($data,$key){
    $content = $data ^ $key;
    return $content;
  }
  $data="this is test";
  $key =uniqid();
  $content = encrypt($data,$key);
  echo $content;
  $content = decrypt($content,$key);

2.置换

  $str="abcdefg";
  $strs="";
  for($i=0;$i<strlen($str);$i++){
    $num = ord($str[$i])+10;
    $strs.=chr($num);    
  }

for($i=0;$i<strlen($strs);$i++){
    $num = ord($strs[$i])-10;
    echo chr($num);
}

3.位移

 $num=0;
  $id = uniqid();
  $target = "";
  $str="abcdefg";
  for($j=0;$j <strlen($id);$j++){
    $num = $num + ord($id[$j]);
  }
  for($i=0;$i<strlen($str);$i++){
    $index = ($i+3)%strlen($str);
    $target.=$str[$index];
  }
  echo $str;
  echo "\r\n";
  $totalLen = strlen($target);
for($i=0;$i< strlen($target);$i++){
    $index = abs(($i+3+1)%$totalLen);
    echo $target[$index];
}

 4.案例将一个10的整形数字通过程序变为二进制输出

    function aa($num){
        $info="";
        while ($num > 0) {
            echo $num;
            echo "\r\n";
            $num = $num >>1;
            $info .= $num & 1;
        }
        echo $info;
    }

    $num=10;
    aa($num);

 

posted on 2020-08-17 20:17  孤灯引路人  阅读(333)  评论(0编辑  收藏  举报

导航