Example of RSA asymmetric cryptographic algorithm using OpenSSL _php tutorial

Source: Internet
Author: User
Tags asymmetric encryption
This article mainly introduces the use of OpenSSL to implement the RSA asymmetric encryption algorithm example, please refer to the use of it

The code is as follows: _keypath = $path; }/** * Create the key pair,save the key to $this->_keypath */Public Function CreateKey () {$r = Openssl_pkey_new (); O Penssl_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;} $fil E = $this->_keypath. Directory_separator. ' Priv.key '; $PRK = file_get_contents ($file); $this->_privkey = openssl_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-& Gt;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 = Openssl_private_decrypt ($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->set Uppubkey (); $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, if you do not need to delete $rsa = new RSA (' Ssl-key '); Private key encryption, public key decryption echo ' Source: I'm an old turtle.
'; $pre = $rsa->privencrypt (' I am an old turtle '); 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;? > It's important to note that Apache supports OpenSSL

http://www.bkjia.com/PHPjc/727549.html www.bkjia.com true http://www.bkjia.com/PHPjc/727549.html techarticle This article mainly introduces the use of OpenSSL to implement the RSA asymmetric encryption algorithm example, we refer to the following code:? php/*** using 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.