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

    <?php
    
    namespace Aes;
    error_reporting(E_ALL);
    ini_set('display_errors', '1');
    class Aes
    {
        /**
         * var string $method 加解密方法,可通过openssl_get_cipher_methods()获得
         */
        protected $method;
    
        /**
         * var string $secret_key 加解密的密钥
         */
        protected $secret_key;
    
        /**
         * var string $iv 加解密的向量,有些方法需要设置比如CBC
         */
        protected $iv;
    
        /**
         * var string $options (不知道怎么解释,目前设置为0没什么问题)
         */
        protected $options;
    
        /**
         * 构造函数
         *
         * @param string $key 密钥
         * @param string $method 加密方式
         * @param string $iv iv向量
         * @param mixed $options 还不是很清楚
         *
         */
        public function __construct($key, $method = 'AES-128-ECB', $iv = '', $options = 0)
        {
            // key是必须要设置的
            $this->secret_key = isset($key) ? $key : 'morefun';
    
            $this->method = $method;
    
            $this->iv = $iv;
    
            $this->options = $options;
        }
    
        /**
         * 加密方法,对数据进行加密,返回加密后的数据
         *
         * @param string $data 要加密的数据
         *
         * @return string
         *
         */
        public function encrypt($data)
        {
            return openssl_encrypt($data, $this->method, $this->secret_key, $this->options, $this->iv);
        }
    
        /**
         * 解密方法,对数据进行解密,返回解密后的数据
         *
         * @param string $data 要解密的数据
         *
         * @return string
         *
         */
        public function decrypt($data)
        {
            return openssl_decrypt($data, $this->method, $this->secret_key, $this->options, $this->iv);
        }
    }
    
    
    
    $aes = new Aes('12345678');
    
    $str = 'hello';
    $encrypted = $aes->encrypt($str);
    
    echo '要加密的字符串:'.$str.'<br>加密后的字符串:', $encrypted, '<hr>';
    
    $decrypted = $aes->decrypt($encrypted);
    
    echo '要解密的字符串:', $encrypted, '<br>解密后的字符串:', $decrypted;
    

      

    <?php

    namespace Aes;
    error_reporting(E_ALL);
    ini_set('display_errors', '1');
    class Aes
    {
    /**
    * var string $method 加解密方法,可通过openssl_get_cipher_methods()获得
    */
    protected $method;

    /**
    * var string $secret_key 加解密的密钥
    */
    protected $secret_key;

    /**
    * var string $iv 加解密的向量,有些方法需要设置比如CBC
    */
    protected $iv;

    /**
    * var string $options (不知道怎么解释,目前设置为0没什么问题)
    */
    protected $options;

    /**
    * 构造函数
    *
    * @param string $key 密钥
    * @param string $method 加密方式
    * @param string $iv iv向量
    * @param mixed $options 还不是很清楚
    *
    */
    public function __construct($key, $method = 'AES-128-ECB', $iv = '', $options = 0)
    {
    // key是必须要设置的
    $this->secret_key = isset($key) ? $key : 'morefun';

    $this->method = $method;

    $this->iv = $iv;

    $this->options = $options;
    }

    /**
    * 加密方法,对数据进行加密,返回加密后的数据
    *
    * @param string $data 要加密的数据
    *
    * @return string
    *
    */
    public function encrypt($data)
    {
    return openssl_encrypt($data, $this->method, $this->secret_key, $this->options, $this->iv);
    }

    /**
    * 解密方法,对数据进行解密,返回解密后的数据
    *
    * @param string $data 要解密的数据
    *
    * @return string
    *
    */
    public function decrypt($data)
    {
    return openssl_decrypt($data, $this->method, $this->secret_key, $this->options, $this->iv);
    }
    }



    $aes = new Aes('12345678');

    $str= 'hello';
    $encrypted = $aes->encrypt($str);

    echo '要加密的字符串:'.$str.'<br>加密后的字符串:', $encrypted, '<hr>';

    $decrypted = $aes->decrypt($encrypted);

    echo '要解密的字符串:', $encrypted, '<br>解密后的字符串:', $decrypted;
  • 相关阅读:
    爬虫requests模块 1
    刷题记录:ctf473831530_2018_web_virink_web
    刷题记录:[RCTF 2019]Nextphp
    CTF XSS
    PHP 7 源码学习 序&第一、二章
    刷题记录:[安洵杯 2019]iamthinking
    刷题记录:[GWCTF 2019]枯燥的抽奖
    preg_match绕过总结
    刷题记录:[SWPU2019]easy_python
    刷题记录:[GWCTF 2019]我有一个数据库
  • 原文地址:https://www.cnblogs.com/brady-wang/p/9754048.html
Copyright © 2011-2022 走看看