{"id":533,"date":"2024-11-07T23:28:22","date_gmt":"2024-11-07T15:28:22","guid":{"rendered":"http:\/\/www.wengdao.net\/?p=533"},"modified":"2024-11-08T00:14:51","modified_gmt":"2024-11-07T16:14:51","slug":"rsa-%e5%8a%a0%e5%af%86%e7%ae%97%e6%b3%95","status":"publish","type":"post","link":"https:\/\/jiandao.zliby.com\/?p=533","title":{"rendered":"RSA \u975e\u5bf9\u79f0\u52a0\u5bc6\u7684\u4e00\u4e9b\u6ce8\u610f\u4e8b\u9879"},"content":{"rendered":"<p>\u5728php\u4e2dRSA\u52a0\u5bc6\u89e3\u5bc6\uff0c\u5bc6\u94a5\u957f\u5ea6\u548c\u586b\u5145\u65b9\u5f0f\u4e24\u79cd\u53c2\u6570\uff0c\u5b83\u4eec\u76f4\u63a5\u5f71\u54cd\u52a0\u5bc6\u7684\u5b89\u5168\u6027\u548c\u517c\u5bb9\u6027\uff0c\u751a\u81f3\u5f71\u54cd\u52a0\u5bc6\u89e3\u5bc6\u6210\u529f\u4e0e\u5426<\/p>\n<p>\u5bc6\u94a5\u957f\u5ea6:<\/p>\n<ul>\n<li>RSA \u5e38\u7528\u7684\u5bc6\u94a5\u957f\u5ea6\u6709 1024\u30012048\u30013072 \u548c 4096 \u4f4d\u3002<\/li>\n<li>\u5bc6\u94a5\u957f\u5ea6\u8d8a\u957f\uff0c\u52a0\u5bc6\u5f3a\u5ea6\u8d8a\u9ad8\uff0c\u56e0\u4e3a\u8981\u5206\u89e3 <\/li>\n<li>\u5bc6\u94a5\u957f\u5ea6\u8d8a\u957f\uff0c\u52a0\u5bc6\u548c\u89e3\u5bc6\u6240\u9700\u7684\u8ba1\u7b97\u8d44\u6e90\u8d8a\u591a\u3002\u957f\u5bc6\u94a5\u52a0\u5bc6\u89e3\u5bc6\u901f\u5ea6\u8f83\u6162\uff0c\u5360\u7528\u66f4\u591a\u7684\u5904\u7406\u65f6\u95f4\u548c\u5b58\u50a8\u7a7a\u95f4\u3002<\/li>\n<li>\u63a8\u8350 \u901a\u5e38 2048 \u4f4d RSA \u5bc6\u94a5\u5728\u5927\u591a\u6570\u60c5\u51b5\u4e0b\u8db3\u591f\u5b89\u5168\uff0c4096 \u4f4d\u66f4\u4e3a\u5b89\u5168\u4f46\u901f\u5ea6\u4f1a\u66f4\u6162\u3002\u5177\u4f53\u9009\u62e9\u9700\u8981\u5728\u5b89\u5168\u6027\u548c\u6027\u80fd\u4e4b\u95f4\u6743\u8861<\/li>\n<\/ul>\n<p>\u586b\u5145\u65b9\u5f0f:<br \/>\nRSA \u4f7f\u7528\u586b\u5145\u65b9\u5f0f\u6765\u786e\u4fdd\u52a0\u5bc6\u7684\u5b89\u5168\u6027\u3002\u586b\u5145\u65b9\u5f0f\u51b3\u5b9a\u4e86\u5728\u52a0\u5bc6\u524d\u5982\u4f55\u5904\u7406\u660e\u6587\uff0c\u9632\u6b62\u4e00\u4e9b\u6f5c\u5728\u7684\u653b\u51fb<\/p>\n<ul>\n<li>RSA_PKCS1_PADDING \u65b9\u5f0f<br \/>\nPKCS#1 v1.5 \u6807\u51c6\u4e2d\u7684\u586b\u5145\u65b9\u5f0f\uff0c\u5fc5\u987b\u6bd4 RSA \u94a5\u6a21\u957f\u77ed\u81f3\u5c1111\u4e2a\u5b57\u8282, \u4e5f\u5c31\u662f\u3000RSA_size(rsa) \u2013 11,<br \/>\n\u6bd4\u5982 2048 \u4f4d\u5bc6\u94a5\u53ef\u4ee5\u52a0\u5bc6\u7684\u6570\u636e\u6700\u5927\u4e3a 245 \u5b57\u8282<\/li>\n<li>RSA_PKCS1_OAEP_PADDING \u65b9\u5f0f<br \/>\nPKCS#1 v2.0 \u4e2d\u5f15\u5165\u7684\u586b\u5145\u65b9\u5f0f,\u4e0e PKCS#1 v1.5 \u76f8\u6bd4\u5b89\u5168\u6027\u6709\u5f88\u5927\u63d0\u5347.<br \/>\n\u5b83\u5360\u7528\u4e86\u66f4\u591a\u7a7a\u95f4\uff08\u4e00\u822c\u4e3a 41\u5b57\u8282\uff09,\u4e5f\u5c31\u662f RSA_size(rsa) \u2013 41<br \/>\n\u4f8b\u5982\uff0c2048 \u4f4d\u5bc6\u94a5\u53ef\u4ee5\u52a0\u5bc6\u7684\u660e\u6587\u6700\u5927\u4e3a 214 \u5b57\u8282<\/li>\n<li>RSA_NO_PADDING \u65b9\u5f0f<br \/>\n\u4e0d\u586b\u5145\uff0c\u53ef\u4ee5\u548cRSA\u94a5\u6a21\u957f\u4e00\u6837\u957f<\/li>\n<\/ul>\n<p><strong>RSA\u52a0\u5bc6\u5177\u6709\u786e\u5b9a\u6027\uff0c\u7ed9\u5b9a\u7684\u660e\u6587\u548c\u5bc6\u94a5\uff0c\u6bcf\u6b21\u52a0\u5bc6\u7684\u8f93\u51fa\u5bc6\u6587\u90fd\u5c06\u662f\u76f8\u540c\u7684\uff0c\u8fd9\u79cd\u786e\u5b9a\u6027\u5bb9\u6613\u5e26\u6765\u5b89\u5168\u98ce\u9669\uff0c\u5927\u91cf\u5bc6\u6587\u7edf\u8ba1\u5206\u6790\u4e0b\uff0c\u5bb9\u6613\u88ab\u63a8\u65ad\u5176\u4e2d\u4e00\u4e9b\u654f\u611f\u660e\u6587\u5185\u5bb9\u3002<br \/>\n\u540c\u6837RSA\u5177\u6709\u5ef6\u5c55\u6027\uff0c\u5728\u4e0d\u89e3\u5bc6\u5bc6\u6587\u7684\u60c5\u51b5\u4e0b\uff0c\u653b\u51fb\u8005\u53ef\u4ee5\u5bf9\u5bc6\u6587\u8fdb\u884c\u7279\u5b9a\u7684\u6570\u5b66\u64cd\u4f5c\uff0c\u4f7f\u660e\u6587\u53d1\u751f\u53ef\u9884\u6d4b\u7684\u53d8\u5316\uff0c\u5bb9\u6613\u88ab\u7be1\u6539\u3002<br \/>\n\u63a8\u8350\u4f7f\u7528 RSA_PKCS1_OAEP_PADDING\uff0c\u907f\u514d\u4f7f\u7528 RSA_PKCS1_PADDING \u548c RSA_NO_PADDING \u4e00\u4e2a\u8fc7\u671f\u4e00\u4e2a\u4e0d\u5b89\u5168\u3002<\/strong><\/p>\n<p><strong>\u6ce8\u610f\uff1a\u5bc6\u94a5\u957f\u5ea6\u548c\u586b\u5145\u65b9\u5f0f\u76f8\u4e92\u5f71\u54cd\uff0c\u76f8\u540c\u957f\u5ea6\u7684\u5bc6\u94a5\u4e0d\u540c\u586b\u5145\u65b9\u5f0f\uff0c\u53ef\u4ee5\u52a0\u5bc6\u7684\u6570\u636e\u957f\u5ea6\u4e0d\u76f8\u540c\u3002<\/strong><\/p>\n<p>rsa \u4f8b\u5b50<\/p>\n<pre><code class=\"prettyprint\" >&lt;?php\n\nclass RSAEncryption {\n    private $privateKey;\n    private $publicKey;\n\n    public function __construct($keyLength = 2048) {\n        $this-&gt;generateKeys($keyLength);\n    }\n\n    \/\/ \u751f\u6210\u5bc6\u94a5\u5bf9\n    private function generateKeys($keyLength) {\n        $config = [\n            &quot;private_key_bits&quot; =&gt; $keyLength,\n            &quot;private_key_type&quot; =&gt; OPENSSL_KEYTYPE_RSA,\n        ];\n\n        \/\/ \u751f\u6210\u79c1\u94a5\u8d44\u6e90\n        $res = openssl_pkey_new($config);\n        openssl_pkey_export($res, $this-&gt;privateKey);  \/\/ \u5bfc\u51fa\u79c1\u94a5\n        $publicKeyDetails = openssl_pkey_get_details($res);\n        $this-&gt;publicKey = $publicKeyDetails[&quot;key&quot;];  \/\/ \u83b7\u53d6\u516c\u94a5\n    }\n\n    \/\/ \u83b7\u53d6\u516c\u94a5\n    public function getPublicKey() {\n        return $this-&gt;publicKey;\n    }\n\n    \/\/ \u83b7\u53d6\u79c1\u94a5\n    public function getPrivateKey() {\n        return $this-&gt;privateKey;\n    }\n\n    \/\/ \u4f7f\u7528\u516c\u94a5\u52a0\u5bc6\n    public function encryptWithPublicKey($data, $padding = OPENSSL_PKCS1_PADDING) {\n        if (!openssl_public_encrypt($data, $encryptedData, $this-&gt;publicKey, $padding)) {\n            throw new Exception(&quot;\u516c\u94a5\u52a0\u5bc6\u5931\u8d25\uff1a&quot; . openssl_error_string());\n        }\n        return base64_encode($encryptedData);  \/\/ \u8fd4\u56de\u52a0\u5bc6\u540e\u7684base64\u7f16\u7801\n    }\n\n    \/\/ \u4f7f\u7528\u79c1\u94a5\u89e3\u5bc6\n    public function decryptWithPrivateKey($encryptedData, $padding = OPENSSL_PKCS1_PADDING) {\n        if (!openssl_private_decrypt(base64_decode($encryptedData), $decryptedData, $this-&gt;privateKey, $padding)) {\n            throw new Exception(&quot;\u79c1\u94a5\u89e3\u5bc6\u5931\u8d25\uff1a&quot; . openssl_error_string());\n        }\n        return $decryptedData;  \/\/ \u8fd4\u56de\u89e3\u5bc6\u540e\u7684\u660e\u6587\n    }\n\n    \/\/ \u4f7f\u7528\u79c1\u94a5\u52a0\u5bc6\n    public function encryptWithPrivateKey($data, $padding = OPENSSL_PKCS1_PADDING) {\n        if (!openssl_private_encrypt($data, $encryptedData, $this-&gt;privateKey, $padding)) {\n            throw new Exception(&quot;\u79c1\u94a5\u52a0\u5bc6\u5931\u8d25\uff1a&quot; . openssl_error_string());\n        }\n        return base64_encode($encryptedData);  \/\/ \u8fd4\u56de\u52a0\u5bc6\u540e\u7684base64\u7f16\u7801\n    }\n\n    \/\/ \u4f7f\u7528\u516c\u94a5\u89e3\u5bc6\n    public function decryptWithPublicKey($encryptedData, $padding = OPENSSL_PKCS1_PADDING) {\n        if (!openssl_public_decrypt(base64_decode($encryptedData), $decryptedData, $this-&gt;publicKey, $padding)) {\n            throw new Exception(&quot;\u516c\u94a5\u89e3\u5bc6\u5931\u8d25\uff1a&quot; . openssl_error_string());\n        }\n        return $decryptedData;  \/\/ \u8fd4\u56de\u89e3\u5bc6\u540e\u7684\u660e\u6587\n    }\n}\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u5728php\u4e2dRSA\u52a0\u5bc6\u89e3\u5bc6\uff0c\u5bc6\u94a5\u957f\u5ea6\u548c\u586b\u5145\u65b9\u5f0f\u4e24\u79cd\u53c2\u6570\uff0c\u5b83\u4eec\u76f4\u63a5\u5f71\u54cd\u52a0\u5bc6\u7684\u5b89\u5168\u6027\u548c\u517c\u5bb9\u6027\uff0c\u751a\u81f3\u5f71\u54cd\u52a0\u5bc6\u89e3\u5bc6\u6210\u529f\u4e0e [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3,1],"tags":[],"_links":{"self":[{"href":"https:\/\/jiandao.zliby.com\/index.php?rest_route=\/wp\/v2\/posts\/533"}],"collection":[{"href":"https:\/\/jiandao.zliby.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jiandao.zliby.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jiandao.zliby.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/jiandao.zliby.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=533"}],"version-history":[{"count":9,"href":"https:\/\/jiandao.zliby.com\/index.php?rest_route=\/wp\/v2\/posts\/533\/revisions"}],"predecessor-version":[{"id":543,"href":"https:\/\/jiandao.zliby.com\/index.php?rest_route=\/wp\/v2\/posts\/533\/revisions\/543"}],"wp:attachment":[{"href":"https:\/\/jiandao.zliby.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=533"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jiandao.zliby.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=533"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jiandao.zliby.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=533"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}