Home > Backend Development > PHP Tutorial > Looking for a replacement for the PHP7.1 encryption function mcrypt_module_open()

Looking for a replacement for the PHP7.1 encryption function mcrypt_module_open()

WBOY
Release: 2016-10-22 00:14:14
Original
1763 people have browsed it

I upgraded PHP to 7.1 today and found out that this function is not recommended to be used:
mcrypt_module_open() is deprecated

Are there any other options to replace it?

==============================

Because 7.1 has not been officially released, and the release document of 7.1 states that it is not recommended to use extensions like mcrypt in the future, so there is currently no solution to this problem. If there are students who encounter the same problem, please use version 5 or 7.0.

Reply content:

I upgraded PHP to 7.1 today and found out that this function is not recommended to be used:
mcrypt_module_open() is deprecated

Are there any other options to replace it?

==============================

Because 7.1 has not been officially released, and the release document of 7.1 states that it is not recommended to use extensions like mcrypt in the future, so there is currently no solution to this problem. If there are students who encounter the same problem, please use version 5 or 7.0.

I strongly disagree with @eechen’s statement, who said it’s not mentioned in the manual! ?

http://php.net/manual/zh/migr...

Quoting the original text of the manual:

<code>mcrypt 扩展已经过时了大约10年,并且用起来很复杂。因此它被废弃并且被 OpenSSL 所取代。 从PHP 7.2起它将被从核心代码中移除并且移到PECL中。</code>
Copy after login

In PHP 7.1, the mycrypt extension can also be found and used in the PHP installation package. It will be removed in PHP 7.2, but we can still download the source code from PECL, compile and install this extension.

However, it is best to follow PHP's suggestions and gradually replace the role of mcrypt through OpenSSL.

Asymmetric encryption can be used! Public key encryption, private key decryption.

The mcrypt extension has been deprecated in 7.1, and will be moved from built-in to PECL in 7.2.

If you need full compatibility, you can only ignore deprecated and use it;
If you just need a substitute with similar functions, the official recommendation is to use the OpenSSL extension.

Please refer to the official manual for details.

But there is no Warning about deprecated in the manual, and naturally there is no explanation of alternatives.
And when I run it with PHP 7.0, there is no such warning that it is not recommended.
Finally, you said that 7.1 has not been officially released yet, you Think of it as a bug.

Supplement:
Thank you for the reminder. The PHP manual gives an alternative on the 7.1 migration page, which is to use OpenSSL to replace MCrypt.
Let’s take a look at using OpenSSL to implement symmetric encryption AES and asymmetric encryption RSA.

<code>AES:
<?php
header('Content-Type: text/plain;charset=utf-8');
$data = 'phpbest';
$key = 'oScGU3fj8m/tDCyvsbEhwI91M1FcwvQqWuFpPoDHlFk='; //echo base64_encode(openssl_random_pseudo_bytes(32));
$iv = 'w2wJCnctEG09danPPI7SxQ=='; //echo base64_encode(openssl_random_pseudo_bytes(16));
echo '内容: '.$data."\n";

$encrypted = openssl_encrypt($data, 'aes-256-cbc', base64_decode($key), OPENSSL_RAW_DATA, base64_decode($iv));
echo '加密: '.base64_encode($encrypted)."\n";

$encrypted = base64_decode('To3QFfvGJNm84KbKG1PLzA==');
$decrypted = openssl_decrypt($encrypted, 'aes-256-cbc', base64_decode($key), OPENSSL_RAW_DATA, base64_decode($iv));
echo '解密: '.$decrypted."\n";
?>

RSA:
用openssl生成rsa密钥对(私钥/公钥):
openssl genrsa -out rsa_private_key.pem 1024
openssl rsa -pubout -in rsa_private_key.pem -out rsa_public_key.pem
<?php
header('Content-Type: text/plain;charset=utf-8');
$data = 'phpbest';
echo '原始内容: '.$data."\n";

openssl_public_encrypt($data, $encrypted, file_get_contents(dirname(__FILE__).'/rsa_public_key.pem'));
echo '公钥加密: '.base64_encode($encrypted)."\n";

$encrypted = base64_decode('nMD7Yrx37U5AZRpXukingESUNYiSUHWThekrmRA0oD0=');
openssl_private_decrypt($encrypted, $decrypted, file_get_contents(dirname(__FILE__).'/rsa_private_key.pem'));
echo '私钥解密: '.$decrypted."\n";
?></code>
Copy after login
Related labels:
php
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template