zoukankan      html  css  js  c++  java
  • 字符串生成及加密

    
    
    <?php
    namespace actionstrlib;
    
    class basicAction extends action
    {
        // 数值型字符串,代号 1
        private $cs1 = array('0','1','2','3','4','5','6','7','8','9');
        // 纯小写字符串,代号 2
        private $cs2 = array('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');
        // 多类型字符串,代号 3
        private $cs3 = 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 function randstr($len,$type=1)
        {
            if( $len<1 ) return false;
            $RET = '';
            switch( $type )
            {
                case 1:
                    $RET = $RET.$this->randcs1(false);
                    for( $i=2; $i<=$len; $i++ ) $RET = $RET.$this->randcs1();
                    return $RET;
                case 2:
                    for( $i=1; $i<=$len; $i++ ) $RET = $RET.$this->cs2[rand(0,25)];
                    return $RET;
                case 3:
                    $RET = $RET.$this->randcs3(false);
                    for( $i=2; $i<=$len; $i++ ) $RET = $RET.$this->randcs3();
                    return $RET;
                default:
                    return false;
            }
        }
        
        private function randcs1($zero=true)
        {
            $from = 1;
            if( $zero ) $from = 0;
            return $this->cs1[rand($from,9)];
        }
        
        private function randcs3($zero=true)
        {
            $from = 1;
            if( $zero ) $from = 0;
            return $this->cs3[rand($from,61)];
        }
        
        public function adpassword($password){
            $a=range('A','Z');
            $b=range('a','z');
            $c=range(0,9);
            $a=implode('',$a);
            $b=implode('',$b);
            $c=implode('',$c);
            $res=$a.$b.$c;//生成随机字符串的源字符串
            $str='';//初始化一个空的字符串
            for($i=1;$i<=32;$i++){
                $tmp=str_shuffle($res);
                $str.=$tmp[0];
            }
            //$str 为我们生成的随机密码
            $result_str=md5($password.$str);
            $data=array(
                'password'=>$result_str,
                'salt'=>$str
                );
            return $data;
        }
    
        //可逆加密  key  密钥
        /** $data = 'orderid=11';    // 被加密信息
            $key = '3n4w';    // 密钥
            $encrypt = A('strlib/basic/encrypt',array($data, $key));
            $decrypt = A('strlib/basic/decrypt',array($encrypt, $key));
            echo $encrypt, "
    ", $decrypt;
        **/
        public function encrypt($data, $key)
        {
            $key    =    md5($key);
            $x        =    0;
            $len    =    strlen($data);
            $l        =    strlen($key);
            $char = $str = '';
            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);
        }
    
        public function decrypt($data, $key)
        {
            $key = md5($key);
            $x = 0;
            $data = base64_decode($data);
            $len = strlen($data);
            $l = strlen($key);
            $char = $str = '';
            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;
        }
    }
    
    
    
    
    ?>
    
  • 相关阅读:
    链表VS数组
    数组VS集合
    最好、最坏、平均、均摊时间复杂度
    代码时间、空间复杂度分析
    “echo >”和“echo >>”的区别
    两数之和
    hadoop学习之----------IntelliJ IDEA上实现MapReduce中最简单的单词统计的程序(本地 和 hadoop 两种实现方式)
    Ubuntu16.04中解决关于The Internet Topology Zoo 的gml文件的读取并画图的问题
    Ubuntu16.04解决Ubuntu Sofware打开后无反应
    Ubuntu16.04中如何启用floodlight的一种方式
  • 原文地址:https://www.cnblogs.com/xiaofei723/p/14924033.html
Copyright © 2011-2022 走看看