An example of RSA asymmetric encryption algorithm using OpenSSL

Source: Internet
Author: User
Tags decrypt openssl asymmetric encryption

  This article mainly introduces the use of OpenSSL to implement RSA asymmetric encryption algorithm example, we refer to the use of the bar

  Code as follows: <?php/**  * uses OpenSSL to implement asymmetric encryption  * @since 2010-07-08  */class Rsa {   /**      * private key      */        private $_privkey;          /**          * public key          */         private $_pubkey;          /**          * the keys saving path       &NBSP ;  */        private $_keypath;          /**          * the construtor,the param $path is the keys Savi Ng path          */        Public function __construct ($path)         {                if (empty ($path) | |!is_dir ($path)) {                      &NBsp throw new Exception (' must set the keys save Path ');                                   $ This->_keypath = $path;        }          /**          * Create the key PA Ir,save the key to $this->_keypath          */        Public Function cre Atekey ()         {                $r = Openssl_pkey_new (); &n Bsp               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 ';           &nbsp     $PRK = file_get_contents ($file);                 $this->_privkey = openssl_pkey_get_private ($PRK);                 return true;        }          /**          * setup the public Key          */        public Function Setuppubkey ()       &N Bsp {                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)   &NB Sp     {       ,         if (!is_string ($data)) {                        return null;                                   $ This->setupprivkey ();                   $r = Openssl_private_encrypt ($data, $encrypted, $this->_ Privkey);                 if ($r) {                &N Bsp       Return base64_eNcode ($encrypted);                                 return n ull;        }          /**          * decrypt with the Private key          */        Public Function Privdecrypt ($encrypted) &nbsp ;       {                if (!is_string ($encrypted)) {    & nbsp                   return null;                                   $ This->setupprivkey ();                   $encrypted = Base64_decode ($encrypted);                   $r = Openssl_private_decrypt ($encrypted, $decrypted, $this- >_privkey);     &NBSp           if ($r) {                      &N Bsp return $decrypted;                                 return n ull;        }          /**          * encrypt with publ IC key          */        Public Function Pubencrypt ($data)     &N Bsp   {                if (!is_string ($data)) {                        return null;                                   $ This->setuppubkey ();                   $r = Openssl_public_encrypt ($data, $encrypted, $this->_p Ubkey);                 if ($r) {                    & nbsp   return Base64_encode ($encrypted);                                 return n ull;        }          /**          * decrypt with the Public key          */        Public Function Pubdecrypt ($crypted)   &N Bsp     (                if (!is_string ($crypted)) {      &NB Sp                 return null;                                   $ This->setuppubkey ();                   $crypted = Base64_decode ($crypted);     &nbsp             $r = Openssl_public_decrypt ($crypted, $decrypted, $this->_pubkey);                 if ($r) {                &N Bsp       return $decrypted;                                 return n ull;        }           public Function __destruct ()         {                @ fclose ($this->_privkey);                 @ fclose ($this->_pubkey);        }  / //Below is a simple test demo, please delete $rsa = new RSA (' Ssl-key ') if not required;  //private key encryption, public key decryption echo ' Source: I am old turtle <br/> '; $pre = $rsa->privencrypt (' I am old turtle '); 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?>     need to be aware that Apache supports OpenSSL  
Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.