The mcrypt php extension obsolete problem solving

php function clusters of mcrypt_ start deprecated in version 7.1.0 and 7.2.0 versions completely discarded. If the current project there is the latest version mcrypt_ encrypt this function and want to update php


The official recommended openssl_encrypt / openssl_decrypt alternative mcrypt_ the encrypt and mcrypt_ the decrypt


MCRYPT_RIJNDAEL_256 not AES-256, if you want to use mcrypt_ clusters to achieve AES-256, then you should use MCRYPT_RIJNDAEL_128 algorithm + 32-bit key, openssl_ cluster is more clearly defined the various modes. Here I compiled a correspondence relationship for your reference:

MCRYPT_RIJNDAEL_128 & CBC + 16位Key = openssl_encrypt(AES-128-CBC, 16位Key) = AES-128
MCRYPT_RIJNDAEL_128 & CBC + 24位Key = openssl_encrypt(AES-192-CBC, 24位Key) = AES-192
MCRYPT_RIJNDAEL_128 & CBC + 32位Key = openssl_encrypt(AES-256-CBC, 32位Key) = AES-256


We are here to write an example


mcrypt Code:

<?php

$key = 'keykeykey';

// Note that I use the 32-bit key
$key = md5(md5($key) . 'salt');

$iv = md5($key, true);

$data = 'asdfgh';

$encode = mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $key, $data, MCRYPT_MODE_CFB, $iv);

$encode = base64_encode($encode);

echo $encode . PHP_EOL;

// decryption processing

$decode = mcrypt_decrypt(MCRYPT_RIJNDAEL_128, $key, base64_decode($encode), MCRYPT_MODE_CFB, $iv);

echo $decode;

echo PHP_EOL;


openssl Code

$key = 'keykeykey';
$key = md5(md5($key) . 'salt');
$iv = md5($key, true);

$data = 'asdfgh';

$encode = openssl_encrypt($data, 'aes-256-cfb', $key, OPENSSL_RAW_DATA, $iv);

//完美替换$encode = mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $key, $data, MCRYPT_MODE_CFB, $iv);

$encode = base64_encode($encode);

echo $encode . '<br>';

// decryption

$decode = openssl_decrypt(base64_decode($encode), 'aes-256-cfb', $key, OPENSSL_RAW_DATA, $iv);

echo $decode;


Openssl focus is here, I am using aes-256-cfb (because there mcrypt using a 32-bit key, if it is a 16-bit key, please refer to the correspondence between the above)



Guess you like

Origin blog.51cto.com/chinalx1/2417580