{"id":1077103,"date":"2025-01-08T11:59:13","date_gmt":"2025-01-08T03:59:13","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1077103.html"},"modified":"2025-01-08T11:59:30","modified_gmt":"2025-01-08T03:59:30","slug":"python%e5%ad%97%e5%85%b8%e5%af%b9%e7%85%a7%e8%a1%a8%e5%a6%82%e4%bd%95%e5%8a%a0%e5%af%86%e4%b8%ad%e6%96%87-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1077103.html","title":{"rendered":"python\u5b57\u5178\u5bf9\u7167\u8868\u5982\u4f55\u52a0\u5bc6\u4e2d\u6587"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24181336\/cabfa6ba-737f-45fb-825f-153baf506429.webp\" alt=\"python\u5b57\u5178\u5bf9\u7167\u8868\u5982\u4f55\u52a0\u5bc6\u4e2d\u6587\" \/><\/p>\n<p><p> <strong>Python\u5b57\u5178\u5bf9\u7167\u8868\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u6cd5\u6765\u52a0\u5bc6\u4e2d\u6587\uff0c\u4f8b\u5982\u901a\u8fc7\u54c8\u5e0c\u51fd\u6570\u3001\u5bf9\u79f0\u52a0\u5bc6\u7b97\u6cd5\u6216\u975e\u5bf9\u79f0\u52a0\u5bc6\u7b97\u6cd5\u7b49\u3002<\/strong>\u5176\u4e2d\uff0c\u4f7f\u7528\u5bf9\u79f0\u52a0\u5bc6\u7b97\u6cd5\uff08\u5982AES\uff09\u662f\u6bd4\u8f83\u5e38\u89c1\u4e14\u5b89\u5168\u7684\u65b9\u5f0f\u3002\u4e0b\u9762\u6211\u4eec\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u4e00\u79cd\u4f7f\u7528AES\u5bf9Python\u5b57\u5178\u4e2d\u7684\u4e2d\u6587\u8fdb\u884c\u52a0\u5bc6\u7684\u65b9\u6cd5\uff0c\u5e76\u89e3\u91ca\u5176\u4e2d\u7684\u6b65\u9aa4\u548c\u6ce8\u610f\u4e8b\u9879\u3002<\/p>\n<\/p>\n<p><p><strong>\u4e00\u3001\u5b89\u88c5\u5fc5\u8981\u7684\u5e93<\/strong><\/p>\n<\/p>\n<p><p>\u8981\u4f7f\u7528AES\u52a0\u5bc6\u7b97\u6cd5\uff0c\u9700\u8981\u5b89\u88c5<code>pycryptodome<\/code>\u5e93\u3002\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u547d\u4ee4\u8fdb\u884c\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install pycryptodome<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u4e8c\u3001\u751f\u6210\u5bc6\u94a5\u548c\u521d\u59cb\u5316\u5411\u91cf<\/strong><\/p>\n<\/p>\n<p><p>\u5728\u5bf9\u79f0\u52a0\u5bc6\u4e2d\uff0c\u5bc6\u94a5\u548c\u521d\u59cb\u5316\u5411\u91cf\uff08IV\uff09\u662f\u5fc5\u4e0d\u53ef\u5c11\u7684\u3002\u5bc6\u94a5\u7528\u4e8e\u52a0\u5bc6\u548c\u89e3\u5bc6\u6570\u636e\uff0c\u800cIV\u7528\u4e8e\u786e\u4fdd\u76f8\u540c\u7684\u660e\u6587\u5728\u4e0d\u540c\u7684\u52a0\u5bc6\u64cd\u4f5c\u4e2d\u4f1a\u4ea7\u751f\u4e0d\u540c\u7684\u5bc6\u6587\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from Crypto.Cipher import AES<\/p>\n<p>from Crypto.Util.Padding import pad, unpad<\/p>\n<p>from Crypto.Random import get_random_bytes<\/p>\n<h2><strong>\u751f\u6210\u5bc6\u94a5\u548c\u521d\u59cb\u5316\u5411\u91cf<\/strong><\/h2>\n<p>key = get_random_bytes(16)  # AES-128<\/p>\n<p>iv = get_random_bytes(16)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u4e09\u3001\u521b\u5efa\u52a0\u5bc6\u548c\u89e3\u5bc6\u51fd\u6570<\/strong><\/p>\n<\/p>\n<p><p>\u521b\u5efa\u4e24\u4e2a\u51fd\u6570\uff0c\u4e00\u4e2a\u7528\u4e8e\u52a0\u5bc6\u4e2d\u6587\u5b57\u7b26\u4e32\uff0c\u53e6\u4e00\u4e2a\u7528\u4e8e\u89e3\u5bc6\u5bc6\u6587\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def encrypt(text, key, iv):<\/p>\n<p>    cipher = AES.new(key, AES.MODE_CBC, iv)<\/p>\n<p>    padded_text = pad(text.encode(&#39;utf-8&#39;), AES.block_size)<\/p>\n<p>    encrypted_text = cipher.encrypt(padded_text)<\/p>\n<p>    return encrypted_text<\/p>\n<p>def decrypt(encrypted_text, key, iv):<\/p>\n<p>    cipher = AES.new(key, AES.MODE_CBC, iv)<\/p>\n<p>    decrypted_text = unpad(cipher.decrypt(encrypted_text), AES.block_size)<\/p>\n<p>    return decrypted_text.decode(&#39;utf-8&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u56db\u3001\u52a0\u5bc6\u5b57\u5178\u4e2d\u7684\u4e2d\u6587<\/strong><\/p>\n<\/p>\n<p><p>\u5047\u8bbe\u6211\u4eec\u6709\u4e00\u4e2a\u5305\u542b\u4e2d\u6587\u7684\u5b57\u5178\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u4e0a\u8ff0\u52a0\u5bc6\u51fd\u6570\u5bf9\u5176\u8fdb\u884c\u52a0\u5bc6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\u5b57\u5178<\/p>\n<p>data = {<\/p>\n<p>    &quot;name&quot;: &quot;\u5f20\u4e09&quot;,<\/p>\n<p>    &quot;address&quot;: &quot;\u5317\u4eac\u5e02\u6d77\u6dc0\u533a&quot;<\/p>\n<p>}<\/p>\n<h2><strong>\u52a0\u5bc6\u5b57\u5178<\/strong><\/h2>\n<p>encrypted_data = {}<\/p>\n<p>for key, value in data.items():<\/p>\n<p>    encrypted_data[key] = encrypt(value, key, iv)<\/p>\n<p>print(encrypted_data)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u4e94\u3001\u89e3\u5bc6\u5b57\u5178\u4e2d\u7684\u4e2d\u6587<\/strong><\/p>\n<\/p>\n<p><p>\u540c\u6837\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u89e3\u5bc6\u51fd\u6570\u5bf9\u52a0\u5bc6\u540e\u7684\u5b57\u5178\u8fdb\u884c\u89e3\u5bc6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u89e3\u5bc6\u5b57\u5178<\/p>\n<p>decrypted_data = {}<\/p>\n<p>for key, value in encrypted_data.items():<\/p>\n<p>    decrypted_data[key] = decrypt(value, key, iv)<\/p>\n<p>print(decrypted_data)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u516d\u3001\u6ce8\u610f\u4e8b\u9879<\/strong><\/p>\n<\/p>\n<ol>\n<li><strong>\u5bc6\u94a5\u7ba1\u7406<\/strong>\uff1a\u5bc6\u94a5\u5fc5\u987b\u5b89\u5168\u5730\u5b58\u50a8\u548c\u7ba1\u7406\uff0c\u5982\u679c\u5bc6\u94a5\u6cc4\u9732\uff0c\u6240\u6709\u7684\u52a0\u5bc6\u6570\u636e\u90fd\u4f1a\u53d8\u5f97\u4e0d\u5b89\u5168\u3002<\/li>\n<li><strong>\u521d\u59cb\u5316\u5411\u91cf<\/strong>\uff1aIV\u4e0d\u9700\u8981\u4fdd\u5bc6\uff0c\u4f46\u6bcf\u6b21\u52a0\u5bc6\u64cd\u4f5c\u90fd\u5e94\u8be5\u4f7f\u7528\u4e0d\u540c\u7684IV\uff0c\u4ee5\u786e\u4fdd\u76f8\u540c\u7684\u660e\u6587\u4e0d\u4f1a\u4ea7\u751f\u76f8\u540c\u7684\u5bc6\u6587\u3002<\/li>\n<li><strong>\u586b\u5145\u65b9\u5f0f<\/strong>\uff1a\u5bf9\u4e8eAES\u52a0\u5bc6\uff0c\u660e\u6587\u6570\u636e\u7684\u957f\u5ea6\u5fc5\u987b\u662f\u5757\u5927\u5c0f\u7684\u500d\u6570\uff0c\u6240\u4ee5\u9700\u8981\u8fdb\u884c\u586b\u5145\u3002\u89e3\u5bc6\u65f6\u4e5f\u9700\u8981\u53bb\u9664\u586b\u5145\u3002<\/li>\n<\/ol>\n<p><p><strong>\u4e03\u3001\u603b\u7ed3<\/strong><\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u4f7f\u7528AES\u52a0\u5bc6\u7b97\u6cd5\uff0c\u6211\u4eec\u53ef\u4ee5\u6709\u6548\u5730\u4fdd\u62a4Python\u5b57\u5178\u4e2d\u7684\u4e2d\u6587\u6570\u636e\u3002<strong>\u8fd9\u79cd\u65b9\u6cd5\u4e0d\u4ec5\u9002\u7528\u4e8e\u4e2d\u6587\u6570\u636e\uff0c\u8fd8\u53ef\u4ee5\u7528\u4e8e\u4efb\u4f55\u9700\u8981\u52a0\u5bc6\u7684\u654f\u611f\u4fe1\u606f<\/strong>\u3002\u867d\u7136\u52a0\u5bc6\u548c\u89e3\u5bc6\u8fc7\u7a0b\u53ef\u80fd\u4f1a\u589e\u52a0\u7cfb\u7edf\u7684\u590d\u6742\u6027\u548c\u8ba1\u7b97\u5f00\u9500\uff0c\u4f46\u8fd9\u662f\u786e\u4fdd\u6570\u636e\u5b89\u5168\u7684\u91cd\u8981\u63aa\u65bd\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u5b57\u5178\u4e2d\u5904\u7406\u4e2d\u6587\u5b57\u7b26\u7684\u52a0\u5bc6\uff1f<\/strong><br \/>\u5728Python\u4e2d\u5904\u7406\u4e2d\u6587\u5b57\u7b26\u7684\u52a0\u5bc6\u901a\u5e38\u6d89\u53ca\u5230\u5b57\u7b26\u7f16\u7801\u548c\u52a0\u5bc6\u7b97\u6cd5\u3002\u53ef\u4ee5\u4f7f\u7528<code>pycryptodome<\/code>\u5e93\u8fdb\u884c\u52a0\u5bc6\uff0c\u9996\u5148\u786e\u4fdd\u5c06\u4e2d\u6587\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u5b57\u8282\u683c\u5f0f\uff0c\u7136\u540e\u518d\u8fdb\u884c\u52a0\u5bc6\u3002\u89e3\u5bc6\u65f6\u9700\u8981\u5c06\u5b57\u8282\u683c\u5f0f\u8f6c\u6362\u56de\u4e2d\u6587\u5b57\u7b26\u4e32\u3002\u5177\u4f53\u5b9e\u73b0\u53ef\u4ee5\u53c2\u8003\u4ee5\u4e0b\u6b65\u9aa4\uff1a\u9009\u62e9\u5408\u9002\u7684\u52a0\u5bc6\u7b97\u6cd5\uff08\u5982AES\uff09\uff0c\u5c06\u4e2d\u6587\u5b57\u7b26\u4e32\u7f16\u7801\u4e3aUTF-8\u5b57\u8282\uff0c\u8fdb\u884c\u52a0\u5bc6\uff0c\u6700\u540e\u89e3\u5bc6\u5e76\u8f6c\u6362\u56de\u4e2d\u6587\u3002<\/p>\n<p><strong>\u52a0\u5bc6Python\u5b57\u5178\u4e2d\u7684\u4e2d\u6587\u5185\u5bb9\u6709\u54ea\u4e9b\u5e38\u7528\u7684\u65b9\u6cd5\uff1f<\/strong><br \/>\u5e38\u89c1\u7684\u52a0\u5bc6\u65b9\u6cd5\u5305\u62ec\u5bf9\u79f0\u52a0\u5bc6\uff08\u5982AES\u3001DES\u7b49\uff09\u548c\u975e\u5bf9\u79f0\u52a0\u5bc6\uff08\u5982RSA\uff09\u3002\u5bf9\u79f0\u52a0\u5bc6\u9002\u5408\u4e8e\u5927\u89c4\u6a21\u6570\u636e\u7684\u52a0\u5bc6\uff0c\u4f8b\u5982\u5b57\u5178\u4e2d\u7684\u4e2d\u6587\u6587\u672c\u3002\u975e\u5bf9\u79f0\u52a0\u5bc6\u5219\u9002\u5408\u4e8e\u5bc6\u94a5\u7684\u5b89\u5168\u4f20\u8f93\u3002\u53ef\u4ee5\u5229\u7528<code>cryptography<\/code>\u5e93\u6216<code>pycryptodome<\/code>\u5e93\u6765\u5b9e\u73b0\u8fd9\u4e9b\u7b97\u6cd5\uff0c\u5177\u4f53\u9009\u62e9\u53d6\u51b3\u4e8e\u5e94\u7528\u573a\u666f\u548c\u5b89\u5168\u9700\u6c42\u3002<\/p>\n<p><strong>\u5982\u4f55\u786e\u4fdd\u52a0\u5bc6\u540e\u7684Python\u5b57\u5178\u4e2d\u6587\u5185\u5bb9\u5b89\u5168\uff1f<\/strong><br \/>\u786e\u4fdd\u52a0\u5bc6\u5185\u5bb9\u5b89\u5168\u7684\u5173\u952e\u5728\u4e8e\u4f7f\u7528\u5f3a\u52a0\u5bc6\u7b97\u6cd5\u548c\u7ba1\u7406\u5bc6\u94a5\u3002\u9009\u62e9\u81f3\u5c11128\u4f4d\u7684\u52a0\u5bc6\u7b97\u6cd5\uff0c\u5982AES\uff0c\u5e76\u4fdd\u6301\u52a0\u5bc6\u5bc6\u94a5\u7684\u673a\u5bc6\u6027\u3002\u6b64\u5916\uff0c\u5b9a\u671f\u66f4\u6362\u5bc6\u94a5\u548c\u5ba1\u67e5\u52a0\u5bc6\u65b9\u6cd5\u7684\u5b89\u5168\u6027\u4e5f\u662f\u4fdd\u969c\u6570\u636e\u5b89\u5168\u7684\u91cd\u8981\u63aa\u65bd\u3002\u53ef\u4ee5\u901a\u8fc7\u5b9e\u73b0\u65e5\u5fd7\u8bb0\u5f55\u548c\u76d1\u63a7\u673a\u5236\u6765\u8fdb\u4e00\u6b65\u786e\u4fdd\u6570\u636e\u672a\u88ab\u672a\u6388\u6743\u8bbf\u95ee\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u5b57\u5178\u5bf9\u7167\u8868\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u6cd5\u6765\u52a0\u5bc6\u4e2d\u6587\uff0c\u4f8b\u5982\u901a\u8fc7\u54c8\u5e0c\u51fd\u6570\u3001\u5bf9\u79f0\u52a0\u5bc6\u7b97\u6cd5\u6216\u975e\u5bf9\u79f0\u52a0\u5bc6\u7b97\u6cd5\u7b49\u3002\u5176\u4e2d\uff0c\u4f7f\u7528 [&hellip;]","protected":false},"author":3,"featured_media":1077130,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1077103"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=1077103"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1077103\/revisions"}],"predecessor-version":[{"id":1077143,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1077103\/revisions\/1077143"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1077130"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1077103"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1077103"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1077103"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}