您好,欢迎访问一九零五行业门户网

PHP7中如何使用“DES-EDE-CBC”加解密

本篇文章给大家介绍一下php7中使用“des-ede-cbc”加解密的方法。有一定的参考价值,有需要的朋友可以参考一下,希望对大家有所帮助。
1. 条件约束之前php5上常使用的mcrypt库在php7.1+上已经被移除,故我们采用openssl对数据进行加解密。
加密方式采用des-ede-cbc方式。
密钥填充方式为:采用24位密钥,先将key进行md5校验取值,得出16位字串,再取key md5校验值前8位追加到先前的取值后面。由此组装出24位的密钥。
2. 代码分享<?phpclass desedecbc {private $cipher, $key, $iv;/** * desedecbc constructor. * @param $cipher * @param $key * @param $iv */public function __construct($cipher, $key, $iv) {$this->cipher = $cipher;$this->key= $this->getformatkey($key);$this->iv = $iv;}/** * @func 加密 * @param $msg * @return string */public function encrypt($msg) {$des = @openssl_encrypt($msg, $this->cipher, $this->key, openssl_raw_data, $this->iv);return base64_encode($des);}/** * @func 解密 * @param $msg * @return string */public function decrypt($msg) {return @openssl_decrypt(base64_decode($msg), $this->cipher, $this->key, openssl_raw_data, $this->iv);}/** * @func 生成24位长度的key * @param $skey * @return bool|string */private function getformatkey($skey) {$md5value= md5($skey);$md5valuelen = strlen($md5value);$key = $md5value . substr($md5value, 0, $md5valuelen / 2);return hex2bin($key);}}$cipher = 'des-ede-cbc';$msg = 'helloworld';$key = '12345678';$iv = "\x00\x00\x00\x00\x00\x00\x00\x00";$des = new desedecbc($cipher, $key, $iv);// 加密$msg = $des->encrypt($msg);echo '加密后: ' . $msg . php_eol;// 解密$src = $des->decrypt($msg);echo '解密后: ' . $src . php_eol;
3. 一点说明可以根据实际情况调整加密方式、key的填充方式、及iv向量来满足不同的需求。
推荐学习:php视频教程
以上就是php7中如何使用“des-ede-cbc”加解密的详细内容。
其它类似信息

推荐信息