Home  >  Article  >  Backend Development  >  Correct usage posture of encryption and decryption DES in php

Correct usage posture of encryption and decryption DES in php

藏色散人
藏色散人forward
2020-01-16 14:21:362789browse

Foreword: In daily development, we often encrypt key characters, maybe for security or for standardization. Today we will use DES encryption correctly

Task flow chart

Correct usage posture of encryption and decryption DES in php

Fragment 1

Just do it, I started writing des encryption code as follows

class DES
{
    var $key;
    var $iv; //偏移量
 
    function DES( $key, $iv=0 ) {
        //key长度8例如:1234abcd
        $this->key = $key;
        if( $iv == 0 ) {
            $this->iv = $key;
        } else {
            $this->iv = $iv; //mcrypt_create_iv ( mcrypt_get_block_size (MCRYPT_DES, MCRYPT_MODE_CBC), MCRYPT_DEV_RANDOM );
        }
    }
 
    function encrypt($str) {
        //加密,返回大写十六进制字符串
        $size = mcrypt_get_block_size ( MCRYPT_DES, MCRYPT_MODE_CBC );
        $str = $this->pkcs5Pad ( $str, $size );
        return strtoupper( bin2hex( mcrypt_cbc(MCRYPT_DES, $this->key, $str, MCRYPT_ENCRYPT, $this->iv ) ) );
    }
 
    function decrypt($str) {
        //解密
        $strBin = $this->hex2bin( strtolower( $str ) );
        $str = mcrypt_cbc( MCRYPT_DES, $this->key, $strBin, MCRYPT_DECRYPT, $this->iv );
        $str = $this->pkcs5Unpad( $str );
        return $str;
    }
 
    function hex2bin($hexData) {
        $binData = "";
        for($i = 0; $i < strlen ( $hexData ); $i += 2) {
            $binData .= chr ( hexdec ( substr ( $hexData, $i, 2 ) ) );
        }
        return $binData;
    }
 
    function pkcs5Pad($text, $blocksize) {
        $pad = $blocksize - (strlen ( $text ) % $blocksize);
        return $text . str_repeat ( chr ( $pad ), $pad );
    }
 
    function pkcs5Unpad($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 );
    }
}

Test code

<?php
header("Content-type: text/html; charset=utf-8");
error_reporting(0);
require "DES5.php";
// 秘钥
$key = &#39;MOxinrui&#39;;
$crypt = new DES5($key);
$str = &#39;podsmia&#39;;
echo "原字符是".$str.&#39;<br>&#39;;
$encrypt_str = $crypt->encrypt($str);
echo "加密后的字符是".$encrypt_str.&#39;<br>&#39;;
$decrypt_str= $crypt->decrypt($encrypt_str);
echo "解密后的字符是".$decrypt_str.&#39;<br>&#39;;

Effect

原字符是podsmia
加密后的字符是9490E64136137FD8
解密后的字符是podsmia

After a few days, I had nothing to do , continue to run the code

Deprecated: Methods with the same name as their class will not be constructors in a future version of PHP; DES5 has a deprecated constructor in D:\phpstudy_pro\WWW\des\ DES5.php on line 2

The original characters are podsmia

Fatal error: Uncaught Error: Call to undefined function mcrypt_get_block_size() in D:\phpstudy_pro\WWW\des \DES5.php:19 Stack trace: #0 D:\phpstudy_pro\WWW\des\1.php(10): DES5->encrypt('podsmia') #1 {main} thrown in D:\phpstudy_pro\WWW \des\DES5.php on line 19

reported fatalerror to me. This is unscientific. I didn’t move anything. What's going on? After some research later. It was found that it was a problem with the php version. I ran it successfully before because I was using php5.5, but this time I got an error and I was using php7.2. Then I need to find an alternative. . Next, watch my operation

Fragment 2

<?php
class DES7
{
    //要改的加密
    public function desEncrypt($str,$key) {
        $iv = $key;
        $data = openssl_encrypt($str,"DES-CBC",$key,OPENSSL_RAW_DATA,$iv);
        $data = strtolower(bin2hex($data));
        return $data;
    }
 
    //要改的解密
    public function desDecrypt($str,$key) {
        $iv = $key;
        return openssl_decrypt (hex2bin($str), &#39;DES-CBC&#39;, $key, OPENSSL_RAW_DATA,$iv);
    }
}

Test code

<?php
header("Content-type: text/html; charset=utf-8");
//error_reporting(0);
// 秘钥
require "DES7.php";
$key = &#39;MOxinrui&#39;;
$crypt = new DES7($key);
$str = &#39;问哪个&#39;;
echo "原字符是".$str.&#39;<br>&#39;;
$encrypt_str = $crypt->desEncrypt($str,$key);
echo "加密后的字符是".$encrypt_str.&#39;<br>&#39;;
$decrypt_str= $crypt->desDecrypt($encrypt_str,$key);
echo "解密后的字符是".$decrypt_str.&#39;<br>&#39;;

Effect

原字符是问哪个
加密后的字符是074b8beee21eefca7ec3a60cb8edda18
解密后的字符是问哪个

The problem is solved perfectly. If you encounter this kind of problem in the future, you can just copy my code, which is convenient and trouble-free.

For more PHP related knowledge, please visit PHP Tutorial!

The above is the detailed content of Correct usage posture of encryption and decryption DES in php. For more information, please follow other related articles on the PHP Chinese website!

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