Example of rsa asymmetric encryption algorithm using openssl _ PHP Tutorial

Source: Internet
Author: User
Tags asymmetric encryption
Use openssl to implement rsa asymmetric encryption algorithm example. This article describes how to use openssl to implement rsa asymmetric encryption algorithms. For more information, see the following code :? Php *** use openssl to implement asymmetric encryption * @ since2010-07-08 * This article mainly introduces the example of using openssl to implement rsa asymmetric encryption algorithm.

The code is as follows: _ KeyPath = $ path;}/*** create the key pair, save the key to $ this-> _ keyPath */public function createKey () {$ r = openssl_pkey_new (); openssl_pkey_export ($ r, $ privKey); file_put_contents ($ this-> _ keyPath. DIRECTORY_SEPARATOR. 'priv. key ', $ privKey); $ this-> _ privKey = openssl_pkey_get_public ($ privKey); $ rp = openssl_pkey_get_details ($ r); $ pubKey = $ rp ['key']; file_put_contents ($ this-> _ keyPath. DI RECTORY_SEPARATOR. 'pub. key', $ pubKey); $ this-> _ pubKey = openssl_pkey_get_public ($ pubKey);}/*** setup the private key */public function setupPrivKey () {if (is_resource ($ this-> _ privKey) {return true;} $ file = $ this-> _ keyPath. DIRECTORY_SEPARATOR. 'priv. key'; $ prk = file_get_contents ($ file); $ this-> _ privKey = openssl_pkey_get_private ($ prk); return true;}/*** setup the public key */publi C function setupPubKey () {if (is_resource ($ this-> _ pubKey) {return true;} $ file = $ this-> _ keyPath. DIRECTORY_SEPARATOR. 'pub. key'; $ puk = file_get_contents ($ file); $ this-> _ pubKey = openssl_pkey_get_public ($ puk); return true ;} /*** encrypt with the private key */public function privEncrypt ($ data) {if (! Is_string ($ data) {return null;} $ this-> setupPrivKey (); $ r = openssl_private_encrypt ($ data, $ encrypted, $ this-> _ privKey ); if ($ r) {return base64_encode ($ encrypted);} return null;}/*** decrypt with the private key */public function privDecrypt ($ encrypted) {if (! Is_string ($ encrypted) {return null;} $ this-> setupPrivKey (); $ encrypted = base64_decode ($ encrypted); $ r = Encrypt ($ encrypted, $ decrypted, $ this-> _ privKey); if ($ r) {return $ decrypted;} return null;}/*** encrypt with public key */public function pubEncrypt ($ data) {if (! Is_string ($ data) {return null;} $ this-> setupPubKey (); $ r = openssl_public_encrypt ($ data, $ encrypted, $ this-> _ pubKey ); if ($ r) {return base64_encode ($ encrypted);} return null;}/*** decrypt with the public key */public function pubDecrypt ($ crypted) {if (! Is_string ($ crypted) {return null;} $ this-> setupPubKey (); $ crypted = base64_decode ($ crypted); $ r = openssl_public_decrypt ($ crypted, $ decrypted, $ this-> _ pubKey); if ($ r) {return $ decrypted;} return null;} public function _ destruct () {@ fclose ($ this-> _ privKey); @ fclose ($ this-> _ pubKey) ;}// The following is a simple test demo, delete $ rsa = new Rsa ('SSL-key') if not needed; // private key encryption, public key decryption echo 'Source: I'm a regular hacker
'; $ Pre = $ rsa-> privEncrypt (' I am a newbie '); echo 'private encrypted:
'. $ Pre .'
'; $ Pud = $ rsa-> pubDecrypt ($ pre); echo 'public decrypted:'. $ pud .'
'; // Public key encryption, private key decryption echo 'Source: dry IT
'; $ Pue = $ rsa-> pubEncrypt ('dry IT'); echo 'public encrypt:
'. $ Pue .'
'; $ Prd = $ rsa-> privDecrypt ($ pue); echo 'private decrypt:'. $ prd;?> Note that apache must support OpenSSL

The pipeline code is as follows :? Php/*** use openssl for asymmetric encryption * @ since 2010-07-08 *...

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.