Solving the encryption problem of OpenSSL in PHP

Source: Internet
Author: User
This article mainly introduces the resolution of the encryption problem of OpenSSL in PHP, has a certain reference value, now share to everyone, the need for friends can refer to

Recent company projects need to use OpenSSL encryption and Java side for interface verification, and then test the environment to upgrade to PHP7 when the encryption will be error, and then the multi-faceted check finally found the reason:

    1. PHP7 in the environment, openssl_get_privatekey replace the methodopenssl_pkey_get_private

    2. You need to convert the key, the window environment and the Linux environment of the key format is different (temporarily unsure whether it is the relationship of the operating system)

    3. PHP's key verification needs to be added to the tail.

It's attached to the method.
To convert the secret key format:

function Transjavarsakeytophpopenssl ($content) {        if ($content) {            return trim (Chunk_split ($content, @, "\ n")) ;        }        return false;    }

How to add and tail:

function Appendflags ($content, $isPublic = True) {        if ($isPublic) {            return "-----BEGIN public KEY-----\ n". $conte Nt. "\ n-----END public KEY-----\ n";        }        else {            return '-----BEGIN PRIVATE KEY-----\ n '. $content. "\ n-----END PRIVATE KEY-----\ n";}    }

The above is the whole content of this article, I hope that everyone's learning has helped, more relevant content please pay attention to!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.