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