PHP implements AES encryption and decryption core code and test code

不言
Release: 2023-04-03 18:48:01
Original
1922 people have browsed it

This article brings you the core code and test code for implementing AES encryption and decryption in PHP. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

Core code:

<?php
 
namespace Aes;
 
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 = &#39;AES-128-ECB&#39;, $iv = &#39;&#39;, $options = 0)
    {
        // key是必须要设置的
        $this->secret_key = isset($key) ? $key : &#39;morefun&#39;;
 
        $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->iv);
    }
 
    /**
     * 解密方法,对数据进行解密,返回解密后的数据
     *
     * @param string $data 要解密的数据
     *
     * @return string
     *
     */
    public function decrypt($data)
    {
        return openssl_decrypt($data, $this->method, $this->secret_key, $this->options, $this->iv);
    }
}
Copy after login

Test code:

$aes = new OpenSSLAES(&#39;12345678&#39;);
 
$encrypted = $aes->encrypt(&#39;bbm是一家很有意思的公司&#39;);
 
echo &#39;要加密的字符串:bbm是一家很傻逼的公司<br>加密后的字符串:&#39;, $encrypted, &#39;<hr>&#39;;
 
$decrypted = $aes->decrypt($encrypted);
 
echo &#39;要解密的字符串:&#39;, $encrypted, &#39;<br>解密后的字符串:&#39;, $decrypted;
Copy after login

Related recommendations:

Encryption and decryption php blowfish encryption and decryption algorithm

Encryption and decryption functions in PHP and DES encryption and decryption examples, phpdes encryption and decryption examples

php rsa encryption and decryption examples, rsa encryption and decryption examples

The above is the detailed content of PHP implements AES encryption and decryption core code and test code. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template