美文网首页
2019-12-30

2019-12-30

作者: 言雨朦胧 | 来源:发表于2019-12-30 13:45 被阅读0次

    PHP5.6 与 PHP7 加解密区别
    PHP5.6

    <?php
    class Prpcrypt
    {
        public $key;
        function __construct($k)
        {
            $this->key = base64_decode($k . "=");
        }
        public function encrypt($text)
        {
            try {
                // 网络字节序
                $size = mcrypt_get_block_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CBC);
                $module = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', MCRYPT_MODE_CBC, '');
                $iv = substr($this->key, 0, 16);
                mcrypt_generic_init($module, $this->key, $iv);
                //加密
                $encrypted = mcrypt_generic($module, $text);
                mcrypt_generic_deinit($module);
                mcrypt_module_close($module);
                //print(base64_encode($encrypted));
                //使用BASE64对加密后的字符串进行编码
                return base64_encode($encrypted);
            } catch (Exception $e) {
                print $e;
                return false;
            }
        }
        public function decrypt($encrypted)
        {
            try {
                $ciphertext_dec = base64_decode($encrypted);
                $module = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', MCRYPT_MODE_CBC, '');
                $iv = substr($this->key, 0, 16);
                mcrypt_generic_init($module, $this->key, $iv);
                $decrypted = mdecrypt_generic($module, $ciphertext_dec);
                mcrypt_generic_deinit($module);
                mcrypt_module_close($module);
            } catch (Exception $e) {
                return false;
            }
            return $decrypted;
            
            
        }
        function getRandomStr()
        {
            $str = "";
            $str_pol = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz";
            $max = strlen($str_pol) - 1;
            for ($i = 0; $i < 16; $i++) {
                $str .= $str_pol[mt_rand(0, $max)];
            }
            return $str;
        }
    }
    
    $k = "abc1237890123456789012345678901234567890abc";
    $decrypStr = "hello";
    $cryptObj = new Prpcrypt($k);
    $ret = $cryptObj->encrypt($decrypStr);
    
    $str = "SR4RIdC6WpiYS00yijC6Wg==";
    $ret = $cryptObj->decrypt($str);
    var_dump($ret);
    

    PHP7 加解密方法

    <?php
    
    class Prpcrypt
    {
        public $key;
        function __construct($k)
        {
            $this->key = base64_decode($k . "=");
        }
        public function encrypt($text)
        {
            try {
                $iv = substr($this->key, 0, 16);
                $encrypted = openssl_encrypt($text, 'AES-256-CBC', $this->key, OPENSSL_RAW_DATA, $iv);
            } catch (Exception $e) {
                print $e;
                return false;
            }
    
    //        openssl_decrypt(base64_decode($encrypted), 'AES-256-CBC', $this->key, OPENSSL_RAW_DATA, $iv);
            var_dump(base64_encode($encrypted));
            return base64_encode($encrypted);
        }
        public function decrypt($encrypted)
        {
            try {
                $ciphertext_dec = base64_decode($encrypted);
                $iv = substr($this->key, 0, 16);
                $decrypted = openssl_decrypt($ciphertext_dec, 'AES-256-CBC', $this->key, OPENSSL_RAW_DATA,$iv);
            } catch (Exception $e) {
                return false;
            }
            return $decrypted;
    
    
        }
        function getRandomStr()
        {
            $str = "";
            $str_pol = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz";
            $max = strlen($str_pol) - 1;
            for ($i = 0; $i < 16; $i++) {
                $str .= $str_pol[mt_rand(0, $max)];
            }
            return $str;
        }
    }
    
    $k = "abc1237890123456789012345678901234567890abc";
    $decrypStr = "hello";
    $cryptObj = new Prpcrypt($k);
    $ret = $cryptObj->encrypt($decrypStr);
    echo  $ret . "\n....";
    
    $str = "fvu2cyYrQ2QKbUEfDl5qaw==";
    $ret = $cryptObj->decrypt($str);
    var_dump($ret);
    
    

    相关文章

      网友评论

          本文标题:2019-12-30

          本文链接:https://www.haomeiwen.com/subject/obxmoctx.html