{"id":447,"date":"2017-12-15T14:12:04","date_gmt":"2017-12-15T06:12:04","guid":{"rendered":"http:\/\/luchanglong.com.cn\/?p=447"},"modified":"2017-12-15T14:13:51","modified_gmt":"2017-12-15T06:13:51","slug":"php-rsa%e5%8a%a0%e5%af%86%e8%a7%a3%e5%af%86%e5%ae%8c%e6%95%b4%e5%ae%9e%e4%be%8b","status":"publish","type":"post","link":"https:\/\/blog.la998.com\/?p=447","title":{"rendered":"php rsa\u52a0\u5bc6\u89e3\u5bc6\u5b8c\u6574\u5b9e\u4f8b"},"content":{"rendered":"<pre class=\"lang:php decode:true \">&lt;?php\r\n\/**\r\n * RSA\u52a0\u5bc6\r\n * env:(PHP 4 &gt;= 4.0.4, PHP 5, PHP 7)\r\n * \u5bc6\u94a5\u5bf9\u751f\u6210\r\n * \u5de5\u5177\uff1aOpenSSL\r\n * \u751f\u6210\u79c1\u94a5\uff1agenrsa -out rsa_private_key.pem 1024\r\n * \u751f\u6210\u516c\u94a5\uff1arsa -in rsa_private_key.pem -pubout -out rsa_public_key.pem\r\n * Author:luchanglong\r\n * Date:2017-12-14\r\n * **********************************************\r\n * * \u79c1\u94a5\u4e22\u5931\u5c06\u5bfc\u81f4\u6570\u636e\u6c38\u4e45\u65e0\u6cd5\u89e3\u5bc6\r\n * *********************************************\r\n *\/\r\n\r\nheader(\"Content-type: text\/html; charset=utf-8\");\r\n\r\nclass RsaTools\r\n{\r\n    \/\/\u79c1\u94a5\u6587\u4ef6\u8def\u5f84\r\n    private $rsaPrivateKeyFilePath;\r\n\r\n    \/\/\u79c1\u94a5\u503c\r\n    private $rsaPrivateKey;\r\n\r\n    \/\/\u516c\u94a5\u6587\u4ef6\u8def\u5f84\r\n    private $rsaPublicKeyFilePath;\r\n\r\n    \/\/\u516c\u94a5\u503c\r\n    private $rsaPublicKey;\r\n\r\n    function __construct()\r\n    {\r\n        $this-&gt;rsaPrivateKeyFilePath=dirname(__FILE__).DIRECTORY_SEPARATOR.'key'.DIRECTORY_SEPARATOR.'rsa_private_key.pem';\r\n        $this-&gt;rsaPublicKeyFilePath=dirname(__FILE__).DIRECTORY_SEPARATOR.'key'.DIRECTORY_SEPARATOR.'rsa_public_key.pem';\r\n    }\r\n\r\n    \/**\r\n     *  rsa\u516c\u94a5\u52a0\u5bc6\r\n     **\/\r\n    public function rsaEncrypt($data) {\r\n        if($this-&gt;checkEmpty($this-&gt;rsaPublicKeyFilePath)){\r\n            \/\/\u8bfb\u53d6\u5b57\u7b26\u4e32\r\n            $pubKey= $this-&gt;rsaPublicKey;\r\n            $res = \"-----BEGIN PUBLIC KEY-----\\n\" .\r\n                wordwrap($pubKey, 64, \"\\n\", true) .\r\n                \"\\n-----END PUBLIC KEY-----\";\r\n        }else {\r\n            \/\/\u8bfb\u53d6\u516c\u94a5\u6587\u4ef6\r\n            $pubKey = file_get_contents($this-&gt;rsaPublicKeyFilePath);\r\n            \/\/\u8f6c\u6362\u4e3aopenssl\u683c\u5f0f\u5bc6\u94a5\r\n            $res = openssl_get_publickey($pubKey);\r\n        }\r\n\r\n        ($res) or die('RSA\u516c\u94a5\u9519\u8bef\u3002\u8bf7\u68c0\u67e5\u516c\u94a5\u6587\u4ef6\u683c\u5f0f\u662f\u5426\u6b63\u786e');\r\n        $data=trim($data);\r\n        openssl_public_encrypt($data,$encrypted,$pubKey);\/\/\u516c\u94a5\u52a0\u5bc6\r\n        $encrypted = base64_encode($encrypted);\r\n        return $encrypted;\r\n    }\r\n    \/**\r\n     * rsa\u79c1\u94a5\u89e3\u5bc6\r\n     **\/\r\n    public function rsaDecrypt($data) {\r\n\r\n        if($this-&gt;checkEmpty($this-&gt;rsaPrivateKeyFilePath)){\r\n            \/\/\u8bfb\u5b57\u7b26\u4e32\r\n            $priKey=$this-&gt;rsaPrivateKey;\r\n            $res = \"-----BEGIN RSA PRIVATE KEY-----\\n\" .\r\n                wordwrap($priKey, 64, \"\\n\", true) .\r\n                \"\\n-----END RSA PRIVATE KEY-----\";\r\n        }else {\r\n            $priKey = file_get_contents($this-&gt;rsaPrivateKeyFilePath);\r\n            $res = openssl_get_privatekey($priKey);\r\n        }\r\n        ($res) or die('\u60a8\u4f7f\u7528\u7684\u79c1\u94a5\u683c\u5f0f\u9519\u8bef\uff0c\u8bf7\u68c0\u67e5RSA\u79c1\u94a5\u914d\u7f6e');\r\n        $data=base64_decode($data);\r\n        openssl_private_decrypt($data, $dcyCont, $res);\r\n        return $dcyCont;\r\n    }\r\n\r\n    \/**\r\n     * \u6821\u9a8c$value\u662f\u5426\u975e\u7a7a\r\n     *  if not set ,return true;\r\n     *    if is null , return true;\r\n     **\/\r\n    protected function checkEmpty($value) {\r\n        if (!isset($value))\r\n            return true;\r\n        if ($value === null)\r\n            return true;\r\n        if (trim($value) === \"\")\r\n            return true;\r\n\r\n        return false;\r\n    }\r\n\r\n}\r\n\r\n\r\n$r=new RsaTools();\r\n$str=\"0123456789?&gt;&lt;~!@#$%^&amp;*()_+qwertyuiopasdfghjklzxcvbnm\";\r\necho '\u5f85\u52a0\u5bc6\uff1a'.$str.\"&lt;br&gt;\";\r\n$en=$r-&gt;rsaEncrypt($str);\r\necho '\u52a0\u5bc6\u540e\uff1a'.$en.'&lt;br&gt;';\r\n$de=$r-&gt;rsaDecrypt($en);\r\necho '\u89e3\u5bc6\u540e\uff1a'.$de;\r\n\r\n<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>&lt;?php \/** * RSA\u52a0\u5bc6 * env:(PHP 4 &gt;= 4.0.4, PHP 5, P &hellip; <a href=\"https:\/\/blog.la998.com\/?p=447\" class=\"more-link\">\u7ee7\u7eed\u9605\u8bfb<span class=\"screen-reader-text\">php rsa\u52a0\u5bc6\u89e3\u5bc6\u5b8c\u6574\u5b9e\u4f8b<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[],"class_list":["post-447","post","type-post","status-publish","format-standard","hentry","category-php"],"_links":{"self":[{"href":"https:\/\/blog.la998.com\/index.php?rest_route=\/wp\/v2\/posts\/447","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.la998.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.la998.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.la998.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.la998.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=447"}],"version-history":[{"count":3,"href":"https:\/\/blog.la998.com\/index.php?rest_route=\/wp\/v2\/posts\/447\/revisions"}],"predecessor-version":[{"id":450,"href":"https:\/\/blog.la998.com\/index.php?rest_route=\/wp\/v2\/posts\/447\/revisions\/450"}],"wp:attachment":[{"href":"https:\/\/blog.la998.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=447"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.la998.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=447"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.la998.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=447"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}