成都创新互联网站制作重庆分公司

PHP的AES(高级加密标准AdvancedEncryptionStandard)加密

AES介绍
高级加密标准(英语:Advanced Encryption Standard,缩写:AES),在密码学中又称Rijndael加密法,是美国联邦政府采用的一种区块加密标准。

这个标准用来替代原先的DES,已经被多方分析且广为全世界所使用。
经过五年的甄选流程,高级加密标准由美国国家标准与技术研究院(NIST)于2001年11月26日发布于FIPS PUB 197,并在2002年5月26日成为有效的标准。
2006年,高级加密标准已然成为对称密钥加密中最流行的算法之一。

创新互联公司专注于企业全网营销推广、网站重做改版、云和网站定制设计、自适应品牌网站建设、成都h5网站建设商城网站建设、集团公司官网建设、成都外贸网站制作、高端网站制作、响应式网页设计等建站业务,价格优惠性价比高,为云和等各大城市提供网站开发制作服务。

class AES
{
    public $method = '';
    public $key = '';
    public $iv = '';

    public function __construct(string $method, string $key, string $iv)
    {
        if (!in_array($method, openssl_get_cipher_methods())) {
            throw new \Exception($method . ' encryption method is not support.');
        }
        $this->method = $method;
        $this->key = $key;
        $this->iv = $iv;
    }

    //AES加密
    public function aesEncryption(string $data): string
    {
        $result = openssl_encrypt($data, $this->method, $this->key, OPENSSL_RAW_DATA, $this->iv);
        return base64_encode($result);
    }

    //AES解密
    public function aesDecryption(string $data): string
    {
        return openssl_decrypt(base64_decode($data), $this->method, $this->key, OPENSSL_RAW_DATA, $this->iv);
    }
}

$config = [
    'AES-128-CBC1', //method加密方式  # AES-256-CBC等
    'helloworld', //key加密key
    md5(time() . uniqid(), true), //iv保证偏移量为16位
];

try{
    $obj = new AES(...$config);
    echo $encryptionResult = $obj->aesEncryption('Jack') . PHP_EOL;
    echo $decryptionResult = $obj->aesDecryption($encryptionResult) . PHP_EOL;
}catch (\Exception $e){
    exit($e->getMessage().PHP_EOL);
}

分享名称:PHP的AES(高级加密标准AdvancedEncryptionStandard)加密
本文网址:http://cxhlcq.com/article/gijppi.html

其他资讯

在线咨询

微信咨询

电话咨询

028-86922220(工作日)

18980820575(7×24)

提交需求

返回顶部