This article describes how to use openssl to implement rsa asymmetric encryption algorithms.
The Code is as follows: <? Php/*** use openssl for asymmetric encryption * @ since 2010-07-08 */class Rsa {/*** private key */private $ _ privKey; /*** public key */private $ _ pubKey;/*** the keys saving path */private $ _ keyPath;/*** the construtor, the param $ path is the keys saving path */public function _ construct ($ path) {if (empty ($ path) |! Is_dir ($ path) {throw new Exception ('must set the keys save path');} $ this-> _ 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. DIRECTORY_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 = opens Sl_pkey_get_private ($ prk); return true;}/*** setup the public key */public 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 required; // Private key Encryption and Public key decryption echo 'source: I am an old man <br/> '; $ pre = $ rsa-> privEn Crypt ('I'm a newbie'); echo 'private encrypted: <br/> '. $ pre. '<br/>'; $ pud = $ rsa-> pubDecrypt ($ pre); echo 'public decrypted :'. $ pud. '<br/>'; // public key encryption, Private Key decryption echo 'source: dry IT <br/> '; $ pue = $ rsa-> pubEncrypt ('dry IT '); echo 'public encrypt: <br/> '. $ pue. '<br/>'; $ prd = $ rsa-> privDecrypt ($ pue); echo 'private decrypt :'. $ prd;?> Note that apache must support OpenSSL