PHP RSA encryption Decryption use method _php instance

Source: Internet
Author: User
Tags begin rsa private key decrypt openssl openssl rsa php server asymmetric encryption

When the PHP server interacts with the client and provides an open API, it usually requires data encryption for the sensitive part of the API data transmission, when RSA asymmetric encryption can be used, and here is an example to illustrate how to encrypt and decrypt data using PHP.

1, encryption and decryption of the first step is to generate a public key, private key pair, the private key encrypted content can be decrypted through the public key (in turn can also)

Download the open source RSA key generation tool OpenSSL (usually the Linux system comes with the program), unzip it to a separate folder, go into the bin directory, and execute the following command:

OpenSSL genrsa-out Rsa_private_key.pem 1024 OpenSSL pkcs8-topk8-inform pem-in rsa_private_key.pem-outform pem-no
Crypt-out Private_key.pem
OpenSSL rsa-in rsa_private_key.pem-pubout-out Rsa_public_key.pem


The first command generates the original RSA private key file Rsa_private_key.pem, the second command converts the original RSA private key to the PKCS8 format, and the third generates the RSA public key Rsa_public_key.pem
It is seen from above that the corresponding public key can be generated by the private key, so we use the private key Private_key.pem on the server side, and the public key is distributed to the front end of Android and iOS

2, PHP Use the generated public key, the private key to encrypt and decrypt, directly on the code

<?php $private _key = '-----BEGIN RSA private Key-----miicxqibaakbgqc3//sr2txw0wrc2dysx8vnglqt3y7ldu9+ LBLI6E1KS5LFC5JL tgf7kbtskchbm3ouehwqp1zj85ije59af5gib2klbd6h4wrbbha2xe1sq21ykja/gqx7/iria3zqfxgv/qekygox+
Xalvoolzqdwh76o2n1vp1d+td3amhsk7qidaqab Aogbakh14bmitesqd4pywodwmy7rrrvyfpenjjtecljvkb7ikrvxvdkp1xijngkh 2H5SYHQ5QSLPSGYJ1M/XKDNGINWALVHVD3BOKKGKG1BZN7AO5PXT+HERQXAVWWS6 ga63yvsic8jcodxiuvxjnumqrlaqof6aub/ 2VWC2T5MDMXLHAKEA3PWGPVXGLIWL 3h7qlyzlrlrbfrurn4cyl4uyaakokkavzly04glle8ycgoc2dzl4eil4l/+x/gaq deJU/
chlrqjbanozy0meovkwhu4bscsdnfm6usqowybewhyyh/otv1a3sqcce1f+ qbaclcqenihajccdmgyj53lfigyv0wcs54kcqaxapkahclrkqladquv5iwyyj25f oiq+y8sgccs73qixru1ypjy9yka/meg9smsl4oh9ioigi+
Zuygh9ydsmeq0cqqc2 4g3ip2g3lndrdzim5nz7pfnmyrabxk/ugvuwdk47iwtzhfkdhxkfc8qepuhbsahl Qjifgxy4ejkubm3fpdgjakafwuxyssijjvrhwnhfbg0rfkvvy63osmnrxil4x6ey yi9lblcsyfpl25l7l5zmjrahn45zaioobrwqpm5edu7c--

---end RSA PRIVATE KEY-----'; $public _key = '-----BEGIN public Key-----migfma0gcsqgsib3dqebaquaa4gnadcbiqkbgqc3//SR2TXW0WRC2DYSX8VNGLQT 3y7ldu9+lbli6e1ks5lfc5jltgf7kbtskchbm3ouehwqp1zj85ije59af5gib2kl Bd6h4wrbbHA2XE1sq21ykja/

gqx7/iria3zqfxgv/qekygox+xalvoolzqdwh76o 2n1vp1d+td3amhsk7qidaqab-----End public KEY-----';
echo $private _key; $pi _key = openssl_pkey_get_private ($private _key);//This function can be used to determine whether the private key is available, the available return resource ID Resource ID $pu _key = openssl_pkey_get_
Public ($public _key);//This function can be used to determine whether the key is available Print_r ($pi _key); echo "\ n";


Print_r ($pu _key), echo "\ n"; 
$data = "AASSSSASSSDDD";//Original Data $encrypted = ""; 

$decrypted = "";

echo "Source data:", $data, "\ n";

echo "Private key encrypt:\n"; Openssl_private_encrypt ($data, $encrypted, $pi _key);//private key Encryption $encrypted = Base64_encode ($encrypted);//encrypted content usually contains special characters,

Need to encode the conversion, in the network transmission through the URL to pay attention to whether the Base64 encoding is URL-safe echo $encrypted, "\ n";

echo "Public key decrypt:\n";

Openssl_public_decrypt (Base64_decode ($encrypted), $decrypted, $pu _key);//private key encrypted content can be decrypted through the public key echo $decrypted, "\ n";
echo "---------------------------------------\ n";

echo "Public key encrypt:\n"; Openssl_public_encrypt ($Data, $encrypted, $pu _key);//public key Encryption $encrypted = Base64_encode ($encrypted);

echo $encrypted, "\ n";
echo "Private key decrypt:\n";
 Openssl_private_decrypt (Base64_decode ($encrypted), $decrypted, $pi _key);//private key decryption echo $decrypted, "\ n";

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: info-contact@alibabacloud.com 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.