Using OpenSSL to implement the RSA asymmetric encryption algorithm example _php instance

Source: Internet
Author: User
Tags decrypt openssl asymmetric encryption

Copy Code code as follows:

<?php
/**
* Use OpenSSL to implement 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 = 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->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->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, if not required please delete
$rsa = new RSA (' Ssl-key ');

Private key encryption, public key decryption
Echo ' Source: I am the 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;
?>


The need to be aware of is that Apache supports OpenSSL

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.