首页 > 开发 > PHP > 正文

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

2024-05-04 22:01:56
字体:大 中 小
来源:转载
供稿:网友

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

  1. <?php 
  2.  
  3. namespace MyObjSummary; 
  4.  
  5. /** 
  6.  
  7.  * 使用openssl实现非对称加密 
  8.  
  9.  */ 
  10.  
  11. class Rsa 
  12.  
  13. { 
  14.  
  15.     /** 
  16.  
  17.      * 私钥 
  18.  
  19.      *  
  20.  
  21.      */ 
  22.  
  23.     private $_privKey; 
  24.  
  25.   
  26.  
  27.     /** 
  28.  
  29.      * 公钥 
  30.  
  31.      *  
  32.  
  33.      */ 
  34.  
  35.     private $_pubKey; 
  36.  
  37.   
  38.  
  39.     /** 保存文件地址 
  40.  
  41.      * @var 
  42.  
  43.      */ 
  44.  
  45.     private $_keyPath; 
  46.  
  47.   
  48.  
  49.     /** 公钥 
  50.  
  51.      * @var string 
  52.  
  53.      */ 
  54.  
  55.     private $_pubKeyLink = "-----BEGIN PUBLIC KEY----- 
  56.  
  57. MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCF4sz1eu4XgLeIK9Aiu4+rfglt 
  58.  
  59. k1gmNhUytOtk3kbzPoy2XoR5sQIRXBYnIagwBVOLPWDacVJoqjfeK6xGvL17745u 
  60.  
  61. Z7RubcZIW62ocgX3swIDAQAB 
  62.  
  63. -----END PUBLIC KEY-----"; 
  64.  
  65.     /**私钥 
  66.  
  67.      * @var string 
  68.  
  69.      */ 
  70.  
  71.     private $_priKeyLink = "-----BEGIN RSA PRIVATE KEY----- 
  72.  
  73. MIICXAIBAAKBgQCF4sz1eu4XgLeIK9Aiu4+rfgltk1gmNhUytOtk3kbzPoy2XoR5 
  74.  
  75. sQIRXBYnIagwBVOLPWDacVJoqjfeK6xGvL17745uwNSw3eKLl1qm+w2z5KhNEnpg 
  76.  
  77. LWxKxSPMfekt1Aj3Te0Ct652Scr42Coca/ld2mGkZ7RubcZIW62ocgX3swIDAQAB 
  78.  
  79. AoGAHinbvU6Fx5vDPZWJXdnd42gQ3bP9fxZeLj9ebSo61+B2uTuQIw6DBcA2aXiG 
  80.  
  81. uNLqYItif7RaOaRn09EJDiLFmYwRBXAGnEdSnxWRy/IMrtKATV+dLnyFDVrIzsn+ 
  82.  
  83. /9l3HQXKhlSqTc4v7o1sWAM9GW2vjB3X432BjzbgqCyplOECQQC7UnvQUZYT+sum 
  84.  
  85. PStREJt85krUKgeFwyQdji+BdAXhv9xz3PiSWsAvw87zFrpBKcWbTimSH38onKGa 
  86.  
  87. htuYE08xAkEAtvjx7t05TiVusPcsgABxoABKRKZpcY5QQIXTT3oigvCMuz41nBDm 
  88.  
  89. EXeot+TXBGwG0QNS7p5BwkrXfCFJJONkIwJAUbcItfZxPqQAJLO4arOQ8KpRaD4x 
  90.  
  91. a+OVpKL7DEC9tB4LICv773RRNET5yUdX1sdPIZG2Rr0grmmtgYhk0PFTcQJBAI8I 
  92.  
  93. uv2VL3fMBI4SGWWN/LPSeZkUdPbh0GmRCSo4nPOfxK8= 
  94.  
  95. -----END RSA PRIVATE KEY-----"; 
  96.  
  97.     /** 
  98.  
  99.      * Rsa constructor. 
  100.  
  101.      * @param string $path 
  102.  
  103.      */ 
  104.  
  105.     public function __construct($path='') 
  106.  
  107.     { 
  108.  
  109.         if (!emptyempty($path)) { 
  110.  
  111.             $this->_keyPath = $path; 
  112.  
  113.         } 
  114.  
  115.     } 
  116.  
  117.   
  118.  
  119.     /** 
  120.  
  121.      * 创建公钥和私钥 
  122.  
  123.      *  
  124.  
  125.      */ 
  126.  
  127.     public function createKey() 
  128.  
  129.     { 
  130.  
  131.         $config = [ 
  132.  
  133.             "config" => 'D:/Min/Install/wamp/wamp64/bin/php/php5.6.25/extras/ssl/openssl.cnf', 
  134.  
  135.             "digest_alg" => "sha512", 
  136.  
  137.             "private_key_bits" => 4096, 
  138.  
  139.             "private_key_type" => OPENSSL_KEYTYPE_RSA, 
  140.  
  141.   
  142.  
  143.         ]; 
  144.  
  145.         // 生成私钥 
  146.  
  147.         $rsa = openssl_pkey_new($config); 
  148.  
  149.         openssl_pkey_export($rsa, $privKey, NULL, $config); 
  150.  
  151.         file_put_contents($this->_keyPath . DIRECTORY_SEPARATOR . 'priv.key', $privKey); 
  152.  
  153.         $this->_privKey = openssl_pkey_get_public($privKey); 
  154.  
  155.         // 生成公钥 
  156.  
  157.         $rsaPri = openssl_pkey_get_details($rsa); 
  158.  
  159.         $pubKey = $rsaPri['key']; 
  160.  
  161.         file_put_contents($this->_keyPath . DIRECTORY_SEPARATOR . 'pub.key', $pubKey); 
  162.  
  163.         $this->_pubKey = openssl_pkey_get_public($pubKey); 
  164.  
  165.     } 
  166.  
  167.   
  168.  
  169.     /** 设置私钥 
  170.  
  171.      * @return bool 
  172.  
  173.      */ 
  174.  
  175.     public function setupPrivKey() 
  176.  
  177.     { 
  178.  
  179.         if (is_resource($this->_privKey)) { 
  180.  
  181.             return true; 
  182.  
  183.         } 
  184.  
  185.         //从文件中获取 
  186.  
  187.         /*$file = $this->_keyPath . DIRECTORY_SEPARATOR . 'priv.key'; 
  188.  
  189.         $privKey = file_get_contents($file);*/ 
  190.  
  191.         $privKey = $this->_priKeyLink; 
  192.  
  193.         $this->_privKey = openssl_pkey_get_private($privKey); 
  194.  
  195.         return true; 
  196.  
  197.     } 
  198.  
  199.   
  200.  
  201.     /** 设置公钥 
  202.  
  203.      * @return bool 
  204.  
  205.      */ 
  206.  
  207.     public function setupPubKey() 
  208.  
  209.     { 
  210.  
  211.         //从文件中获取 
  212.  
  213.         /*$file = $this->_keyPath . DIRECTORY_SEPARATOR . 'pub.key'; 
  214.  
  215.         $pubKey = file_get_contents($file);*/ 
  216.  
  217.         //数据源 
  218.  
  219.         $pubKey = $this->_pubKeyLink; 
  220.  
  221.         $this->_pubKey = openssl_pkey_get_public($pubKey); 
  222.  
  223.         return true; 
  224.  
  225.     } 
  226.  
  227.   
  228.  
  229.     /** 用私钥加密 
  230.  
  231.      * @param $data 
  232.  
  233.      * @return null|string 
  234.  
  235.      */ 
  236.  
  237.     public function privEncrypt($data) 
  238.  
  239.     { 
  240.  
  241.         if (!is_string($data)) { 
  242.  
  243.             return null; 
  244.  
  245.         } 
  246.  
  247.         $this->setupPrivKey(); 
  248.  
  249.         $result = openssl_private_encrypt($data, $encrypted, $this->_privKey); 
  250.  
  251.         if ($result) { 
  252.  
  253.             return base64_encode($encrypted); 
  254.  
  255.         } 
  256.  
  257.         return null; 
  258.  
  259.     } 
  260.  
  261.   
  262.  
  263.     /** 私钥解密 
  264.  
  265.      * @param $encrypted 
  266.  
  267.      * @return null 
  268.  
  269.      */ 
  270.  
  271.     public function privDecrypt($encrypted) 
  272.  
  273.     { 
  274.  
  275.         if (!is_string($encrypted)) { 
  276.  
  277.             return null; 
  278.  
  279.         } 
  280.  
  281.         $this->setupPrivKey(); 
  282.  
  283.         $encrypted = base64_decode($encrypted); 
  284.  
  285.         $result = openssl_private_decrypt($encrypted, $decrypted, $this->_privKey); 
  286.  
  287.         if ($result) { 
  288.  
  289.             return $decrypted; 
  290.  
  291.         } 
  292.  
  293.         return null; 
  294.  
  295.     } 
  296.  
  297.   
  298.  
  299.     /** 公钥加密 
  300.  
  301.      * @param $data 
  302.  
  303.      * @return null|string 
  304.  
  305.      */ 
  306.  
  307.     public function pubEncrypt($data) 
  308.  
  309.     { 
  310.  
  311.         if (!is_string($data)) { 
  312.  
  313.             return null; 
  314.  
  315.         } 
  316.  
  317.         $this->setupPubKey(); 
  318.  
  319.         $result = openssl_public_encrypt($data, $encrypted, $this->_pubKey); 
  320.  
  321.         if ($result) { 
  322.  
  323.             return base64_encode($encrypted); 
  324.  
  325.         } 
  326.  
  327.         return null; 
  328.  
  329.     } 
  330.  
  331.   
  332.  
  333.     /** 公钥解密 
  334.  
  335.      * @param $crypted 
  336.  
  337.      * @return null 
  338.  
  339.      */ 
  340.  
  341.     public function pubDecrypt($crypted) 
  342.  
  343.     { 
  344.  
  345.         if (!is_string($crypted)) { 
  346.  
  347.             return null; 
  348.  
  349.         } 
  350.  
  351.         $this->setupPubKey(); 
  352.  
  353.         $crypted = base64_decode($crypted); 
  354.  
  355.         $result = openssl_public_decrypt($crypted, $decrypted, $this->_pubKey); 
  356.  
  357.         if ($result) { 
  358.  
  359.             return $decrypted; 
  360.  
  361.         } 
  362.  
  363.         return null; 
  364.  
  365.     } 
  366.  
  367.   
  368.  
  369.     /** 私钥签名 
  370.  
  371.      * @param $data 
  372.  
  373.      * @return string 
  374.  
  375.      */ 
  376.  
  377.     public function priKeySign($data) 
  378.  
  379.     { 
  380.  
  381.         if(!is_string($data)) return null; 
  382.  
  383.         $private_key=openssl_get_privatekey($this->_priKeyLink); 
  384.  
  385.         $original_str= $data ;//原数据 
  386.  
  387.         openssl_sign($original_str,$sign,$private_key); 
  388.  
  389.         openssl_free_key($private_key); 
  390.  
  391.         $sign=base64_encode($sign);//最终的签名 
  392.  
  393.         return $sign ; 
  394.  
  395.     } 
  396.  
  397.   
  398.  
  399.     /** 公钥验签 
  400.  
  401.      * @param $sign 
  402.  
  403.      * @param $data 
  404.  
  405.      * @return bool 
  406.  
  407.      */ 
  408.  
  409.     public  function pubKeyCheck($sign,$data) 
  410.  
  411.     { 
  412.  
  413.         if(!is_string($sign) || !is_string($data)) return null; 
  414.  
  415.         $public_key=openssl_get_publickey($this->_pubKeyLink); 
  416.  
  417.         $sign=base64_decode($sign);//得到的签名 
  418.  
  419.         $original_str=$data; 
  420.  
  421.         $result=(bool)openssl_verify($original_str,$sign,$public_key); 
  422.  
  423.         openssl_free_key($public_key); 
  424.  
  425.         return $result ; 
  426.  
  427.     } 
  428.  
  429.   
  430.  
  431.     /** 
  432.  
  433.      * __destruct 
  434.  
  435.      *  
  436.  
  437.      */ 
  438.  
  439.     public function __destruct() { 
  440.  
  441.         @fclose($this->_privKey); 
  442.  
  443.         @fclose($this->_pubKey); 
  444.  
  445.     } 
  446.  
  447. } 
  448.  
  449. $rsa = new Rsa(); 
  450.  
  451.   
  452.  
  453. echo "openssl_private_encrypt,openssl_public_decrypt","<br />"; 
  454.  
  455. //私钥加密,公钥解密 
  456.  
  457. echo "私钥加密,公钥验签","<br />"; 
  458.  
  459. echo "待加密数据:testInfo","<br />"; 
  460.  
  461. $pre = $rsa->privEncrypt("testInfo"); 
  462.  
  463. echo "加密后的密文:<br />" . $pre . "<br />"; 
  464.  
  465. $pud = $rsa->pubDecrypt($pre); 
  466.  
  467. echo "解密后数据:" . $pud . "<br />"; 
  468.  
  469. echo "<hr>"; 
  470.  
  471.   
  472.  
  473.   
  474.  
  475. //公钥加密,私钥解密 
  476.  
  477. echo "openssl_public_encrypt,openssl_private_decrypt","<br />"; 
  478.  
  479. echo "公钥加密,私钥验签","<br />"; 
  480.  
  481. echo "待加密数据:ssh-test","<br />"; 
  482.  
  483. $pue = $rsa->pubEncrypt("ssh-test"); 
  484.  
  485. echo "加密后的密文:","<br />" . $pue . "<br />"; 
  486.  
  487. $prd = $rsa->privDecrypt($pue); 
  488.  
  489. echo "解密后数据:" . $prd; 
  490.  
  491.   
  492.  
  493. echo "<hr>";echo "<hr>"; 
  494.  
  495.   
  496.  
  497. echo "openssl_sign,openssl_verify","<br />"; 
  498.  
  499. echo "私钥签名,公钥验签","<br />"; 
  500.  
  501. echo "待加密数据:test=32","<br />"; 
  502.  
  503. $pre = $rsa->priKeySign('test=32'); 
  504.  
  505. echo "加密后的密文:","<br />" . $pre . "<br />"; 
  506. //Vevb.com 
  507. $pud = $rsa->pubKeyCheck($pre,'test=32'); 
  508.  
  509. echo "是否解密成功:" . $pud . "<br />"; 
  510.  
  511. echo "<hr>"; 

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表