This article mainly introduces the method of using the generated public key and private key for encryption and decryption in PHP. Interested friends can refer to it. I hope it will be helpful to everyone.
When the PHP server interacts with the client and provides an open API, it is usually necessary to encrypt sensitive part of the API data transmission. At this time, rsa asymmetric encryption can come in handy. Here is an example. Explain how to use PHP to implement data encryption and decryption
1. The first step in encryption and decryption is to generate a public key and private key pair. The content encrypted by the private key can be decrypted by the public key (and vice versa)
Download the open source RSA key generation tool openssl (usually Linux systems come with this program), unzip it to a separate folder, enter the bin directory, and execute the following command:
1 2 3 |
|
The first command generates the original RSA private key file rsa_private_key.pem, the second command converts the original RSA private key to pkcs8 format, and the third command generates the RSA public key rsa_public_key.pem
It can be seen from the above that the private key is passed The corresponding public key can be generated, so we use the private key private_key.pem on the server side, and the public key is distributed to front-ends such as android and ios
2. Use the generated public key and private key in php for encryption and decryption , go directly to the code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
|
Summary: The above is the entire content of this article, I hope it will be helpful to everyone's study.
Related recommendations:
PHP implements the function of capturing pictures from online albums with anti-leeching settings
The above is the detailed content of How to use the generated public key and private key for encryption and decryption in PHP. For more information, please follow other related articles on the PHP Chinese website!