Home  >  Article  >  Backend Development  >  Solving the encryption problem of OpenSSL in PHP

Solving the encryption problem of OpenSSL in PHP

不言
不言Original
2018-07-11 16:37:331594browse

This article mainly introduces the solution to the encryption problem of OpenSSL in PHP. It has a certain reference value. Now I share it with you. Friends in need can refer to it.

There is a need for it in recent company projects. Go to the encryption and java side of OpenSSL to perform interface verification. When the test environment is upgraded to PHP7, an encryption error will occur. Later, after multiple inspections, the cause was finally found:

  1. # in the PHP7 environment. ##openssl_get_privatekeyThe method is changed to openssl_pkey_get_private

  2. You need to convert the secret key. There is a difference between the key format in the window environment and the Linux environment (not sure yet) Is it related to the operating system?)

  3. PHP’s key verification needs to add the header and tail.

Post 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;
    }
Method 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";
        }
    }
The above is this article The entire content, I hope it will be helpful to everyone's study. For more related content, please pay attention to the PHP Chinese website!

Related recommendations:

The above is the detailed content of Solving the encryption problem of OpenSSL in PHP. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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