PHP RSA encryption and decryption using method

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

PHP RSA encryption and decryption using method

This article mainly introduces the PHP RSA encryption and decryption use method, this article explained the generation public key, the private key and uses the generated public key, the private key to encrypt the decryption instance in the PHP, needs the friend to be possible to refer to under

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:

?

1 2 3 OpenSSL genrsa-out Rsa_private_key.pem 1024 OpenSSL pkcs8-topk8-inform pem-in rsa_private_key.pem-outform Pem-nocryp T-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

?

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 45 46 47 48 49 50 51 52 53 54 55 56 57-58 <?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";//raw 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); After the encrypted content usually contains special characters, need to encode the conversion, in the network through the URL transmission should pay attention to whether the Base64 code is the URL security 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 decrypts 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.