The method of encrypting and decrypting using the generated public key and private key in PHP

Source: Internet
Author: User
Tags begin rsa private key php server asymmetric encryption
This article mainly introduces the use of generated public key in PHP, the private key to encrypt the decryption method, interested in the friend's reference, I hope to be helpful to everyone.

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, a private key pair, the private key encrypted content can be decrypted by 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, enter the Bin directory, and 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


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
From the above, we can generate the corresponding public key through the private key, so we use the private key Private_key.pem on the server side, the public key is issued to the Android and iOS and other front

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

<?php$private_key = '-----BEGIN RSA private Key-----miicxqibaakbgqc3//sr2txw0wrc2dysx8vnglqt3y7ldu9+ lbli6e1ks5lfc5jltgf7kbtskchbm3ouehwqp1zj85ije59af5gib2klbd6h4wrbbha2xe1sq21ykja/gqx7/iria3zqfxgv/qekygox+ Xalvoolzqdwh76o2n1vp1d+ td3amhsk7qidaqabaogbakh14bmitesqd4pywodwmy7rrrvyfpenjjtecljvkb7ikrvxvdkp1xijngkh2h5syhq5qslpsgyj1m/ xkdnginwalvhvd3bokkgkg1bzn7ao5pxt+herqxavwws6ga63yvsic8jcodxiuvxjnumqrlaqof6aub/ 2vwc2t5mdmxlhakea3pwgpvxgliwl3h7qlyzlrlrbfrurn4cyl4uyaakokkavzly04glle8ycgoc2dzl4eil4l/+x/gaqdeju/ chlrqjbanozy0meovkwhu4bscsdnfm6usqowybewhyyh/otv1a3sqcce1f+ qbaclcqenihajccdmgyj53lfigyv0wcs54kcqaxapkahclrkqladquv5iwyyj25foiq+y8sgccs73qixru1ypjy9yka/meg9smsl4oh9ioigi+ zuygh9ydsmeq0cqqc24g3ip2g3lndrdzim5nz7pfnmyrabxk/ Ugvuwdk47iwtzhfkdhxkfc8qepuhbsahlqjifgxy4ejkubm3fpdgjakafwuxyssijjvrhwnhfbg0rfkvvy63osmnrxil4x6eyyi9lblcsyfpl25l7l5zmjrah n45zaioobrwqpm5edu7c-----END RSA PRIVATE KEY-----'; $public _key = '-----BEGIN public KEY-----migfma0gcsqgsib3dqebaquaa4gnadcbiqkbgqc3//sr2txw0wrc2dysx8vnglqt3y7ldu9+lbli6e1ks5lfc5jltgf7kbtskchbm3ouehwqp1zj85ije59af5gib2klbd6h4wrbbha2xe1sq21ykja/gqx7/iria3zqfxgv/ Qekygox+xalvoolzqdwh76o2n1vp1d+td3amhsk7qidaqab-----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, return the resource id Resource id$pu_key = openssl_pkey_get_public ($public _key) The function can be used to determine if the public key is available Print_r ($pi _key), echo "\ n";p rint_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);//The encrypted content usually contains special characters that need to be encoded under conversion, to be aware that Base64 encoding is a URL-safe echo $ when transferring over a URL between networks encrypted, "\ n"; echo "public key decrypt:\n"; Openssl_public_decrypt (Base64_decode ($encrypted), $decrypted, $pu _key) ;//The contents of the private key encryption can be decrypted by the public key echo $decrypted, "\ n"; echo "---------------------------------------\ n"; echo "publicly 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";

Summary : The above is the entire content of this article, I hope to be able to help you learn.

Related recommendations:

PHP implementation of image watermark based on the dynamic adding function of color environment

The principle of the PHP event mechanism

PHP implementation for the set of anti-theft chain Network album image capture function

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.