Blogger Information
Blog 40
fans 1
comment 0
visits 32445
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
AES加密技术初接触
李明伟的博客
Original
1233 people have browsed it

AES加密技术的PHP7.0以前版本

第一步——定义AES加密的类

class AES

第二步——定义AES的秘钥

private $secretkey = "aksjdhaskjhdkjas";

第三步——定义加密向量(加密向量定义为常量)

define('IV','1wsjdhask2hdkja1');

第四步——定义加密方法

public function encrypt($str){
    $encrypt_str = mcrypt_encrypt(MCRYPT_RIJNDAEL_128,$this->secretkey,$str,MCRYPT_MODE_CBC,IV);
    return base64_encode($encrypt_str);
}

其中的mcypt_encrypt函数对数据进行加密,参数为五个,依次为加密方法,秘钥,需要加密的内容,加密模型,加密向量

第五步——定义解密方法

public function decrypt($str){
    $str = base64_decode($str);
    $decrypt_str = mcrypt_decrypt(MCRYPT_RIJNDAEL_128,$this->secretkey,$str,MCRYPT_MODE_CBC,IV);
    return $decrypt_str;
}

其中的mcypt_decrypt函数对数据进行加密,参数为五个,依次为加密方法,秘钥,需要加密的内容,加密模型,加密向量

全部代码

实例

<?php
//php7版本以前使用
$aes = new AES();
$str = 'php天下第一';
$res_encrypt = $aes->encrypt($str);
echo $res_encrypt;
echo '<br>';
$res_decrypt = $aes->decrypt($res_encrypt);
echo $res_decrypt;
/*
 * AES加密
 */
class AES
{
    //定义秘钥
    private $secretkey = "aksjdhaskjhdkjas";
    public function __construct()
    {
            define('IV','1wsjdhask2hdkja1');//定义CBC模式16位偏移量(加密向量)
    }
    //加密,$str为要加密的数据
    public function encrypt($str){
        $encrypt_str = mcrypt_encrypt(MCRYPT_RIJNDAEL_128,$this->secretkey,$str,MCRYPT_MODE_CBC,IV);
        return base64_encode($encrypt_str);
    }
    //解密,$str为要解密的数据
    public function decrypt($str){
        $str = base64_decode($str);
        $decrypt_str = mcrypt_decrypt(MCRYPT_RIJNDAEL_128,$this->secretkey,$str,MCRYPT_MODE_CBC,IV);
        return $decrypt_str;
    }
}
?>

运行实例 »

点击 "运行实例" 按钮查看在线实例

php版本7.0版本以上的写法(参考学习)

实例

<?php
//php7.0以上的版本的写法
//$str = 'www.php.cn(PHP中文网)';
//$aes = new AES_OPENSSL();
//
//$res_en = $aes->encrypt($str);
//echo $res_en;
//echo '<br>==============================<br>';
//$res_de = $aes->decrypt($res_en);
//echo $res_de;
//
//
//// php 7.2
//class AES_OPENSSL {
//
//    public function __construct(){
//        $this->_cipher = "aes-128-gcm";	// 加密方式
//        $this->_options = 0; 			// options 是以下标记的按位或: OPENSSL_RAW_DATA 、 OPENSSL_ZERO_PADDING
//        $this->_tag = '';				// 使用 AEAD 密码模式(GCM 或 CCM)时传引用的验证标签
//
//        $ivlen = openssl_cipher_iv_length($this->_cipher);// 获得该加密方式的iv长度
//        // $this->_iv:非null的初始化向量
//        $this->_iv = openssl_random_pseudo_bytes($ivlen);// 生成相应长度的伪随机字节串作为初始化向量
//        $this->_key = 'sdfsaokjlwer98wasf';
//    }
//
//    public function encrypt($plaintext) {
//        $ciphertext = openssl_encrypt($plaintext, $this->_cipher, $this->_key, $this->_options, $this->_iv,$this->_tag);
//        return $ciphertext;
//    }
//
//    public function decrypt($ciphertext) {
//        $original_plaintext = openssl_decrypt($ciphertext, $this->_cipher, $this->_key, $this->_options, $this->_iv,$this->_tag);
//        return $original_plaintext;
//    }
//}

运行实例 »

点击 "运行实例" 按钮查看在线实例



Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!