国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 語言 > PHP > 正文

PHP實現的AES加密、解密封裝類與用法示例

2024-05-05 00:04:49
字體:
來源:轉載
供稿:網友

本文實例講述了PHP實現的AES加密、解密封裝類與用法。分享給大家供大家參考,具體如下:

<?php/** * Class AES * 用于AES加解密數據 * time:2018-04-27 */class AES{  protected $cipher = MCRYPT_RIJNDAEL_256; //AES加密算法  protected $mode = MCRYPT_MODE_CBC; //采用cbc加密模式  protected $key; //密鑰  protected $iv; //cbc模式加密向量,如為空將采用密鑰代替  /**   * AES constructor.   *   * @param   $key 密鑰   * @param null $iv 向量 可選 如為空將采用密鑰代替   *   * @throws Exception   */  public function __construct($key, $iv = NULL)  {    if (!extension_loaded("mcrypt")) {//      throw new /Exception("mcrypt extension do not exist. it was DEPRECATED in PHP 7.1.0, and REMOVED in PHP 7.2.0. use OpenSSL instead");    }    $this->key = $key;    $this->iv = $iv;  }  /**   * 加密數據   * @param $data   *   * @return string   */  public function encrypt($data)  {    $td = mcrypt_module_open($this->cipher, '', $this->mode, '');    $key = hash("sha256", $this->key, true);    $iv = isset($this->iv) ? hash("sha256", $this->iv, true) : $key;    $data = $this->padding($data);    mcrypt_generic_init($td, $key, $iv);    $encryptedData = base64_encode(mcrypt_generic($td, $data));    mcrypt_generic_deinit($td);    mcrypt_module_close($td);    return $encryptedData;  }  /**   * 解密數據   * @param $data   *   * @return bool|string   */  public function decrypt($data)  {    $td = mcrypt_module_open($this->cipher, '', $this->mode, '');    $key = hash("sha256", $this->key, true);    $iv = isset($this->iv) ? hash("sha256", $this->iv, true) : $key;    mcrypt_generic_init($td, $key, $iv);    $decrypted_data = mdecrypt_generic($td, base64_decode($data));    mcrypt_generic_deinit($td);    mcrypt_module_close($td);    return $this->unPadding($decrypted_data);  }  /**   * 填充數據到分組大小的整數倍   * @param null $data   *   * @return string   */  protected function padding($data = null)  {    $blockSize = 32; //MCRYPT_RIJNDAEL_256算法的分組大小是32字節    $pad = $blockSize - (strlen($data) % $blockSize);    return $data . str_repeat(chr($pad), $pad);  }  /**   * 去掉填充的數據   * @param null $data   *   * @return bool|string   */  protected function unPadding($data = null)  {    $pad = ord($data[strlen($data) - 1]);    if ($pad > strlen($data)) {      return false;    }    if (strspn($data, chr($pad), strlen($data) - $pad) != $pad) {      return false;    }    return substr($data, 0, -1 * $pad);  }  /**   * @return mixed   */  public function getSecretKey()  {    return $this->key;  }  /**   * @param mixed $key   */  public function setSecretKey($key)  {    $this->key = $key;  }  /**   * @return null   */  public function getIv()  {    return $this->iv;  }  /**   * @param null $iv   */  public function setIv($iv)  {    $this->iv = $iv;  }}//使用方法:$keyStr = 'sq8f77fwhksk';$aes = new AES($keyStr);$str = 'm.survivalescaperooms.com';$chgstr = $aes->encrypt($str);echo $chgstr;echo "<br/>";$rstr = $aes->decrypt($chgstr);echo $rstr;?>

運行結果:

pDyiRRNaxlss2b6SgoiVPdkD2m1QWhX393lh2iFgGdY=
m.survivalescaperooms.com

希望本文所述對大家PHP程序設計有所幫助。


注:相關教程知識閱讀請移步到PHP教程頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表

圖片精選

主站蜘蛛池模板: 丰镇市| 普安县| 永年县| 西华县| 屏东县| 翼城县| 西昌市| 高淳县| 洞口县| 崇义县| 新建县| 长汀县| 白山市| 繁昌县| 黄梅县| 平果县| 林州市| 江西省| 额敏县| 岳阳县| 乌鲁木齐市| 巴东县| 阿城市| 邵东县| 德阳市| 甘德县| 陕西省| 池州市| 从化市| 漳州市| 泰顺县| 和龙市| 子洲县| 隆化县| 札达县| 长泰县| 苍溪县| 广南县| 肃宁县| 门头沟区| 福安市|