PHP如何实现AES加密、解密?方法介绍(代码示例)

PHP如何实现AES加密、解密?方法介绍(代码示例),第1张

概述PHP如何实现AES加密解密?方法介绍(代码示例

1、mcrypt_encrypt AES加密,解密

class lib_desEnctyp{    private $key = "";    private $iv = "";    /**    * 构造,传递二个已经进行base64_encode的KEY与IV    *    * @param string $key    * @param string $iv    */    function __construct ($key, $iv)    {        if (empty($key) || empty($iv)) {            echo 'key and iv is not valID';            exit();        }        $this->key = $key;        $this->iv = $iv;    }    /**    *加密    * @param <type> $value    * @return <type>    */    public function encrypt ($value)    {        $td = mcrypt_module_open(MCRYPT_3DES, '', MCRYPT_MODE_CBC, '');        $iv = base64_decode($this->iv);        $value = $this->@R_419_5095@PKCS7($value);        $key = base64_decode($this->key);        mcrypt_generic_init($td, $key, $iv);        $ret = base64_encode(mcrypt_generic($td, $value));        mcrypt_generic_deinit($td);        mcrypt_module_close($td);        return $ret;    }    /**    *解密    * @param <type> $value    * @return <type>    */    public function decrypt ($value)    {        $td = mcrypt_module_open(MCRYPT_3DES, '', MCRYPT_MODE_CBC, '');        $iv = base64_decode($this->iv);        $key = base64_decode($this->key);        mcrypt_generic_init($td, $key, $iv);        $ret = trim(mdecrypt_generic($td, base64_decode($value)));        $ret = $this->Un@R_419_5095@PKCS7($ret);        mcrypt_generic_deinit($td);        mcrypt_module_close($td);        return $ret;    }    private function @R_419_5095@PKCS7 ($data)    {        $block_size = mcrypt_get_block_size('tripledes', 'cbc');        $@R_419_5095@_char = $block_size - (strlen($data) % $block_size);        $data .= str_repeat(chr($@R_419_5095@_char), $@R_419_5095@_char);        return $data;    }    private function Un@R_419_5095@PKCS7($text)    {        $pad = ord($text{strlen($text) - 1});        if ($pad > strlen($text)) {            return false;        }        if (strspn($text, chr($pad), strlen($text) - $pad) != $pad) {            return false;        }        return substr($text, 0, - 1 * $pad);    }}

2、openssl 加密,解密 [方式1]

/** * DES加密类 * User: gaowei * Date: 2017/12/12 * Time: 19:23 */class DesEncrypt {    private $key = "";    private $iv = "";    /**     * 构造,传递二个已经进行base64_encode的KEY与IV     *     * @param string $key     * @param string $iv     */    function __construct ($key, $iv)    {        if (empty($key) || empty($iv)) {            echo 'key and iv is not valID';            exit();        }        $this->key = $key;        $this->iv = $iv;//8        //$this->iv = $iv.'00000000000';//16    }    /**     * @Title 加密     * @author gaowei     * @date 2017/12/18     * @param string $value 要传的参数     * @ //OPENSSL_RAW_DATA|OPENSSL_ZERO_@R_419_5095@ //AES-128-ECB|AES-256-CBC|BF-CBC     * @return Json     * */    public function encrypt ($value) {        //参考地址:https://stackoverflow.com/questions/41181905/PHP-mcrypt-encrypt-to-openssl-encrypt-and-openssl-zero-@R_419_5095@-problems#        $value = $this->@R_419_5095@PKCS7($value);        $key = base64_decode($this->key);        $iv  = base64_decode($this->iv);        //AES-128-ECB|不能用 AES-256-CBC|16 AES-128-CBC|16 BF-CBC|8 aes-128-gcm|需要加$tag  DES-EDE3-CBC|8        $cipher = "DES-EDE3-CBC";        if (in_array($cipher, openssl_get_cipher_methods())) {            //$ivlen = openssl_cipher_iv_length($cipher);           // $iv = openssl_random_pseudo_bytes($ivlen);            $result = openssl_encrypt($value, $cipher, $key, OPENSSL_SSLV23_@R_419_5095@, $iv);            //$result = base64_encode($result); //为3的时间要用            //store $cipher, $iv, and $tag for decryption later           /* $original_plaintext = openssl_decrypt($result, $cipher, $key, OPENSSL_SSLV23_@R_419_5095@, $iv);            echo $original_plaintext."\n";*/        }        return $result;    }    /**     * @Title 解密     * @author gaowei     * @date 2017/12/18     * @param string $value 要传的参数     * @return Json     * */    public function decrypt ($value) {        $key       = base64_decode($this->key);        $iv        = base64_decode($this->iv);        $decrypted = openssl_decrypt($value, 'DES-EDE3-CBC', $key, OPENSSL_SSLV23_@R_419_5095@, $iv);        $ret = $this->Un@R_419_5095@PKCS7($decrypted);        return $ret;    }    private function @R_419_5095@PKCS7 ($data) {        //$block_size = mcrypt_get_block_size('tripledes', 'cbc');//获取长度        //$block_size = openssl_cipher_iv_length('tripledes', 'cbc');//获取长度        $block_size = 8;        $@R_419_5095@_char = $block_size - (strlen($data) % $block_size);        $data .= str_repeat(chr($@R_419_5095@_char), $@R_419_5095@_char);        return $data;    }    private function Un@R_419_5095@PKCS7($text) {        $pad = ord($text{strlen($text) - 1});        if ($pad > strlen($text)) {            return false;        }        if (strspn($text, chr($pad), strlen($text) - $pad) != $pad) {            return false;        }        return substr($text, 0, - 1 * $pad);    }}

3、openssl 加密,解密 [方式2]

/** * @desc:PHP aes加密解密类 * @author gl * @date 2019/08/31 */class CI_Aes{    /**     * CI_Aes cipher     * @var string     */    protected $cipher = 'aes-128-ecb';    /**     * CI_Aes key     *     * @var string     */    protected $key;    /**     * CI_Aes constructor     * @param string $key Configuration parameter     */    public function __construct($key=null){        $this->key = $key;    }    /**     * Initialize     *     * @param array $params Configuration parameters     * @return CI_Encryption     */    public function initialize($params)    {        if (!empty($params) && is_array($params)) {            foreach ($params as $key => $val) {                $this->$key = $val;            }        }    }    /**     * Encrypt     *     * @param string $data input data     * @return string     */    public function encrypt($data) {        $endata =  openssl_encrypt($data, $this->cipher, $this->key, OPENSSL_RAW_DATA);        return  bin2hex($endata);    }    /**     * Decrypt     *     * @param string $data Encrypted data     * @return string     */    public function decrypt($data) {        $encrypted = hex2bin($data);        return openssl_decrypt($encrypted, $this->cipher, $this->key, OPENSSL_RAW_DATA);    }}

4、其他 加密,解密

//加密函数function lock_url($txt,$key='www.jb51.net'){  $chars = "ABCDEFGHIJKLMnopQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-=+";  $nh = rand(0,64);  $ch = $chars[$nh];  $mdKey = md5($key.$ch);  $mdKey = substr($mdKey,$nh%8, $nh%8+7);  $txt = base64_encode($txt);  $tmp = '';  $i=0;$j=0;$k = 0;  for ($i=0; $i<strlen($txt); $i++) {    $k = $k == strlen($mdKey) ? 0 : $k;    $j = ($nh+strpos($chars,$txt[$i])+ord($mdKey[$k++]))%64;    $tmp .= $chars[$j];  }  return urlencode($ch.$tmp);}//解密函数function unlock_url($txt,$key='www.jb51.net'){  $txt = urldecode($txt);  $chars = "ABCDEFGHIJKLMnopQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-=+";  $ch = $txt[0];  $nh = strpos($chars,$ch);  $mdKey = md5($key.$ch);  $mdKey = substr($mdKey,$nh%8, $nh%8+7);  $txt = substr($txt,1);  $tmp = '';  $i=0;$j=0; $k = 0;  for ($i=0; $i<strlen($txt); $i++) {    $k = $k == strlen($mdKey) ? 0 : $k;    $j = strpos($chars,$txt[$i])-$nh - ord($mdKey[$k++]);    while ($j<0) $j+=64;    $tmp .= $chars[$j];  }  return base64_decode($tmp);}

相关教程推荐:《PHP教程》 总结

以上是内存溢出为你收集整理的PHP如何实现AES加密、解密?方法介绍(代码示例)全部内容,希望文章能够帮你解决PHP如何实现AES加密、解密?方法介绍(代码示例)所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1158285.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-01
下一篇 2022-06-01

发表评论

登录后才能评论

评论列表(0条)

保存