PHP RSA encryption and decryption instance

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

When the PHP server interacts with the client and provides an open API, it is often necessary to encrypt the sensitive part of the API data transfer, at which point RSA Asymmetric encryption can be used, and an example to illustrate how to use PHP to encrypt and decrypt data.

1, the first step of encryption and decryption 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 to a separate folder, into the bin directory, Execute the following command: OpenSSL genrsa-out rsa_private_key.pem 1024openssl pkcs8-topk8-inform pem-in rsa_private_key.pem-outform PEM- Nocrypt-out Private_key.pemopenssl rsa-in rsa_private_key.pem-pubout-out RSA_PUBLIC_KEY.PEM First command to generate 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 from the above to generate the corresponding public key through the private key, so we will private_ the private key Key.pem used on the server side, the public key issued to Android and iOS and other front-end

2, PHP with the generated public key, the private key for encryption and decryption, directly on the code

1<?PHP2 $private _key=-----BEGIN RSA PRIVATE KEY-----3 MIICXQIBAAKBGQC3//SR2TXW0WRC2DYSX8VNGLQT3Y7LDU9+LBLI6E1KS5LFC5JL4 tgf7kbtskchbm3ouehwqp1zj85ije59af5gib2klbd6h4wrbbha2xe1sq21ykja/5 Gqx7/iria3zqfxgv/qekygox+xalvoolzqdwh76o2n1vp1d+td3amhsk7qidaqab6 Aogbakh14bmitesqd4pywodwmy7rrrvyfpenjjtecljvkb7ikrvxvdkp1xijngkh7 2H5SYHQ5QSLPSGYJ1M/XKDNGINWALVHVD3BOKKGKG1BZN7AO5PXT+HERQXAVWWS68 GA63YVSIC8JCODXIUVXJNUMQRLAQOF6AUB/2VWC2T5MDMXLHAKEA3PWGPVXGLIWL9 3h7qlyzlrlrbfrurn4cyl4uyaakokkavzly04glle8ycgoc2dzl4eil4l/+x/gaqTen deju/chlrqjbanozy0meovkwhu4bscsdnfm6usqowybewhyyh/otv1a3sqcce1f+ One qbaclcqenihajccdmgyj53lfigyv0wcs54kcqaxapkahclrkqladquv5iwyyj25f A Oiq+y8sgccs73qixru1ypjy9yka/meg9smsl4oh9ioigi+zuygh9ydsmeq0cqqc2 - 4g3ip2g3lndrdzim5nz7pfnmyrabxk/ugvuwdk47iwtzhfkdhxkfc8qepuhbsahl - Qjifgxy4ejkubm3fpdgjakafwuxyssijjvrhwnhfbg0rfkvvy63osmnrxil4x6ey the yi9lblcsyfpl25l7l5zmjrahn45zaioobrwqpm5edu7c ------END RSA PRIVATE KEY-----'; -  - $public _key=-----BEGIN Public KEY----- + MIGFMA0GCSQGSIB3DQEBAQUAA4GNADCBIQKBGQC3//SR2TXW0WRC2DYSX8VNGLQT - 3y7ldu9+lbli6e1ks5lfc5jltgf7kbtskchbm3ouehwqp1zj85ije59af5gib2kl + bd6h4wrbbha2xe1sq21ykja/gqx7/iria3zqfxgv/qekygox+xalvoolzqdwh76o A 2n1vp1d+td3amhsk7qidaqab at-----END Public KEY-----'; -  - //echo $private _key; - $pi _key= Openssl_pkey_get_private ($private _key);//This function can be used to determine if the private key is available, and can be used to return the resource ID Resource ID - $pu _key= Openssl_pkey_get_public ($public _key);//This function can be used to determine if the public key is available - Print_r($pi _key);Echo"\ n"; in Print_r($pu _key);Echo"\ n"; -  to  + $data= "AASSSSASSSDDD";//Raw Data - $encrypted= "";  the $decrypted= "";  *  $ Echo"Source data:",$data, "\ n";Panax Notoginseng  - Echo"Private Key encrypt:\n"; the  +Openssl_private_encrypt ($data,$encrypted,$pi _key);//private key Encryption A $encrypted=Base64_encode($encrypted);//The encrypted content usually contains special characters that need to be encoded for conversion and to be aware that the Base64 encoding is URL-safe when transmitted over a network through a URL. the Echo $encrypted, "\ n"; +  - Echo"Public Key decrypt:\n"; $  $Openssl_public_decrypt (Base64_decode($encrypted),$decrypted,$pu _key);//private key encrypted content can be decrypted by public key - Echo $decrypted, "\ n"; -  the Echo"---------------------------------------\ n"; - Echo"Public Key encrypt:\n";Wuyi  theOpenssl_public_encrypt ($data,$encrypted,$pu _key);//Public Key Cryptography - $encrypted=Base64_encode($encrypted); Wu Echo $encrypted, "\ n"; -  About Echo"Private Key decrypt:\n"; $Openssl_private_decrypt (Base64_decode($encrypted),$decrypted,$pi _key);//private Key Decryption - Echo $decrypted, "\ n";

PHP RSA encryption and decryption instance

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.