PHP AES加密和解密

文章描述:

thinkphp6第三方类 AES加密和解密

php版本:7.3

php框架:thinkphp6

 

php.ini

搜索extension=openssl打开注释

 

1、extend新建EncryptionTool/EncryptionTool.php构造工具类:

<?php

namespace EncryptionTool;

class EncryptionTool{

    public static function enAES($originTxt, $key): string{

        return base64_encode(openssl_encrypt($originTxt, 'AES-128-ECB',$key, OPENSSL_RAW_DATA));
    }

    public static function deAES($originTxt, $key): string{

        $data = base64_decode($originTxt);
        return openssl_decrypt($data,'AES-128-ECB',$key, OPENSSL_RAW_DATA);
    }

}

2、控制器中使用

use EncryptionTool\EncryptionTool;
$hello = "Hello World";

echo "原始数据: " . $hello . "<br/>";
$data = EncryptionTool::enAES($hello, "1234567891234567");
echo "加密后: " . $data . "<br/>";
echo "解密后: " . EncryptionTool::deAES($data, "1234567891234567") . "<br/>";

 

发布时间:2023/05/25

发表评论