A-A+

php和openssl实现非对称加密的代码示例

2020年02月03日 我爱编程 暂无评论

本篇文章给大家带来的内容是关于php和openssl实现非对称加密的代码示例,有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助。

  1. <?php
  2. namespace MyObjSummary;
  3. /**
  4.  * 使用openssl实现非对称加密
  5.  */
  6. class Rsa
  7. {
  8.     /**
  9.      * 私钥
  10.      * 
  11.      */
  12.     private $_privKey;
  13.     /**
  14.      * 公钥
  15.      * 
  16.      */
  17.     private $_pubKey;
  18.     /** 保存文件地址
  19.      * @var
  20.      */
  21.     private $_keyPath;
  22.     /** 公钥
  23.      * @var string
  24.      */
  25.     private $_pubKeyLink = "-----BEGIN PUBLIC KEY-----
  26. MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCF4sz1eu4XgLeIK9Aiu4+rfglt
  27. k1gmNhUytOtk3kbzPoy2XoR5sQIRXBYnIagwBVOLPWDacVJoqjfeK6xGvL17745u
  28. Z7RubcZIW62ocgX3swIDAQAB
  29. -----END PUBLIC KEY-----";
  30.     /**私钥
  31.      * @var string
  32.      */
  33.     private $_priKeyLink = "-----BEGIN RSA PRIVATE KEY-----
  34. MIICXAIBAAKBgQCF4sz1eu4XgLeIK9Aiu4+rfgltk1gmNhUytOtk3kbzPoy2XoR5
  35. sQIRXBYnIagwBVOLPWDacVJoqjfeK6xGvL17745uwNSw3eKLl1qm+w2z5KhNEnpg
  36. LWxKxSPMfekt1Aj3Te0Ct652Scr42Coca/ld2mGkZ7RubcZIW62ocgX3swIDAQAB
  37. AoGAHinbvU6Fx5vDPZWJXdnd42gQ3bP9fxZeLj9ebSo61+B2uTuQIw6DBcA2aXiG
  38. uNLqYItif7RaOaRn09EJDiLFmYwRBXAGnEdSnxWRy/IMrtKATV+dLnyFDVrIzsn+
  39. /9l3HQXKhlSqTc4v7o1sWAM9GW2vjB3X432BjzbgqCyplOECQQC7UnvQUZYT+sum
  40. PStREJt85krUKgeFwyQdji+BdAXhv9xz3PiSWsAvw87zFrpBKcWbTimSH38onKGa
  41. htuYE08xAkEAtvjx7t05TiVusPcsgABxoABKRKZpcY5QQIXTT3oigvCMuz41nBDm
  42. EXeot+TXBGwG0QNS7p5BwkrXfCFJJONkIwJAUbcItfZxPqQAJLO4arOQ8KpRaD4x
  43. a+OVpKL7DEC9tB4LICv773RRNET5yUdX1sdPIZG2Rr0grmmtgYhk0PFTcQJBAI8I
  44. uv2VL3fMBI4SGWWN/LPSeZkUdPbh0GmRCSo4nPOfxK8=
  45. -----END RSA PRIVATE KEY-----";
  46.     /**
  47.      * Rsa constructor.
  48.      * @param string $path
  49.      */
  50.     public function __construct($path='')
  51.     {
  52.         if (!emptyempty($path)) {
  53.             $this->_keyPath = $path;
  54.         }
  55.     }
  56.     /**
  57.      * 创建公钥和私钥
  58.      * 
  59.      */
  60.     public function createKey()
  61.     {
  62.         $config = [
  63.             "config" => 'D:\Min\Install\wamp\wamp64\bin\php\php5.6.25\extras\ssl\openssl.cnf',
  64.             "digest_alg" => "sha512",
  65.             "private_key_bits" => 4096,
  66.             "private_key_type" => OPENSSL_KEYTYPE_RSA,
  67.         ];
  68.         // 生成私钥
  69.         $rsa = openssl_pkey_new($config);
  70.         openssl_pkey_export($rsa$privKey, NULL, $config);
  71.         file_put_contents($this->_keyPath . DIRECTORY_SEPARATOR . 'priv.key'$privKey);
  72.         $this->_privKey = openssl_pkey_get_public($privKey);
  73.         // 生成公钥
  74.         $rsaPri = openssl_pkey_get_details($rsa);
  75.         $pubKey = $rsaPri['key'];
  76.         file_put_contents($this->_keyPath . DIRECTORY_SEPARATOR . 'pub.key'$pubKey);
  77.         $this->_pubKey = openssl_pkey_get_public($pubKey);
  78.     }
  79.     /** 设置私钥
  80.      * @return bool
  81.      */
  82.     public function setupPrivKey()
  83.     {
  84.         if (is_resource($this->_privKey)) {
  85.             return true;
  86.         }
  87.         //从文件中获取
  88.         /*$file = $this->_keyPath . DIRECTORY_SEPARATOR . 'priv.key';
  89.         $privKey = file_get_contents($file);*/
  90.         $privKey = $this->_priKeyLink;
  91.         $this->_privKey = openssl_pkey_get_private($privKey);
  92.         return true;
  93.     }
  94.     /** 设置公钥
  95.      * @return bool
  96.      */
  97.     public function setupPubKey()
  98.     {
  99.         //从文件中获取
  100.         /*$file = $this->_keyPath . DIRECTORY_SEPARATOR . 'pub.key';
  101.         $pubKey = file_get_contents($file);*/
  102.         //数据源
  103.         $pubKey = $this->_pubKeyLink;
  104.         $this->_pubKey = openssl_pkey_get_public($pubKey);
  105.         return true;
  106.     }
  107.     /** 用私钥加密
  108.      * @param $data
  109.      * @return null|string
  110.      */
  111.     public function privEncrypt($data)
  112.     {
  113.         if (!is_string($data)) {
  114.             return null;
  115.         }
  116.         $this->setupPrivKey();
  117.         $result = openssl_private_encrypt($data$encrypted$this->_privKey);
  118.         if ($result) {
  119.             return base64_encode($encrypted);
  120.         }
  121.         return null;
  122.     }
  123.     /** 私钥解密
  124.      * @param $encrypted
  125.      * @return null
  126.      */
  127.     public function privDecrypt($encrypted)
  128.     {
  129.         if (!is_string($encrypted)) {
  130.             return null;
  131.         }
  132.         $this->setupPrivKey();
  133.         $encrypted = base64_decode($encrypted);
  134.         $result = openssl_private_decrypt($encrypted$decrypted$this->_privKey);
  135.         if ($result) {
  136.             return $decrypted;
  137.         }
  138.         return null;
  139.     }
  140.     /** 公钥加密
  141.      * @param $data
  142.      * @return null|string
  143.      */
  144.     public function pubEncrypt($data)
  145.     {
  146.         if (!is_string($data)) {
  147.             return null;
  148.         }
  149.         $this->setupPubKey();
  150.         $result = openssl_public_encrypt($data$encrypted$this->_pubKey);
  151.         if ($result) {
  152.             return base64_encode($encrypted);
  153.         }
  154.         return null;
  155.     }
  156.     /** 公钥解密
  157.      * @param $crypted
  158.      * @return null
  159.      */
  160.     public function pubDecrypt($crypted)
  161.     {
  162.         if (!is_string($crypted)) {
  163.             return null;
  164.         }
  165.         $this->setupPubKey();
  166.         $crypted = base64_decode($crypted);
  167.         $result = openssl_public_decrypt($crypted$decrypted$this->_pubKey);
  168.         if ($result) {
  169.             return $decrypted;
  170.         }
  171.         return null;
  172.     }
  173.     /** 私钥签名
  174.      * @param $data
  175.      * @return string
  176.      */
  177.     public function priKeySign($data)
  178.     {
  179.         if(!is_string($data)) return null;
  180.         $private_key=openssl_get_privatekey($this->_priKeyLink);
  181.         $original_str$data ;//原数据
  182.         openssl_sign($original_str,$sign,$private_key);
  183.         openssl_free_key($private_key);
  184.         $sign=base64_encode($sign);//最终的签名
  185.         return $sign ;
  186.     }
  187.     /** 公钥验签
  188.      * @param $sign
  189.      * @param $data
  190.      * @return bool
  191.      */
  192.     public  function pubKeyCheck($sign,$data)
  193.     {
  194.         if(!is_string($sign) || !is_string($data)) return null;
  195.         $public_key=openssl_get_publickey($this->_pubKeyLink);
  196.         $sign=base64_decode($sign);//得到的签名
  197.         $original_str=$data;
  198.         $result=(bool)openssl_verify($original_str,$sign,$public_key);
  199.         openssl_free_key($public_key);
  200.         return $result ;
  201.     }
  202.     /**
  203.      * __destruct
  204.      * 
  205.      */
  206.     public function __destruct() {
  207.         @fclose($this->_privKey);
  208.         @fclose($this->_pubKey);
  209.     }
  210. }
  211. $rsa = new Rsa();
  212. echo "openssl_private_encrypt,openssl_public_decrypt","<br />";
  213. //私钥加密,公钥解密
  214. echo "私钥加密,公钥验签","<br />";
  215. echo "待加密数据:testInfo","<br />";
  216. $pre = $rsa->privEncrypt("testInfo");
  217. echo "加密后的密文:<br />" . $pre . "<br />";
  218. $pud = $rsa->pubDecrypt($pre);
  219. echo "解密后数据:" . $pud . "<br />";
  220. echo "<hr>";
  221. //公钥加密,私钥解密
  222. echo "openssl_public_encrypt,openssl_private_decrypt","<br />";
  223. echo "公钥加密,私钥验签","<br />";
  224. echo "待加密数据:ssh-test","<br />";
  225. $pue = $rsa->pubEncrypt("ssh-test");
  226. echo "加密后的密文:","<br />" . $pue . "<br />";
  227. $prd = $rsa->privDecrypt($pue);
  228. echo "解密后数据:" . $prd;
  229. echo "<hr>";echo "<hr>";
  230. echo "openssl_sign,openssl_verify","<br />";
  231. echo "私钥签名,公钥验签","<br />";
  232. echo "待加密数据:test=32","<br />";
  233. $pre = $rsa->priKeySign('test=32');
  234. echo "加密后的密文:","<br />" . $pre . "<br />";
  235. $pud = $rsa->pubKeyCheck($pre,'test=32');
  236. echo "是否解密成功:" . $pud . "<br />";
  237. echo "<hr>";

给我留言

Copyright © 四季博客 保留所有权利.   Theme  Ality

用户登录