{"id":529,"date":"2024-11-07T00:50:43","date_gmt":"2024-11-06T16:50:43","guid":{"rendered":"http:\/\/www.wengdao.net\/?p=529"},"modified":"2024-11-07T00:58:15","modified_gmt":"2024-11-06T16:58:15","slug":"php%e7%8e%af%e5%a2%83%e4%b8%8b%e5%87%a0%e7%a7%8d%e5%8a%a0%e5%af%86%e6%96%b9%e5%bc%8f%e7%9a%84%e4%bd%bf%e7%94%a8%e6%b3%a8%e6%84%8f%e4%ba%8b%e9%a1%b9","status":"publish","type":"post","link":"https:\/\/jiandao.zliby.com\/?p=529","title":{"rendered":"PHP\u73af\u5883\u4e0b\u51e0\u79cd\u52a0\u5bc6\u65b9\u5f0f\u7684\u4f7f\u7528\u6ce8\u610f\u4e8b\u9879"},"content":{"rendered":"<p><strong>1. \u54c8\u5e0c\u7b97\u6cd5 \uff08MD5\u3001SHA-256\uff09\uff1a<\/strong><br \/>\n\u5e38\u7528\u7684MD5\u3001SHA-256 \u5e38\u7528\u4e8e\u5bc6\u7801\u5b58\u50a8\u548c\u6570\u636e\u5b8c\u6574\u6027\u9a8c\u8bc1\uff0c\u54c8\u5e0c\u7b97\u6cd5\u7ed3\u679c\u5355\u5411\u4e0d\u53ef\u9006\uff0c\u52a0\u5bc6\u540e\u65e0\u6cd5\u8fd8\u539f\u6570\u636e\uff0c\u53ea\u80fd\u7528\u4e8e\u6570\u636e\u5b8c\u6574\u6027\u9a8c\u8bc1\uff0c\u5f31\u54c8\u5e0c\u7b97\u6cd5\u7684\u6613\u7834\u89e3\u6027\uff0c\u5982\u679c\u4e0d\u8fdb\u884c\u591a\u5c42\u6b21\u52a0\u5bc6\uff0c\u5bb9\u6613\u88ab\u78b0\u649e\u653b\u51fb\uff0c\u88ab\u66b4\u529b\u7834\u89e3\u6216\u5f69\u8679\u8868\u653b\u51fb\u7834\u89e3\u51fa\u539f\u59cb\u6570\u636e\u3002\u5efa\u8bae\u591a\u5c42\u6b21\u52a0\u5bc6\u5e76\u4f7f\u7528\u4e0d\u540c\u52a0\u76d0\u503c\u3002<\/p>\n<pre><code class=\"prettyprint\" >    &lt;?php\n\/\/ \u660e\u6587\u5b57\u7b26\u4e32\n$plaintext = &quot;hello world&quot;;\n$salt = &quot;abcd&quot;;\n\n\/\/ \u52a0\u76d0\uff1a\u5c06\u76d0\u503c\u6dfb\u52a0\u5230\u5bc6\u7801\u7684\u524d\u540e\n$saltyPassword = $salt . $password; \/\/ \u53ef\u4ee5\u5c06\u76d0\u503c\u6dfb\u52a0\u5230\u5bc6\u7801\u7684\u5f00\u5934\u6216\u7ed3\u5c3e\uff0c\u9009\u62e9\u4efb\u610f\n\n\/\/ \u751f\u6210 MD5 \u54c8\u5e0c\u503c\n$hash = md5($plaintext);\n\n?&gt;\n<\/code><\/pre>\n<p><strong>2. \u5bf9\u79f0\u52a0\u5bc6 \uff08AES-256\uff09<\/strong><br \/>\nPHP\u4e2d\u901a\u5e38\u4f7f\u7528 AES \u7b97\u6cd5\uff08AES-256\uff09\uff0c\u901a\u8fc7 openssl \u6269\u5c55\u5b9e\u73b0\uff0c\u5bf9\u79f0\u52a0\u5bc6\u4f7f\u7528\u540c\u4e00\u4e2a\u5bc6\u94a5\u8fdb\u884c\u52a0\u5bc6\u548c\u89e3\u5bc6\uff0c\u5bc6\u94a5\u7684\u7ba1\u7406\u548c\u5206\u53d1\u6210\u4e3a\u4e3b\u8981\u95ee\u9898\u3002\u56e0\u4e3a\u52a0\u5bc6\u89e3\u5bc6\u53cc\u7aef\u90fd\u5b58\u50a8\u6709\u76f8\u540c\u7684\u5bc6\u94a5\uff0c\u53ef\u80fd\u4f1a\u906d\u9047\u6cc4\u9732\u98ce\u9669\uff0c\u5e76\u6ce8\u610fIV\u521d\u59cb\u5316\u5411\u91cf\u7684\u4f7f\u7528\u3002<\/p>\n<pre><code class=\"prettyprint\" >&lt;?php\nfunction encrypt($data, $key) {\n    $iv = random_bytes(openssl_cipher_iv_length(&#039;aes-256-cbc&#039;));\n    $encrypted = openssl_encrypt($data, &#039;aes-256-cbc&#039;, $key, 0, $iv);\n    return base64_encode($iv . $encrypted);\n}\n\nfunction decrypt($data, $key) {\n    $data = base64_decode($data);\n    $ivLength = openssl_cipher_iv_length(&#039;aes-256-cbc&#039;);\n    $iv = substr($data, 0, $ivLength);\n    $encryptedData = substr($data, $ivLength);\n    return openssl_decrypt($encryptedData, &#039;aes-256-cbc&#039;, $key, 0, $iv);\n}\n\n$key = &quot;your-secret-key&quot;;\n$originalData = &quot;Sensitive Data&quot;;\n$encryptedData = encrypt($originalData, $key);\n$decryptedData = decrypt($encryptedData, $key);\n\n?&gt;\n<\/code><\/pre>\n<p><strong>3. \u975e\u5bf9\u79f0\u52a0\u5bc6 \uff08RSA\uff09<\/strong><br \/>\nPHP\u4e2d\u975e\u5bf9\u79f0\u52a0\u5bc6\u901a\u5e38\u901a\u8fc7 RSA \u6216 \u692d\u5706\u66f2\u7ebf\u52a0\u5bc6\uff08ECC\uff09\u7b97\u6cd5\u5b9e\u73b0\uff0c\u5e38\u7528 openssl \u6216 sodium \u6269\u5c55\uff0c RSA\u7b97\u6cd5\u5728\u52a0\u5bc6\u548c\u89e3\u5bc6\u64cd\u4f5c\u76f8\u6bd4\u5bf9\u79f0\u52a0\u5bc6\u8981\u6162\u5f97\u591a\uff0c\u5c24\u5176\u662f\u9ad8\u5e76\u53d1\u73af\u5883\u4e0b\u66f4\u6d88\u8017\u6027\u80fd\uff0c\u540c\u6837RSA\u52a0\u5bc6\u53d7\u5bc6\u94a5\u957f\u5ea6\u7684\u5f71\u54cd\u6bd4\u8f83\u5927\uff0c\u901a\u5e38\u9700\u8981\u81f3\u5c112048 \u4f4d\u81f3\u66f4\u957f\u7684\u5bc6\u94a5\u624d\u80fd\u4fdd\u8bc1\u5b89\u5168\u3002\u7136\u800c\u66f4\u957f\u7684\u5bc6\u94a5\u4f1a\u8fdb\u4e00\u6b65\u589e\u52a0\u52a0\u5bc6\u548c\u89e3\u5bc6\u7684\u8017\u65f6\uff0c\u5f71\u54cd PHP \u5e94\u7528\u7684\u6027\u80fd\u3002\u540c\u6837\u53ef\u52a0\u5bc6\u7684\u6570\u636e\u5927\u5c0f\uff0c\u53d7\u5bc6\u94a5\u957f\u5ea6\u9650\u5236\uff0c2048\u7684\u5bc6\u94a5\u957f\u5ea6\uff0c\u80fd\u52a0\u5bc6\u7684\u5b57\u7b26\u4e32\u4e0a\u9650\u7ea6\u4e24\u767e\u591a\u4e00\u70b9\uff0c\u66f4\u591a\u5c31\u4f1a\u4ea7\u751f\u5f02\u5e38\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>1. \u54c8\u5e0c\u7b97\u6cd5 \uff08MD5\u3001SHA-256\uff09\uff1a \u5e38\u7528\u7684MD5\u3001SHA-256 \u5e38\u7528\u4e8e\u5bc6\u7801\u5b58\u50a8\u548c\u6570\u636e\u5b8c\u6574\u6027\u9a8c\u8bc1\uff0c\u54c8 [&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],"tags":[],"_links":{"self":[{"href":"https:\/\/jiandao.zliby.com\/index.php?rest_route=\/wp\/v2\/posts\/529"}],"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=529"}],"version-history":[{"count":3,"href":"https:\/\/jiandao.zliby.com\/index.php?rest_route=\/wp\/v2\/posts\/529\/revisions"}],"predecessor-version":[{"id":532,"href":"https:\/\/jiandao.zliby.com\/index.php?rest_route=\/wp\/v2\/posts\/529\/revisions\/532"}],"wp:attachment":[{"href":"https:\/\/jiandao.zliby.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=529"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jiandao.zliby.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=529"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jiandao.zliby.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=529"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}