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

    php实现的aes加密类,代码中有使用方法。

    <?php
    //php aes加密类
    class AESMcrypt {
    
    public $iv = null;
    public $key = null;
    public $bit = 128;
    private $cipher;
    
    public function __construct($bit, $key, $iv, $mode) {
    if(empty($bit) || empty($key) || empty($iv) || empty($mode))
    return NULL;
    
    $this->bit = $bit;
    $this->key = $key;
    $this->iv = $iv;
    $this->mode = $mode;
    
    switch($this->bit) {
    case 192:$this->cipher = MCRYPT_RIJNDAEL_192; break;
    case 256:$this->cipher = MCRYPT_RIJNDAEL_256; break;
    default: $this->cipher = MCRYPT_RIJNDAEL_128;
    } // www.jbxue.com
    
    switch($this->mode) {
    case 'ecb':$this->mode = MCRYPT_MODE_ECB; break;
    case 'cfb':$this->mode = MCRYPT_MODE_CFB; break;
    case 'ofb':$this->mode = MCRYPT_MODE_OFB; break;
    case 'nofb':$this->mode = MCRYPT_MODE_NOFB; break;
    default: $this->mode = MCRYPT_MODE_CBC;
    }
    }
    
    public function encrypt($data) {
    $data = base64_encode(mcrypt_encrypt( $this->cipher, $this->key, $data, $this->mode, $this->iv));
    return $data;
    }
    
    public function decrypt($data) {
    $data = mcrypt_decrypt( $this->cipher, $this->key, base64_decode($data), $this->mode, $this->iv);
    $data = rtrim(rtrim($data), "x00..x1F");
    return $data;
    }
    
    }
    //使用方法
    $aes = new AESMcrypt($bit = 128, $key = 'abcdef1234567890', $iv = '0987654321fedcba', $mode = 'cbc');
    $c = $aes->encrypt('haowei.me');
    var_dump($aes->decrypt($c));
  • 相关阅读:
    进程间通讯----消息队列和共享内存方式的实现
    初探 Yii2 的测试模式 index-test.php
    nginx缓存功能的设置
    php五大运行模式CGI,FAST-CGI,CLI,ISAPI,APACHE模式
    workerman如何写mysql连接池
    Varnish 一般是放在 Nginx 前面还是后面的?
    关于PATH_INFO
    Java8 Lambda表达式
    synchronized的一些记录
    类和实例
  • 原文地址:https://www.cnblogs.com/cfinder010/p/3552076.html
Copyright © 2011-2022 走看看