分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > 软件开发

PHP的AES加密

发布时间:2023-09-06 02:21责任编辑:傅花花关键词:PHP
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加密

原文地址:http://blog.51cto.com/phpme/2313137

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved