zoukankan      html  css  js  c++  java
  • RSA非对称加密算法实现过程

    RSA非对称加密算法实现过程

    非对称加密算法有很多,RSA算法就是其中比较出名的算法之一,下面是具体实现过程

      1 <?php
      2 /**
      3  * 使用openssl实现非对称加密  5  */
      6 class Rsa
      7 {
      8     /**
      9      * private key
     10      */
     11         private $_privKey;
     12  
     13         /**
     14          * public key
     15          */
     16         private $_pubKey;
     17  
     18         /**
     19          * the keys saving path
     20          */
     21         private $_keyPath;
     22  
     23         /**
     24          * the construtor,the param $path is the keys saving path
     25          */
     26         public function __construct($path)
     27         {
     28                 if(empty($path) || !is_dir($path)){
     29                         throw new Exception('Must set the keys save path');
     30                 }
     31  
     32                 $this->_keyPath = $path;
     33         }
     34  
     35         /**
     36          * create the key pair,save the key to $this->_keyPath
     37          */
     38         public function createKey()
     39         {
     40                 $r = openssl_pkey_new();
     41                 openssl_pkey_export($r, $privKey);
     42                 file_put_contents($this->_keyPath . DIRECTORY_SEPARATOR . 'priv.key', $privKey);
     43                 $this->_privKey = openssl_pkey_get_public($privKey);
     44  
     45                 $rp = openssl_pkey_get_details($r);
     46                 $pubKey = $rp['key'];
     47                 file_put_contents($this->_keyPath . DIRECTORY_SEPARATOR .  'pub.key', $pubKey);
     48                 $this->_pubKey = openssl_pkey_get_public($pubKey);
     49         }
     50  
     51         /**
     52          * setup the private key
     53          */
     54         public function setupPrivKey()
     55         {
     56                 if(is_resource($this->_privKey)){
     57                         return true;
     58                 }
     59                 $file = $this->_keyPath . DIRECTORY_SEPARATOR . 'priv.key';
     60                 $prk = file_get_contents($file);
     61                 $this->_privKey = openssl_pkey_get_private($prk);
     62                 return true;
     63         }
     64  
     65         /**
     66          * setup the public key
     67          */
     68         public function setupPubKey()
     69         {
     70                 if(is_resource($this->_pubKey)){
     71                         return true;
     72                 }
     73                 $file = $this->_keyPath . DIRECTORY_SEPARATOR .  'pub.key';
     74                 $puk = file_get_contents($file);
     75                 $this->_pubKey = openssl_pkey_get_public($puk);
     76                 return true;
     77         }
     78  
     79         /**
     80          * encrypt with the private key
     81          */
     82         public function privEncrypt($data)
     83         {
     84                 if(!is_string($data)){
     85                         return null;
     86                 }
     87  
     88                 $this->setupPrivKey();
     89  
     90                 $r = openssl_private_encrypt($data, $encrypted, $this->_privKey);
     91                 if($r){
     92                         return base64_encode($encrypted);
     93                 }
     94                 return null;
     95         }
     96  
     97         /**
     98          * decrypt with the private key
     99          */
    100         public function privDecrypt($encrypted)
    101         {
    102                 if(!is_string($encrypted)){
    103                         return null;
    104                 }
    105  
    106                 $this->setupPrivKey();
    107  
    108                 $encrypted = base64_decode($encrypted);
    109  
    110                 $r = openssl_private_decrypt($encrypted, $decrypted, $this->_privKey);
    111                 if($r){
    112                         return $decrypted;
    113                 }
    114                 return null;
    115         }
    116  
    117         /**
    118          * encrypt with public key
    119          */
    120         public function pubEncrypt($data)
    121         {
    122                 if(!is_string($data)){
    123                         return null;
    124                 }
    125  
    126                 $this->setupPubKey();
    127  
    128                 $r = openssl_public_encrypt($data, $encrypted, $this->_pubKey);
    129                 if($r){
    130                         return base64_encode($encrypted);
    131                 }
    132                 return null;
    133         }
    134  
    135         /**
    136          * decrypt with the public key
    137          */
    138         public function pubDecrypt($crypted)
    139         {
    140                 if(!is_string($crypted)){
    141                         return null;
    142                 }
    143  
    144                 $this->setupPubKey();
    145  
    146                 $crypted = base64_decode($crypted);
    147  
    148                 $r = openssl_public_decrypt($crypted, $decrypted, $this->_pubKey);
    149                 if($r){
    150                         return $decrypted;
    151                 }
    152                 return null;
    153         }
    154  
    155         public function __destruct()
    156         {
    157                 @ fclose($this->_privKey);
    158                 @ fclose($this->_pubKey);
    159         }
    160  
    161 }
    162  
    163 //====================demo=======================
    164 //以下是一个简单的测试demo,如果不需要请删除
    165 $rsa = new Rsa('ssl-key');
    166  
    167 //私钥加密,公钥解密
    168 echo 'source:我是老鳖<br />';
    169 $pre = $rsa->privEncrypt('我是老鳖');
    170 echo 'private encrypted:<br />' . $pre . '<br />';
    171  
    172 $pud = $rsa->pubDecrypt($pre);
    173 echo 'public decrypted:' . $pud . '<br />';
    174  
    175 //公钥加密,私钥解密
    176 echo 'source:干IT的<br />';
    177 $pue = $rsa->pubEncrypt('干IT的');
    178 echo 'public encrypt:<br />' . $pue . '<br />';
    179  
    180 $prd = $rsa->privDecrypt($pue);
    181 echo 'private decrypt:' . $prd;
    182 //========================demo======================
    183 ?>

    参考链接:RSA非对称加密

    多学、 多记、 多练、
  • 相关阅读:
    判断Java Bean对象所有属性是否为空
    Java在服务器下删除文件、删除目录及目录下所有文件失败(file.delete() --> false)
    Java判断两个Integer类型的值是否相等
    Java将多张图片合并打包成zip格式进行下载
    Java给图片添加水印部署到服务器出现中文乱码
    SpringBoot+SpringMvc页面跳转404问题
    已解决:Caused by: java.lang.NoClassDefFoundError: org/springframework/util/AntPathMatcher$AntPatternComparator$PatternInfo
    ActiveMQ如何解决被重复消费和数据丢失的问题?
    高并发系统三大利器之降级
    高并发系统三大利器之限流
  • 原文地址:https://www.cnblogs.com/cyfblogs/p/9857497.html
Copyright © 2011-2022 走看看