Detailed explanation of PHP7 OpenSSL DES-EDE-CBC encryption and decryption

coldplay.xixi
Release: 2023-02-17 21:30:02
forward
1625 people have browsed it

Detailed explanation of PHP7 OpenSSL DES-EDE-CBC encryption and decryption

1. Conditional constraints

The mcrypt library that was commonly used on PHP5 has been moved on PHP7.1 In addition, we use openssl to encrypt and decrypt the data.

The encryption method adoptsDES-EDE-CBCmethod.

The key filling method is: using a 24-bit key, first perform MD5 verification on the key to obtain a 16-bit string, and then take the first 8 digits of the key MD5 verification value and append them to the previous value. value behind. A 24-bit key is assembled from this.

Recommended (free):PHP7

2. Code sharing

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;
Copy after login

3. A little explanation

The encryption method, key filling method, and iv vector can be adjusted according to the actual situation to meet different needs.

The above is the detailed content of Detailed explanation of PHP7 OpenSSL DES-EDE-CBC encryption and decryption. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:csdn.net
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
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!