Sorting out OpenSSL encryption issues in PHP_php tips

韦小宝
Release: 2023-03-17 22:42:01
Original
1824 people have browsed it

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;
  }
Copy after login


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";
    }
  }
Copy after login


##The above is my little experience when encountering OPENSSL encryption problem this time, everyone If you still have questions, leave a message below to discuss.

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!

Related labels:
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