程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 網頁編程 >> PHP編程 >> 關於PHP編程 >> php實現aes加密類分享

php實現aes加密類分享

編輯:關於PHP編程

     這篇文章主要介紹了php實現的aes加密類,代碼中有使用方法,需要的朋友可以參考下

     代碼如下: <?php   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;   }     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));    
    1. 上一頁:
    2. 下一頁:
    Copyright © 程式師世界 All Rights Reserved