• AES加解密


    AES加密类

    <?php
    
    //php aes加密类
    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), "..");
    return $data;
    }
    }
    //使用方法
    $aes = new AESMcrypt($bit = 128, $key = 'abcdef1234567890', $iv = '0987654321fedcba', $mode = 'cbc');
    $c = $aes->encrypt('haowei.me');
    var_dump($aes->decrypt($c));

    demo

    <?php
    
    require_once('./AES.php');
    //$aes = new AES();
    $aes = new AES(true);// 把加密后的字符串按十六进制进行存储
    //$aes = new AES(true,true);// 带有调试信息且加密字符串按十六进制存储
    $key = "this is a 32 byte key";// 密钥
    $keys = $aes->makeKey($key);
    $encode = "123456";// 被加密的字符串
    $ct = $aes->encryptString($encode, $keys);
    echo "encode = ".$ct."<br>";
    $cpt = $aes->decryptString($ct, $keys);
    echo "decode = ".$cpt;
    ?>
  • 相关阅读:
    购买绝版书的好地方——淘宝
    ASP.NET MVC轻教程 Step By Step 1 ——入门
    ASP.NET MVC轻教程 Step By Step 2 ——View初探
    快速启动WebDev.WebServer的方法
    Surface RT使用手记
    ASP.NET MVC轻教程 Step By Step 3 ——使用ViewBag
    Asp.net MVC分页实例
    图示近四年来国外主流编程语言发展趋势
    ASP.NET MVC轻教程 Step By Step 4——Model、View和Controller
    Asp.net MVC使用KindEditor4
  • 原文地址:https://www.cnblogs.com/xiaobiaomei/p/9414286.html
Copyright © 2020-2023  润新知