search
HomeBackend DevelopmentPHP7Detailed explanation of PHP7 OpenSSL DES-EDE-CBC encryption and decryption

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 adopts DES-EDE-CBC method.

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

<?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. 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!

Statement
This article is reproduced at:CSDN. If there is any infringement, please contact admin@php.cn delete

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Tools

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.