search
HomeBackend DevelopmentPHP7Detailed explanation of the method of converting mcrypt to openssl in php7.2

mcrypt has been deprecated in php7.2.

Replace it with openssl.

For example

public function desEncrypt($str,$key) {
     $iv = $key;
         $size = mcrypt_get_block_size ( MCRYPT_DES, MCRYPT_MODE_CBC );
         $str = $this->_pkcs5_pad ( $str, $size );
         return strtoupper( bin2hex( mcrypt_encrypt(MCRYPT_DES, $key, $str, MCRYPT_MODE_CBC, $iv ) ) );
     }
     
public function desDecrypt($str,$key) {
     $iv = $key;
         $strBin = $this->_hex2bin( strtolower( $str ) );
         $str = mcrypt_decrypt( MCRYPT_DES, $key, $strBin, MCRYPT_MODE_CBC, $iv );
         $str = $this->_pkcs5_unpad( $str );
         return $str;
     }
 
private function _pkcs5_pad($text,$block=8){
         $pad = $block - (strlen($text) % $block);
         return $text . str_repeat(chr($pad), $pad);
     }
     
private function _pkcs5_unpad($text) {
        $pad = ord($text{strlen($text)-1});
        if ($pad > strlen($text)) return $text;
        if (strspn($text, chr($pad), strlen($text) - $pad) != $pad) return $text;
        return substr($text, 0, -1 * $pad);
     }

After replacement:

