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));
相關(guān)文章
thinkPHP+PHPExcel實現(xiàn)讀取文件日期的方法(含時分秒)
這篇文章主要介紹了thinkPHP+PHPExcel實現(xiàn)讀取文件日期的方法,可實現(xiàn)針對時分秒的形式進行讀取的功能,涉及PHPExcel中ExcelToPHP函數(shù)的相關(guān)使用方法,需要的朋友可以參考下2016-07-07php+ffmpeg如何獲取視頻縮略圖、視頻分辨率等相關(guān)信息
這篇文章主要給大家介紹了關(guān)于php+ffmpeg如何獲取視頻縮略圖、視頻分辨率等相關(guān)信息的相關(guān)資料,文中通過實例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2022-03-03ThinkPHP3.1新特性之多數(shù)據(jù)庫操作更加完善
對于早期版本的ThinkPHP來說,切換數(shù)據(jù)庫需要使用高級模型,而現(xiàn)在的3.1版本則可以更加輕松的解決了。這篇文章主要介紹了ThinkPHP3.1對多數(shù)據(jù)庫操作,需要的朋友可以參考下2014-06-06PHP date()函數(shù)警告: It is not safe to rely on the system解決方法
這篇文章主要介紹了PHP date()函數(shù)警告: It is not safe to rely on the system解決方法,其實就是時區(qū)設置不正確造成的,本文提供了兩種方法來解決這個問題,需要的朋友可以參考下2014-08-08