zoukankan      html  css  js  c++  java
  • php 加密解密

    function lock($data) {
        $chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-=+";
        $key = 'z123j456s789';
        if($data['type'] == 'encode') {
            $nh = rand(0,64);
            $ch = $chars[$nh];
            $mdKey = md5($key.$ch);
            $mdKey = substr($mdKey,$nh%8, $nh%8+7);
            $txt = base64_encode($data['param']);
            $tmp = '';
            $i=0;$j=0;$k = 0;
            for ($i=0; $i<strlen($txt); $i++) {
                $k = $k == strlen($mdKey) ? 0 : $k;
                $j = ($nh+strpos($chars,$txt[$i])+ord($mdKey[$k++]))%64;
                $tmp .= $chars[$j];
            }
            return urlencode($ch.$tmp);
        }else if($data['type'] == 'decode') {
            $txt = urldecode($data['param']);
            $ch = $txt[0];
            $nh = strpos($chars,$ch);
            $mdKey = md5($key.$ch);
            $mdKey = substr($mdKey,$nh%8, $nh%8+7);
            $txt = substr($txt,1);
            $tmp = '';
            $i=0;$j=0; $k = 0;
            for ($i=0; $i<strlen($txt); $i++) {
                $k = $k == strlen($mdKey) ? 0 : $k;
                $j = strpos($chars,$txt[$i])-$nh - ord($mdKey[$k++]);
                while ($j<0) $j+=64;
                $tmp .= $chars[$j];
            }
            return base64_decode($tmp);
        }
    }
    
    //加密
    $encode = lock(array('param' => '测测哈哈', 'type' => 'encode'));
    echo $encode.'<br/>';
    //解密
    echo lock(array('param' => $encode, 'type' => 'decode'));
    

      测试结果:

  • 相关阅读:
    对类对象使用new时地址分配的情况
    c++堆与栈的简单认识
    多态公有继承
    Cookie & Session & JSP入门
    Response & ServletContext
    Java网络编程篇文章阅读顺序
    URL编程
    UDP网络编程
    TCP网络编程
    InetAddress类的使用
  • 原文地址:https://www.cnblogs.com/zheart/p/9284465.html
Copyright © 2011-2022 走看看