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

