当前位置:首页 > PHP教程 > php高级应用 > 列表

PHP封装的非对称加密RSA算法示例

发布:smiling 来源: PHP粉丝网  添加日期:2021-09-20 15:52:33 浏览: 评论:0 

本文实例讲述了PHP封装的非对称加密RSA算法,分享给大家供大家参考,具体如下:

将php的openssl扩展中的非对称加密函数封装成一个Rsa类。

需要注意的是,在windows上,需要打开openssl的配置文件,请参照官方的openssl扩展安装文档。

在windows上安装openssl扩展

1、将php路径下的两个库文件libeay32.dll和ssleay32.dll复制到操作system32下

2、配置openssl配置文件的位置,在php的路径下,有文件extras/openssl/openssl.cnf,添加环境变量OPENSSL_CONF指向这个文件的全路径。如何添加环境变量请google搜索之。

3、在php.ini里添加一行extension=php_openssl.dll

使用的demo:

  1. //====================demo======================= 
  2. //以下是一个简单的测试demo,如果不需要请删除 
  3. $rsa = new Rsa('sslkey'); //sslkey为存放密钥的路径,将已有的密钥文件复制到该路径下,公钥名称为pub.key,私钥名称为priv.key 
  4. $rsa->createKey(); //创建一对密钥,如果密钥对已经存在,不需调用 
  5. //私钥加密,公钥解密 
  6. echo 'source:脚本之家<br />'; 
  7. $pre = $rsa->privEncrypt('脚本之家'); 
  8. echo 'private encrypted:<br />' . $pre . '<br />'; 
  9. $pud = $rsa->pubDecrypt($pre); 
  10. echo 'public decrypted:' . $pud . '<br />'; 
  11. //公钥加密,私钥解密 
  12. echo 'source:干IT的<br />'; 
  13. $pue = $rsa->pubEncrypt('干IT的'); 
  14. echo 'public encrypt:<br />' . $pue . '<br />'; 
  15. $prd = $rsa->privDecrypt($pue); 
  16. echo 'private decrypt:' . $prd; 
  17. //========================demo====================== 
  18. //====================demo======================= 
  19. //以下是一个简单的测试demo,如果不需要请删除 
  20. $rsa = new Rsa('sslkey'); //sslkey为存放密钥的路径,将已有的密钥文件复制到该路径下,公钥名称为pub.key,私钥名称为priv.key 
  21. $rsa->createKey(); //创建一对密钥,如果密钥对已经存在,不需调用 
  22. //私钥加密,公钥解密 
  23. echo 'source:脚本之家<br />'; 
  24. $pre = $rsa->privEncrypt('脚本之家'); 
  25. echo 'private encrypted:<br />' . $pre . '<br />'; 
  26. $pud = $rsa->pubDecrypt($pre); 
  27. echo 'public decrypted:' . $pud . '<br />'; 
  28. //公钥加密,私钥解密 
  29. echo 'source:干IT的<br />'; 
  30. $pue = $rsa->pubEncrypt('干IT的'); 
  31. echo 'public encrypt:<br />' . $pue . '<br />'; 
  32. $prd = $rsa->privDecrypt($pue); 
  33. echo 'private decrypt:' . $prd; 
  34. //========================demo====================== 

