>  기사  >  백엔드 개발  >  PHP7에서 AES/ECB/PKCS5Padding 암호화를 구현하는 방법

PHP7에서 AES/ECB/PKCS5Padding 암호화를 구현하는 방법

醉折花枝作酒筹
醉折花枝作酒筹앞으로
2021-06-21 09:11:383933검색

이 글에서는 PHP7에서 AES/ECB/PKCS5Padding 암호화를 구현하는 방법을 소개합니다. 도움이 필요한 친구들이 모두 참고할 수 있기를 바랍니다.

PHP7에서 AES/ECB/PKCS5Padding 암호화를 구현하는 방법

class CryptAES
{
   /**
     * 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 = 'AES-128-ECB', $iv = '', $options = 0)
    {
        // key是必须要设置的
        $this->secret_key = isset($key) ? $key : exit('key为必须项');

        $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);
    }
}

추천 학습: php 비디오 튜토리얼

위 내용은 PHP7에서 AES/ECB/PKCS5Padding 암호화를 구현하는 방법의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

성명:
이 기사는 csdn.net에서 복제됩니다. 침해가 있는 경우 admin@php.cn으로 문의하시기 바랍니다. 삭제