php實現(xiàn)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));
相關文章
php使用shmop函數(shù)創(chuàng)建共享內(nèi)存減少負載的方法
這篇文章主要介紹了php使用shmop函數(shù)創(chuàng)建共享內(nèi)存減少負載,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-12-12
php判斷手機瀏覽還是web瀏覽,并執(zhí)行相應的動作簡單實例
下面小編就為大家?guī)硪黄猵hp判斷手機瀏覽還是web瀏覽,并執(zhí)行相應的動作簡單實例。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給的大家做個參考。一起跟隨小編過來看看吧2016-07-07
如何修改Laravel中url()函數(shù)生成URL的根地址
這篇文章主要給大家介紹了關于如何修改Laravel中url()函數(shù)生成URL根地址的相關資料,文中通過示例代碼介紹的非常詳細,對大家學習或者使用laravel具有一定的參考學習價值,需要的朋友們下面跟著小編來一起學習學習吧。2017-08-08

