PHP uses asymmetric encryption algorithm (RSA)

Source: Internet
Author: User
Tags begin rsa private key
PHP uses asymmetric encryption algorithm (RSA)

Explanation

Asymmetric Encryption algorithms require two keys: public key and private key ). A public key is a pair of private keys. if a public key is used to encrypt data, only the corresponding private key can be used for decryption. if a private key is used to encrypt data, only the corresponding public key can be decrypted. Because encryption and decryption use two different keys, this algorithm is called asymmetric encryption algorithm.

Use cases

PHP writes an API for the client (Android, Ios) to decrypt the data.

Create private key and public key

Openssl genrsa-out rsa_private_key.pem 1024 // Generate the original RSA private key file openssl pkcs8-topk8-inform PEM-in encrypted-outform PEM-nocrypt-out private_key.pem // convert the original RSA private key open SSL rsa-in rsa_private_key.pem-pubout-out rsa_public_key.pem // Generate the RSA public key // use the private key rsa_private_key.pem on the server, the public key is issued to android, ios, and other front ends.

Server Class Library

Class Rsa {private static $ PRIVATE_KEY = '----- begin rsa private key ----- encrypt/Fd9IKKMc/Upper + upper/lower/XF5CiLr/aci/Upper + Lower + xFoNInwIDAQABAoGAe + ape7msdo + Lower/lower /export shpzrt4l7 + rSrgMOauWNjSVjr4T4Z168uvsnNocn + 3GWfzbBPQj3Phj E64R/release + yPmvwTs8AENm0wxi/V6loEXPBPxX2R4NjSG + ExYzA7/daDq // release/2738 QlwNqjFnmEjLUaak + Release Export/cxMMCbfxFH4WvhowqoC1iAKDyZ7HF7V + RcxcfuhoBJi/3 + ImEg ==----- end rsa private key -----'; private static $ PUBLIC_KEY = '----- begin public key ----- users/Fd9IKKMc/users + users/RilEbBMdCf55cUzNsfn/XF5CiLr/aci/OHuTe6ULvXs280T5M + users + xFoNInwIDAQAB ----- EN D public key ----- ';/*** get private KEY * @ return bool | resource */private static function getPrivateKey () {$ privKey = self: $ PRIVATE_KEY; return openssl_pkey_get_private ($ privKey);}/*** obtain the public key * @ return bool | resource */private static function getPublicKey () {$ publicKey = self: $ PUBLIC_KEY; return openssl_pkey_get_public ($ publicKey);}/*** private key encryption * @ param string $ data * @ return null | string */public st Atic function privEncrypt ($ data = '') {if (! Is_string ($ data) {return null;} return openssl_private_encrypt ($ data, $ encrypted, self: getPrivateKey ())? Base64_encode ($ encrypted): null;}/*** public key encryption * @ param string $ data * @ return null | string */public static function publicEncrypt ($ data = '') {if (! Is_string ($ data) {return null;} return openssl_public_encrypt ($ data, $ encrypted, self: getPublicKey ())? Base64_encode ($ encrypted): null;}/*** private key decryption * @ param string $ encrypted * @ return null */public static function privDecrypt ($ encrypted = '') {if (! Is_string ($ encrypted) {return null;} return (openssl_private_decrypt (base64_decode ($ encrypted), $ decrypted, self: getPrivateKey ()))? $ Decrypted: null;}/*** public key decryption * @ param string $ encrypted * @ return null */public static function publicDecrypt ($ encrypted = '') {if (! Is_string ($ encrypted) {return null;} return (openssl_public_decrypt (base64_decode ($ encrypted), $ decrypted, self: getPublicKey ()))? $ Decrypted: null ;}}

Server usage

Require_once "Rsa. php "; $ rsa = new Rsa (); $ data ['name'] = 'Tom '; $ data ['age'] = '20 '; $ privEncrypt = $ rsa-> privEncrypt (json_encode ($ data); echo 'after private key encryption :'. $ privEncrypt.'
'; $ PublicDecrypt = $ rsa-> publicDecrypt ($ privEncrypt); echo 'after public key decryption:'. $ publicDecrypt .'
'; $ PublicEncrypt = $ rsa-> publicEncrypt (json_encode ($ data); echo 'after public key encryption:'. $ publicEncrypt .'
'; $ PrivDecrypt = $ rsa-> privDecrypt ($ publicEncrypt); echo 'after private key decryption:'. $ privDecrypt .'
';

Thanks ~

What is a good API design?

Related Article

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.