欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

PHP實現(xiàn)的AES加密、解密封裝類與用法示例

 更新時間:2018年08月02日 14:31:34   作者:u011474028  
這篇文章主要介紹了PHP實現(xiàn)的AES加密、解密封裝類與用法,結(jié)合實例形式分析了php封裝的aes加密解密操作類及相關(guān)使用技巧,需要的朋友可以參考下

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

<?php
/**
 * Class AES
 * 用于AES加解密數(shù)據(jù)
 * 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;
  }
  /**
   * 加密數(shù)據(jù)
   * @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;
  }
  /**
   * 解密數(shù)據(jù)
   * @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);
  }
  /**
   * 填充數(shù)據(jù)到分組大小的整數(shù)倍
   * @param null $data
   *
   * @return string
   */
  protected function padding($data = null)
  {
    $blockSize = 32; //MCRYPT_RIJNDAEL_256算法的分組大小是32字節(jié)
    $pad = $blockSize - (strlen($data) % $blockSize);
    return $data . str_repeat(chr($pad), $pad);
  }
  /**
   * 去掉填充的數(shù)據(jù)
   * @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 = 'www.dbjr.com.cn';
$chgstr = $aes->encrypt($str);
echo $chgstr;
echo "<br/>";
$rstr = $aes->decrypt($chgstr);
echo $rstr;
?>

運行結(jié)果:

pDyiRRNaxlss2b6SgoiVPdkD2m1QWhX393lh2iFgGdY=
www.dbjr.com.cn

PS:關(guān)于加密解密感興趣的朋友還可以參考本站在線工具:

文字在線加密解密工具(包含AES、DES、RC4等):
http://tools.jb51.net/password/txt_encode

MD5在線加密工具:
http://tools.jb51.net/password/CreateMD5Password

在線散列/哈希算法加密工具:
http://tools.jb51.net/password/hash_encrypt

在線MD5/hash/SHA-1/SHA-2/SHA-256/SHA-512/SHA-3/RIPEMD-160加密工具:
http://tools.jb51.net/password/hash_md5_sha

在線sha1/sha224/sha256/sha384/sha512加密工具:
http://tools.jb51.net/password/sha_encode

更多關(guān)于PHP相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《php加密方法總結(jié)》、《PHP編碼與轉(zhuǎn)碼操作技巧匯總》、《PHP數(shù)學(xué)運算技巧總結(jié)》、《PHP數(shù)組(Array)操作技巧大全》、《php字符串(string)用法總結(jié)》、《PHP數(shù)據(jù)結(jié)構(gòu)與算法教程》、《php程序設(shè)計算法總結(jié)》及《php正則表達式用法總結(jié)

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

相關(guān)文章

最新評論