Home  >  Article  >  Backend Development  >  请问PHP如何通过openssl实现AES-128加密算法?

请问PHP如何通过openssl实现AES-128加密算法?

WBOY
WBOYOriginal
2016-06-06 20:35:171481browse

下面这段脚本是来自七牛提供的

<code>$ echo -n [AES128KEY] | openssl rsautl -encrypt -oaep -inkey [QINIU_PUB_KEY_FILE] -pubin | openssl base64 -A | tr "+/" "-_"
</code>

我知道PHP有提供openssl模块,但是我对这个模块不了解,请问有没有了解的朋友,将上面这段代码改写成PHP的。非常感谢。

回复内容:

下面这段脚本是来自七牛提供的

<code>$ echo -n [AES128KEY] | openssl rsautl -encrypt -oaep -inkey [QINIU_PUB_KEY_FILE] -pubin | openssl base64 -A | tr "+/" "-_"
</code>

我知道PHP有提供openssl模块,但是我对这个模块不了解,请问有没有了解的朋友,将上面这段代码改写成PHP的。非常感谢。

<code>class AES_128_CW {
    private $_iv = '';
    private $_secret = '';

    public function __construct($iv,$secret){
        $this->_iv = substr($iv.'0000000000000000', 0,16);//可以忽略这一步,只要你保证iv长度是16
        $this->_secret = hash('md5',$secret,true);
    }

    public function decode($secretData){
        return openssl_decrypt(urldecode($secretData),'aes-128-cbc',$this->_secret,false,$this->_iv);
    }

    public function encode($data){
        return urlencode(openssl_encrypt($data,'aes-128-cbc',$this->_secret,false,$this->_iv));
    }
</code>

}

Statement:
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