Home > Article > Backend Development > Sorting out OpenSSL encryption issues in PHP_php tips
This article mainly explains to you the problems encountered in OpenSSL encryption in PHP and related solutions. Encryption is very common in actual PHP development. It is very important for If you are interested in encryption in PHP, you can refer to it and learn together.
Recently, in the company's projects, it is necessary to use OpenSSL encryption and Java side for interface verification. When the test environment is upgraded to PHP7, encryption errors will occur. Later, after multiple inspections, the reason was finally found. Location: In the
PHP7 environment, replace the openssl_get_privatekey method with openssl_pkey_get_private
You need to convert the secret key. The key format is different in the window environment and the Linux environment. (Not sure if it’s related to the operating system for the time being)
PHP’s key verification requires adding a header and a tail.
Paste the attached method here
Method to convert the secret key format:
function transJavaRsaKeyToPhpOpenSSL($content) { if ($content) { return trim(chunk_split($content, 64, "\n")); } return false; }
How to add header and tail:
function appendFlags($content, $isPublic = true) { if ($isPublic) { return "-----BEGIN PUBLIC KEY-----\n" . $content . "\n-----END PUBLIC KEY-----\n"; } else { return "-----BEGIN PRIVATE KEY-----\n" . $content . "\n-----END PRIVATE KEY-----\n"; } }
Related recommendations:
Detailed explanation of how to use OpenSSL instead of Mcrypt encryption and decryption in PHP 7.1_php tips
Detailed explanation of how PHP uses openssl encryption and decryption examples
php encryption and decryption method based on openssl
The above is the detailed content of Sorting out OpenSSL encryption issues in PHP_php tips. For more information, please follow other related articles on the PHP Chinese website!