//要改的加密
    public function desEncrypt($str,$key) {
      //   $b = openssl_get_cipher_methods();
      //   echo &#39;<pre class="brush:php;toolbar:false">&#39;;
      //   print_r($b);
     $iv = $key;
      //    $size = mcrypt_get_block_size ( MCRYPT_DES, MCRYPT_MODE_CBC );
      //    var_dump($size);exit;
      //    $str = $this->_pkcs5_pad ( $str, $size );
 
         // return strtoupper( bin2hex( mcrypt_encrypt(MCRYPT_DES, $key, $str, MCRYPT_MODE_CBC, $iv ) ) );
        $data = openssl_encrypt($str,"DES-CBC",$key,OPENSSL_RAW_DATA,$iv);
        $data = strtolower(bin2hex($data));
        return $data;
     }
     
    //要改的解密
    public function desDecrypt($str,$key) {
     $iv = $key;
      //    $strBin = $this->_hex2bin( strtolower( $str ) );
      //    $str = mcrypt_decrypt( MCRYPT_DES, $key, $strBin, MCRYPT_MODE_CBC, $iv );
      //    $str = $this->_pkcs5_unpad( $str );
      //    return $str;
        return openssl_decrypt (hex2bin($str), &#39;DES-CBC&#39;, $key, OPENSSL_RAW_DATA,$iv);
     }

These are available online. In the process of using it, the author cannot know which openssl corresponds because he uses MCRYPT_DES. Encryption method, so I found openssl_get_cipher_methods() through the manual; this method can find all the methods supported by openssl and replace them.

echo &#39;<pre class="brush:php;toolbar:false">&#39;;
$a = openssl_get_cipher_methods();
print_r($a);
 
Array
(
    [0] => AES-128-CBC
    [1] => AES-128-CFB
    [2] => AES-128-CFB1
    [3] => AES-128-CFB8
    [4] => AES-128-CTR
    [5] => AES-128-ECB
    [6] => AES-128-OFB
    [7] => AES-128-XTS
    [8] => AES-192-CBC
    [9] => AES-192-CFB
    [10] => AES-192-CFB1
    [11] => AES-192-CFB8
    [12] => AES-192-CTR
    [13] => AES-192-ECB
    [14] => AES-192-OFB
    [15] => AES-256-CBC
    [16] => AES-256-CFB
    [17] => AES-256-CFB1
    [18] => AES-256-CFB8
    [19] => AES-256-CTR
    [20] => AES-256-ECB
    [21] => AES-256-OFB
    [22] => AES-256-XTS
    [23] => BF-CBC
    [24] => BF-CFB
    [25] => BF-ECB
    [26] => BF-OFB
    [27] => CAMELLIA-128-CBC
    [28] => CAMELLIA-128-CFB
    [29] => CAMELLIA-128-CFB1
    [30] => CAMELLIA-128-CFB8
    [31] => CAMELLIA-128-ECB
    [32] => CAMELLIA-128-OFB
    [33] => CAMELLIA-192-CBC
    [34] => CAMELLIA-192-CFB
    [35] => CAMELLIA-192-CFB1
    [36] => CAMELLIA-192-CFB8
    [37] => CAMELLIA-192-ECB
    [38] => CAMELLIA-192-OFB
    [39] => CAMELLIA-256-CBC
    [40] => CAMELLIA-256-CFB
    [41] => CAMELLIA-256-CFB1
    [42] => CAMELLIA-256-CFB8
    [43] => CAMELLIA-256-ECB
    [44] => CAMELLIA-256-OFB
    [45] => CAST5-CBC
    [46] => CAST5-CFB
    [47] => CAST5-ECB
    [48] => CAST5-OFB
    [49] => DES-CBC
    [50] => DES-CFB
    [51] => DES-CFB1
    [52] => DES-CFB8
    [53] => DES-ECB
    [54] => DES-EDE
    [55] => DES-EDE-CBC
    [56] => DES-EDE-CFB
    [57] => DES-EDE-OFB
    [58] => DES-EDE3
    [59] => DES-EDE3-CBC
    [60] => DES-EDE3-CFB
    [61] => DES-EDE3-CFB1
    [62] => DES-EDE3-CFB8
    [63] => DES-EDE3-OFB
    [64] => DES-OFB
    [65] => DESX-CBC
    [66] => IDEA-CBC
    [67] => IDEA-CFB
    [68] => IDEA-ECB
    [69] => IDEA-OFB
    [70] => RC2-40-CBC
    [71] => RC2-64-CBC
    [72] => RC2-CBC
    [73] => RC2-CFB
    [74] => RC2-ECB
    [75] => RC2-OFB
    [76] => RC4
    [77] => RC4-40
    [78] => RC4-HMAC-MD5
    [79] => SEED-CBC
    [80] => SEED-CFB
    [81] => SEED-ECB
    [82] => SEED-OFB
    [83] => aes-128-cbc
    [84] => aes-128-cfb
    [85] => aes-128-cfb1
    [86] => aes-128-cfb8
    [87] => aes-128-ctr
    [88] => aes-128-ecb
    [89] => aes-128-gcm
    [90] => aes-128-ofb
    [91] => aes-128-xts
    [92] => aes-192-cbc
    [93] => aes-192-cfb
    [94] => aes-192-cfb1
    [95] => aes-192-cfb8
    [96] => aes-192-ctr
    [97] => aes-192-ecb
    [98] => aes-192-gcm
    [99] => aes-192-ofb
    [100] => aes-256-cbc
    [101] => aes-256-cfb
    [102] => aes-256-cfb1
    [103] => aes-256-cfb8
    [104] => aes-256-ctr
    [105] => aes-256-ecb
    [106] => aes-256-gcm
    [107] => aes-256-ofb
    [108] => aes-256-xts
    [109] => bf-cbc
    [110] => bf-cfb
    [111] => bf-ecb
    [112] => bf-ofb
    [113] => camellia-128-cbc
    [114] => camellia-128-cfb
    [115] => camellia-128-cfb1
    [116] => camellia-128-cfb8
    [117] => camellia-128-ecb
    [118] => camellia-128-ofb
    [119] => camellia-192-cbc
    [120] => camellia-192-cfb
    [121] => camellia-192-cfb1
    [122] => camellia-192-cfb8
    [123] => camellia-192-ecb
    [124] => camellia-192-ofb
    [125] => camellia-256-cbc
    [126] => camellia-256-cfb
    [127] => camellia-256-cfb1
    [128] => camellia-256-cfb8
    [129] => camellia-256-ecb
    [130] => camellia-256-ofb
    [131] => cast5-cbc
    [132] => cast5-cfb
    [133] => cast5-ecb
    [134] => cast5-ofb
    [135] => des-cbc
    [136] => des-cfb
    [137] => des-cfb1
    [138] => des-cfb8
    [139] => des-ecb
    [140] => des-ede
    [141] => des-ede-cbc
    [142] => des-ede-cfb
    [143] => des-ede-ofb
    [144] => des-ede3
    [145] => des-ede3-cbc
    [146] => des-ede3-cfb
    [147] => des-ede3-cfb1
    [148] => des-ede3-cfb8
    [149] => des-ede3-ofb
    [150] => des-ofb
    [151] => desx-cbc
    [152] => id-aes128-GCM
    [153] => id-aes192-GCM
    [154] => id-aes256-GCM
    [155] => idea-cbc
    [156] => idea-cfb
    [157] => idea-ecb
    [158] => idea-ofb
    [159] => rc2-40-cbc
    [160] => rc2-64-cbc
    [161] => rc2-cbc
    [162] => rc2-cfb
    [163] => rc2-ecb
    [164] => rc2-ofb
    [165] => rc4
    [166] => rc4-40
    [167] => rc4-hmac-md5
    [168] => seed-cbc
    [169] => seed-cfb
    [170] => seed-ecb
    [171] => seed-ofb
)

Hope it is useful to you.

For more related PHP7 articles, please visit: "PHP7" Tutorial

The above is the detailed content of Detailed explanation of the method of converting mcrypt to openssl in php7.2. 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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

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.

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),