本示例在windows7、php 5.2.14、openssl 0.98下开发

  1. <?php 
  2. /** 
  3.  * 使用openssl实现非对称加密 
  4.  * 
  5.  */ 
  6. class Rsa 
  7. { 
  8.   /** 
  9.    * private key 
  10.    */ 
  11.     private $_privKey; 
  12.     /** 
  13.      * public key 
  14.      */ 
  15.     private $_pubKey; 
  16.     /** 
  17.      * the keys saving path 
  18.      */ 
  19.     private $_keyPath; 
  20.     /** 
  21.      * the construtor,the param $path is the keys saving path 
  22.      */ 
  23.     public function __construct($path) 
  24.     { 
  25.         if(emptyempty($path) || !is_dir($path)){ 
  26.             throw new Exception('Must set the keys save path'); 
  27.         } 
  28.         $this->_keyPath = $path; 
  29.     } 
  30.     /** 
  31.      * create the key pair,save the key to $this->_keyPath 
  32.      */ 
  33.     public function createKey() 
  34.     { 
  35.         $r = openssl_pkey_new(); 
  36.         openssl_pkey_export($r, $privKey); 
  37.         file_put_contents($this->_keyPath . DIRECTORY_SEPARATOR . 'priv.key', $privKey); 
  38.         $this->_privKey = openssl_pkey_get_private($privKey); 
  39.         $rp = openssl_pkey_get_details($r); 
  40.         $pubKey = $rp['key']; 
  41.         file_put_contents($this->_keyPath . DIRECTORY_SEPARATOR . 'pub.key', $pubKey); 
  42.         $this->_pubKey = openssl_pkey_get_public($pubKey); 
  43.     } 
  44.     /** 
  45.      * setup the private key 
  46.      */ 
  47.     public function setupPrivKey() 
  48.     { 
  49.         if(is_resource($this->_privKey)){ 
  50.             return true; 
  51.         } 
  52.         $file = $this->_keyPath . DIRECTORY_SEPARATOR . 'priv.key'; 
  53.         $prk = file_get_contents($file); 
  54.         $this->_privKey = openssl_pkey_get_private($prk); 
  55.         return true; 
  56.     } 
  57.     /** 
  58.      * setup the public key 
  59.      */ 
  60.     public function setupPubKey() 
  61.     { 
  62.         if(is_resource($this->_pubKey)){ 
  63.             return true; 
  64.         } 
  65.         $file = $this->_keyPath . DIRECTORY_SEPARATOR . 'pub.key'; 
  66.         $puk = file_get_contents($file); 
  67.         $this->_pubKey = openssl_pkey_get_public($puk); 
  68.         return true; 
  69.     } 
  70.     /** 
  71.      * encrypt with the private key 
  72.      */ 
  73.     public function privEncrypt($data) 
  74.     { 
  75.         if(!is_string($data)){ 
  76.             return null; 
  77.         } 
  78.         $this->setupPrivKey(); 
  79.         $r = openssl_private_encrypt($data, $encrypted, $this->_privKey); 
  80.         if($r){ 
  81.             return base64_encode($encrypted); 
  82.         } 
  83.         return null; 
  84.     } 
  85.     /** 
  86.      * decrypt with the private key 
  87.      */ 
  88.     public function privDecrypt($encrypted) 
  89.     { 
  90.         if(!is_string($encrypted)){ 
  91.             return null; 
  92.         } 
  93.         $this->setupPrivKey(); 
  94.         $encrypted = base64_decode($encrypted); 
  95.         $r = openssl_private_decrypt($encrypted, $decrypted, $this->_privKey); 
  96.         if($r){ 
  97.             return $decrypted; 
  98.         } 
  99.         return null; 
  100.     } 
  101.     /** 
  102.      * encrypt with public key 
  103.      */ 
  104.     public function pubEncrypt($data) 
  105.     { 
  106.         if(!is_string($data)){ 
  107.             return null; 
  108.         } 
  109.         $this->setupPubKey(); 
  110.         $r = openssl_public_encrypt($data, $encrypted, $this->_pubKey); 
  111.         if($r){ 
  112.             return base64_encode($encrypted); 
  113.         } 
  114.         return null; 
  115.     } 
  116.     /** 
  117.      * decrypt with the public key 
  118.      */ 
  119.     public function pubDecrypt($crypted) 
  120.     { 
  121.         if(!is_string($crypted)){ 
  122.             return null; 
  123.         } 
  124.         $this->setupPubKey(); 
  125.         $crypted = base64_decode($crypted); 
  126.         $r = openssl_public_decrypt($crypted, $decrypted, $this->_pubKey); 
  127.         if($r){ 
  128.             return $decrypted; 
  129.         } 
  130.         return null; 
  131.     } 
  132.     public function __destruct() 
  133.     { 
  134.         @ fclose($this->_privKey); 
  135.         @ fclose($this->_pubKey); 
  136.     } 
  137. }

Tags: PHP封装加密 RSA算法

分享到: