<?php namespace Aes; class Aes { /** * var string $method 加解密方法,可經過openssl_get_cipher_methods()得到 */ protected $method; /** * var string $secret_key 加解密的密鑰 */ protected $secret_key; /** * var string $iv 加解密的向量,有些方法須要設置好比CBC */ protected $iv; /** * var string $options (不知道怎麼解釋,目前設置爲0沒什麼問題) */ protected $options; /** * 構造函數 * * @param string $key 密鑰 * @param string $method 加密方式 * @param string $iv iv向量 * @param mixed $options 還不是很清楚 * */ public function __construct($key, $method = 'AES-128-ECB', $iv = '', $options = 0) { // key是必需要設置的 $this->secret_key = isset($key) ? $key : 'morefun'; $this->method = $method; $this->iv = $iv; $this->options = $options; } /** * 加密方法,對數據進行加密,返回加密後的數據 * * @param string $data 要加密的數據 * * @return string * */ public function encrypt($data) { return openssl_encrypt($data, $this->method, $this->secret_key, $this->options, $this->iv); } /** * 解密方法,對數據進行解密,返回解密後的數據 * * @param string $data 要解密的數據 * * @return string * */ public function decrypt($data) { return openssl_decrypt($data, $this->method, $this->secret_key, $this->options, $this->iv); } }
$aes = new OpenSSLAES('12345678'); $encrypted = $aes->encrypt('bbm是一家很傻逼的公司'); echo '要加密的字符串:bbm是一家很傻逼的公司<br>加密後的字符串:', $encrypted, '<hr>'; $decrypted = $aes->decrypt($encrypted); echo '要解密的字符串:', $encrypted, '<br>解密後的字符串:', $decrypted;
在線Aes加解密地址:http://www.seacha.com/tools/aes.htmlphp
文章引用:https://blog.csdn.net/GlatChen/article/details/79978875;html