{"id":25967,"date":"2024-11-24T15:59:57","date_gmt":"2024-11-24T07:59:57","guid":{"rendered":"https:\/\/fwq.ai\/blog\/25967\/"},"modified":"2024-11-24T15:59:57","modified_gmt":"2024-11-24T07:59:57","slug":"lithe-crypt%ef%bc%9a%e7%ae%80%e5%8c%96-php-%e5%ba%94%e7%94%a8%e7%a8%8b%e5%ba%8f%e4%b8%ad%e7%9a%84%e5%8a%a0%e5%af%86","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/25967\/","title":{"rendered":"Lithe Crypt\uff1a\u7b80\u5316 PHP \u5e94\u7528\u7a0b\u5e8f\u4e2d\u7684\u52a0\u5bc6"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/001\/246\/273\/173076733289710.jpg\" class=\"aligncenter\" title=\"Lithe Crypt\uff1a\u7b80\u5316 PHP \u5e94\u7528\u7a0b\u5e8f\u4e2d\u7684\u52a0\u5bc6\u63d2\u56fe\" alt=\"Lithe Crypt\uff1a\u7b80\u5316 PHP \u5e94\u7528\u7a0b\u5e8f\u4e2d\u7684\u52a0\u5bc6\u63d2\u56fe\" \/><\/p>\n<p><strong>lithe crypt<\/strong> \u662f\u4e00\u4e2a\u7b80\u5355\u7684 php \u52a0\u5bc6\u548c\u89e3\u5bc6\u5b9e\u7528\u7a0b\u5e8f\uff0c\u65e8\u5728\u4e0e lithe \u6846\u67b6\u914d\u5408\u4f7f\u7528\u3002\u5b83\u5229\u7528 aes-256-cbc \u7b97\u6cd5\u8fdb\u884c\u5b89\u5168\u6570\u636e\u5904\u7406\u3002<\/p>\n<h2> \u5b89\u88c5 <\/h2>\n<p>\u8981\u5b89\u88c5 lithe crypt \u8f6f\u4ef6\u5305\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528 \u3002\u5982\u679c\u60a8\u5c1a\u672a\u5b89\u88c5\uff0c\u8bf7\u786e\u4fdd composer \u5728\u60a8\u7684\u7cfb\u7edf\u4e0a\u53ef\u7528\u3002\u7136\u540e\u5728\u9879\u76ee\u76ee\u5f55\u4e2d\u8fd0\u884c\u4ee5\u4e0b\u547d\u4ee4\uff1a<\/p>\n<pre>composer require lithemod\/crypt\n<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<h3> \u8981\u6c42 <\/h3>\n<ul>\n<li>php 8 \u6216\u66f4\u9ad8\u7248\u672c<\/li>\n<li>\u5728 php \u5b89\u88c5\u4e2d\u542f\u7528 openssl \u6269\u5c55<\/li>\n<\/ul>\n<h2> \u7528\u6cd5 <\/h2>\n<h3> \u52a0\u8f7d\u73af\u5883\u53d8\u91cf <\/h3>\n<p>\u5728\u4f7f\u7528 crypt \u7c7b\u4e4b\u524d\uff0c\u60a8\u9700\u8981\u52a0\u8f7d\u73af\u5883\u53d8\u91cf\u3002\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\u52a0\u8f7d\u60a8\u7684 .env \u6587\u4ef6\uff1a<\/p>\n<pre>use lithesupportenv;\n\n\/\/ load environment variables\nenv::load(__dir__); \/\/ adjust the path as necessary\n<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<h3> \u8bbe\u7f6eapp_key <\/h3>\n<p>\u786e\u4fdd\u5df2\u8bbe\u7f6e app_key \u73af\u5883\u53d8\u91cf\u3002\u8be5\u5bc6\u94a5\u5e94\u8be5\u662f 32 \u5b57\u8282\u7684 base64 \u7f16\u7801\u5b57\u7b26\u4e32\u3002\u60a8\u53ef\u4ee5\u5728 .env \u6587\u4ef6\u4e2d\u914d\u7f6e\u5b83\uff0c\u4e5f\u53ef\u4ee5\u76f4\u63a5\u5728\u670d\u52a1\u5668\u73af\u5883\u4e2d\u914d\u7f6e\u5b83\u3002<\/p>\n<p>\u6709\u6548\u7684 base64 \u5bc6\u94a5\u793a\u4f8b\uff1a<\/p>\n<pre>yxnkzmfnc2rhc2rmywdlcybhc2rmywdlcybhyxnkzmfnc2q=\n<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<h3> \u52a0\u5bc6\u6570\u636e <\/h3>\n<p>\u8981\u52a0\u5bc6\u6570\u636e\uff0c\u8bf7\u4f7f\u7528 crypt \u7c7b\u7684 encrypt \u65b9\u6cd5\u3002\u60a8\u8fd8\u53ef\u4ee5\u6307\u5b9a\u662f\u5426\u8981\u4f7f\u7528\u56fa\u5b9aiv\uff08\u521d\u59cb\u5316\u5411\u91cf\uff09\u8fdb\u884c\u52a0\u5bc6\uff1a<\/p>\n<pre>use lithesupportsecuritycrypt;\n\n$data = \"sensitive data\";\n\n\/\/ encrypt without fixed iv\n$encrypted = crypt::encrypt($data);\necho \"encrypted data: \" . $encrypted;\n\n\/\/ encrypt with fixed iv (useful for unique values like emails)\n$encryptedwithsameiv = crypt::encrypt($data, true);\necho \"encrypted data with fixed iv: \" . $encryptedwithsameiv;\n<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<h3> \u89e3\u5bc6\u6570\u636e <\/h3>\n<p>\u8981\u89e3\u5bc6\u4e4b\u524d\u52a0\u5bc6\u7684\u6570\u636e\uff0c\u8bf7\u4f7f\u7528decrypt\u65b9\u6cd5\u3002\u60a8\u5fc5\u987b\u6307\u5b9a\u52a0\u5bc6\u671f\u95f4\u4f7f\u7528\u7684\u76f8\u540c\u53c2\u6570\u4ee5\u786e\u4fdd\u6b63\u786e\u89e3\u5bc6\uff1a<\/p>\n<pre>use lithesupportsecuritycrypt;\n\n\/\/ decrypt without fixed iv\n$decrypted = crypt::decrypt($encrypted);\necho \"decrypted data: \" . $decrypted;\n\n\/\/ decrypt with fixed iv\n$decryptedwithsameiv = crypt::decrypt($encryptedwithsameiv, true, $data);\necho \"decrypted data with fixed iv: \" . $decryptedwithsameiv;\n<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<h3> \u5f02\u5e38\u5904\u7406 <\/h3>\n<p>\u5982\u679c app_key \u672a\u8bbe\u7f6e\u6216\u65e0\u6548\uff0ccrypt \u7c7b\u5c06\u629b\u51fa cryptexception\u3002\u5fc5\u987b\u5728\u4ee3\u7801\u4e2d\u5904\u7406\u6b64\u5f02\u5e38\u4ee5\u907f\u514d\u610f\u5916\u9519\u8bef\uff1a<\/p>\n<pre>use LitheExceptionsEncryptionCryptException;\n\ntry {\n    $encrypted = Crypt::encrypt($data);\n    \/\/ Decrypt without fixed IV\n    $decrypted = Crypt::decrypt($encrypted);\n} catch (CryptException $e) {\n    echo \"Encryption Error: \" . $e-&gt;getMessage();\n}\n<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<h2> \u6700\u540e\u7684\u8003\u8651\u56e0\u7d20 <\/h2>\n<p><strong>lithe crypt<\/strong> \u63d0\u4f9b\u4e86\u4e00\u79cd\u5b9e\u7528\u4e14\u5b89\u5168\u7684\u65b9\u6cd5\u6765\u5904\u7406 php \u5e94\u7528\u7a0b\u5e8f\u4e2d\u7684\u6570\u636e\u52a0\u5bc6\u548c\u89e3\u5bc6\u3002\u901a\u8fc7\u5b9e\u65bd aes-256-cbc \u7b97\u6cd5\u5e76\u4e0e lithe \u6846\u67b6\u8f7b\u677e\u96c6\u6210\uff0c\u60a8\u53ef\u4ee5\u6709\u6548\u4fdd\u62a4\u60a8\u7684\u6570\u636e\u3002\u5c1d\u8bd5\u4e00\u4e0b\uff0c\u770b\u770b\u5b83\u5982\u4f55\u589e\u5f3a\u5e94\u7528\u7a0b\u5e8f\u7684\u5b89\u5168\u6027\uff01<\/p>\n<p>\u5982\u679c\u60a8\u6709\u4efb\u4f55\u7591\u95ee\u6216\u5efa\u8bae\uff0c\u8bf7\u968f\u65f6\u5728\u4e0b\u9762\u8bc4\u8bba\uff01<\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fLithe Crypt\uff1a\u7b80\u5316 PHP \u5e94\u7528\u7a0b\u5e8f\u4e2d\u7684\u52a0\u5bc6\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>lithe crypt \u662f\u4e00\u4e2a\u7b80\u5355\u7684 php \u52a0\u5bc6\u548c\u89e3\u5bc6\u5b9e\u7528\u7a0b\u5e8f\uff0c\u65e8\u5728\u4e0e lithe \u6846\u67b6\u914d\u5408\u4f7f\u7528\u3002\u5b83\u5229\u7528 aes-256-cbc \u7b97\u6cd5\u8fdb\u884c\u5b89\u5168\u6570\u636e\u5904\u7406\u3002 \u5b89\u88c5 \u8981\u5b89\u88c5 lithe crypt \u8f6f\u4ef6\u5305\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528 \u3002\u5982\u679c\u60a8\u5c1a\u672a\u5b89\u88c5\uff0c\u8bf7\u786e\u4fdd composer \u5728\u60a8\u7684\u7cfb\u7edf\u4e0a\u53ef\u7528\u3002\u7136\u540e\u5728\u9879\u76ee\u76ee\u5f55\u4e2d\u8fd0\u884c\u4ee5\u4e0b\u547d\u4ee4\uff1a composer require lithemod\/crypt \u767b\u5f55\u540e\u590d\u5236 \u8981\u6c42 php 8 \u6216\u66f4\u9ad8\u7248\u672c \u5728 php \u5b89\u88c5\u4e2d\u542f\u7528 openssl \u6269\u5c55 \u7528\u6cd5 \u52a0\u8f7d\u73af\u5883\u53d8\u91cf \u5728\u4f7f\u7528 crypt \u7c7b\u4e4b\u524d\uff0c\u60a8\u9700\u8981\u52a0\u8f7d\u73af\u5883\u53d8\u91cf\u3002\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\u52a0\u8f7d\u60a8\u7684 .env \u6587\u4ef6\uff1a use lithesupportenv; \/\/ load environment variables env::load(__dir__); \/\/ adjust the path as necessary \u767b\u5f55\u540e\u590d\u5236 \u8bbe\u7f6eapp_key \u786e\u4fdd\u5df2\u8bbe\u7f6e app_key \u73af\u5883\u53d8\u91cf\u3002\u8be5\u5bc6\u94a5\u5e94\u8be5\u662f [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-25967","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/25967","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=25967"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/25967\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=25967"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=25967"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=25967"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}