核心代碼:
?phpnamespace Aes;html' target='_blank'>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- * 解密方法,對數據進行解密,返回解密后的數據 * @param string $data 要解密的數據 * @return string public function decrypt($data) return openssl_decrypt($data, $this- method, $this- secret_key, $this- options, $this- }
測試代碼:
$aes = new OpenSSLAES( 12345678 $encrypted = $aes- encrypt( bbm是一家很有意思的公司 echo 要加密的字符串:bbm是一家很傻逼的公司 br 加密后的字符串: , $encrypted, hr $decrypted = $aes- decrypt($encrypted);echo 要解密的字符串: , $encrypted, br 解密后的字符串: , $decrypted;
相關推薦:
加密解密 php blowfish加密解密算法
PHP中加密解密函數與DES加密解密實例,phpdes加密解密實例
php rsa加密解密實例,rsa加密解密實例
以上就是PHP實現AES加密解密核心代碼以及測試代碼的詳細內容,PHP教程
鄭重聲明:本文版權歸原作者所有,轉載文章僅為傳播更多信息之目的,如作者信息標記有誤,請第一時間聯系我們修改或刪除,多謝。
新聞熱點
疑難解答