{"id":2727,"date":"2025-06-03T17:44:49","date_gmt":"2025-06-04T09:48:48","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=2727"},"modified":"2025-06-04T17:44:49","modified_gmt":"2025-06-04T09:48:48","slug":"%e4%bd%bf%e7%94%a8python%e5%bb%ba%e7%ab%8b%e6%aa%94%e6%a1%88%e5%8a%a0%e5%af%86%e7%a8%8b%e5%bc%8f%ef%bc%9a%e4%b8%80%e6%ad%a5%e4%b8%80%e6%ad%a5%e6%8c%87%e5%8d%97","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/steven\/python\/%e4%bd%bf%e7%94%a8python%e5%bb%ba%e7%ab%8b%e6%aa%94%e6%a1%88%e5%8a%a0%e5%af%86%e7%a8%8b%e5%bc%8f%ef%bc%9a%e4%b8%80%e6%ad%a5%e4%b8%80%e6%ad%a5%e6%8c%87%e5%8d%97\/","title":{"rendered":"\u4f7f\u7528 Python \u5efa\u7acb\u6a94\u6848\u52a0\u5bc6\u7a0b\u5f0f\u7684\u5b8c\u6574\u6307\u5357\uff082025 \u6700\u65b0\u7248\u672c\uff09"},"content":{"rendered":"<p><meta name=\"keywords\" content=\"Python, \u6a94\u6848\u52a0\u5bc6\u7a0b\u5f0f, \u6a94\u6848\u4fdd\u8b77, \u4e00\u6b65\u4e00\u6b65\u6307\u5357\"><\/p>\n<h1>\u4f7f\u7528 Python \u5efa\u7acb\u6a94\u6848\u52a0\u5bc6\u7a0b\u5f0f\u7684\u5b8c\u6574\u6307\u5357\uff082025 \u6700\u65b0\u7248\u672c\uff09<\/h1>\n<p>\u5728\u6578\u4f4d\u5316\u7684\u6642\u4ee3\uff0c\u6a94\u6848\u5b89\u5168\u6027\u8b8a\u5f97\u8d8a\u4f86\u8d8a\u91cd\u8981\u3002\u5982\u679c\u4f60\u60f3\u8981\u4fdd\u8b77\u4f60\u7684\u6a94\u6848\u4e0d\u88ab\u672a\u7d93\u6388\u6b0a\u7684\u8a2a\u554f\uff0c\u4f7f\u7528 Python \u5efa\u7acb\u4e00\u500b\u6a94\u6848\u52a0\u5bc6\u7a0b\u5f0f\u662f\u500b\u4e0d\u932f\u7684\u9078\u64c7\u3002\u672c\u6587\u5c07\u9010\u6b65\u5f15\u5c0e\u4f60\u5982\u4f55\u4f7f\u7528 Python \u4f86\u5be6\u73fe\u6a94\u6848\u7684\u52a0\u5bc6\u548c\u89e3\u5bc6\uff0c\u4e26\u63d0\u4f9b\u6700\u4f73\u5be6\u8e10\u8207\u5be6\u4f5c\u7bc4\u4f8b\u3002<\/p>\n<h2>\u7b2c\u4e00\u6b65\uff1a\u5b89\u88dd Python<\/h2>\n<p>\u9996\u5148\uff0c\u4f60\u9700\u8981\u5b89\u88dd Python\u3002\u8acb\u524d\u5f80 [Python \u5b98\u65b9\u7db2\u7ad9](https:\/\/www.python.org\/downloads\/) \u4e0b\u8f09 2025 \u5e74\u6700\u65b0\u7248\u672c\u7684 Python\uff0c\u4e26\u6309\u7167\u6307\u793a\u5b89\u88dd\u3002\u5b89\u88dd\u5b8c\u6210\u5f8c\uff0c\u5efa\u8b70\u5728\u7d42\u7aef\u6a5f\u4e2d\u8f38\u5165\u4ee5\u4e0b\u547d\u4ee4\u4f86\u78ba\u8a8d\u5b89\u88dd\u6210\u529f\uff1a<\/p>\n<p>&#8220;`bash<br \/>\npython &#8211;version<br \/>\n&#8220;`<\/p>\n<h2>\u7b2c\u4e8c\u6b65\uff1a\u5efa\u7acb\u6a94\u6848\u52a0\u5bc6\u7a0b\u5f0f<\/h2>\n<p>\u63a5\u4e0b\u4f86\uff0c\u6211\u5011\u5c07\u5efa\u7acb\u4e00\u500b\u6a94\u6848\u52a0\u5bc6\u7a0b\u5f0f\u3002\u9019\u88e1\u5c07\u4f7f\u7528 `cryptography` \u51fd\u5f0f\u5eab\uff0c\u9019\u662f Python \u4e2d\u4e00\u500b\u975e\u5e38\u53d7\u6b61\u8fce\u7684\u52a0\u5bc6\u5eab\uff0c\u80fd\u5920\u63d0\u4f9b\u5f37\u5927\u7684\u52a0\u5bc6\u529f\u80fd\u3002\u8acb\u5148\u5b89\u88dd\u8a72\u5eab\uff1a<\/p>\n<p>&#8220;`bash<br \/>\npip install cryptography<br \/>\n&#8220;`<\/p>\n<p>\u7136\u5f8c\uff0c\u4f7f\u7528\u4ee5\u4e0b\u7a0b\u5f0f\u78bc\u5efa\u7acb\u6a94\u6848\u52a0\u5bc6\u548c\u89e3\u5bc6\u7684\u529f\u80fd\uff1a<\/p>\n<p>&#8220;`python<br \/>\nfrom cryptography.fernet import Fernet<br \/>\nimport os<\/p>\n<p># \u751f\u6210\u91d1\u9470<br \/>\ndef generate_key():<br \/>\n    key = Fernet.generate_key()<br \/>\n    with open(&#8220;secret.key&#8221;, &#8220;wb&#8221;) as key_file:<br \/>\n        key_file.write(key)<\/p>\n<p># \u8f09\u5165\u91d1\u9470<br \/>\ndef load_key():<br \/>\n    return open(&#8220;secret.key&#8221;, &#8220;rb&#8221;).read()<\/p>\n<p># \u52a0\u5bc6\u6a94\u6848<br \/>\ndef encrypt_file(filename):<br \/>\n    key = load_key()<br \/>\n    fernet = Fernet(key)<\/p>\n<p>    with open(filename, &#8216;rb&#8217;) as file:<br \/>\n        original = file.read()<\/p>\n<p>    encrypted = fernet.encrypt(original)<\/p>\n<p>    with open(filename, &#8216;wb&#8217;) as encrypted_file:<br \/>\n        encrypted_file.write(encrypted)<\/p>\n<p># \u89e3\u5bc6\u6a94\u6848<br \/>\ndef decrypt_file(filename):<br \/>\n    key = load_key()<br \/>\n    fernet = Fernet(key)<\/p>\n<p>    with open(filename, &#8216;rb&#8217;) as encrypted_file:<br \/>\n        encrypted = encrypted_file.read()<\/p>\n<p>    decrypted = fernet.decrypt(encrypted)<\/p>\n<p>    with open(filename, &#8216;wb&#8217;) as decrypted_file:<br \/>\n        decrypted_file.write(decrypted)<br \/>\n&#8220;`<\/p>\n<p>\u9019\u6bb5\u7a0b\u5f0f\u78bc\u4e2d\uff0c\u6211\u5011\u4f7f\u7528 `cryptography` \u5eab\u4f86\u751f\u6210\u91d1\u9470\u3001\u52a0\u5bc6\u548c\u89e3\u5bc6\u6a94\u6848\u3002\u78ba\u4fdd\u5728\u57f7\u884c\u4e4b\u524d\u5148\u751f\u6210\u91d1\u9470\uff1a<\/p>\n<p>&#8220;`python<br \/>\ngenerate_key()<br \/>\n&#8220;`<\/p>\n<h2>\u7b2c\u4e09\u6b65\uff1a\u57f7\u884c\u6a94\u6848\u52a0\u5bc6\u7a0b\u5f0f<\/h2>\n<p>\u73fe\u5728\u4f60\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u4f86\u52a0\u5bc6\u6216\u89e3\u5bc6\u6a94\u6848\u3002\u8acb\u78ba\u4fdd\u5c07 `yourfile.txt` \u66ff\u63db\u70ba\u4f60\u5be6\u969b\u8981\u52a0\u5bc6\u6216\u89e3\u5bc6\u7684\u6a94\u6848\u540d\u7a31\u3002<\/p>\n<p>&#8220;`bash<br \/>\n# \u52a0\u5bc6\u6a94\u6848<br \/>\npython encrypt_file.py yourfile.txt<\/p>\n<p># \u89e3\u5bc6\u6a94\u6848<br \/>\npython decrypt_file.py yourfile.txt<br \/>\n&#8220;`<\/p>\n<h2>\u932f\u8aa4\u6392\u9664<\/h2>\n<p>&#8211; **\u91d1\u9470\u672a\u627e\u5230\u932f\u8aa4**\uff1a\u8acb\u78ba\u4fdd `secret.key` \u6587\u4ef6\u5b58\u5728\u65bc\u76f8\u540c\u7684\u76ee\u9304\u4e2d\uff0c\u5426\u5247\u5c07\u7121\u6cd5\u89e3\u5bc6\u6a94\u6848\u3002<br \/>\n&#8211; **\u6a94\u6848\u683c\u5f0f\u932f\u8aa4**\uff1a\u8a72\u7a0b\u5f0f\u50c5\u9069\u7528\u65bc\u4e8c\u9032\u4f4d\u6a94\u6848\uff0c\u8acb\u78ba\u4fdd\u52a0\u5bc6\u7684\u6a94\u6848\u4e0d\u662f\u7a7a\u7684\u3002<\/p>\n<h2>\u5ef6\u4f38\u61c9\u7528<\/h2>\n<p>\u9664\u4e86\u57fa\u672c\u7684\u6a94\u6848\u52a0\u5bc6\u8207\u89e3\u5bc6\uff0c\u4f60\u9084\u53ef\u4ee5\u5c07\u9019\u4e9b\u529f\u80fd\u64f4\u5c55\u5230\u66f4\u8907\u96dc\u7684\u61c9\u7528\u4e2d\uff0c\u4f8b\u5982\uff1a<br \/>\n&#8211; \u70ba\u4e0d\u540c\u7684\u6a94\u6848\u4f7f\u7528\u7368\u7279\u7684\u91d1\u9470\u3002<br \/>\n&#8211; \u6574\u5408\u4f7f\u7528\u8005\u4ecb\u9762\u4f86\u7c21\u5316\u64cd\u4f5c\u6d41\u7a0b\u3002<br \/>\n&#8211; \u5c07\u52a0\u5bc6\u6a94\u6848\u4e0a\u50b3\u81f3\u96f2\u7aef\u5132\u5b58\u670d\u52d9\u3002<\/p>\n<p>\u5728\u672c\u6587\u4e2d\uff0c\u6211\u5011\u4ecb\u7d39\u4e86\u5982\u4f55\u4f7f\u7528 Python \u5efa\u7acb\u4e00\u500b\u6a94\u6848\u52a0\u5bc6\u7a0b\u5f0f\uff0c\u8b93\u4f60\u53ef\u4ee5\u6709\u6548\u4fdd\u8b77\u4f60\u7684\u6a94\u6848\u3002\u96a8\u8457\u79d1\u6280\u7684\u4e0d\u65b7\u9032\u6b65\uff0c\u5efa\u8b70\u4f60\u5b9a\u671f\u66f4\u65b0\u548c\u512a\u5316\u4f60\u7684\u52a0\u5bc6\u7a0b\u5f0f\uff0c\u4ee5\u78ba\u4fdd\u6a94\u6848\u5b89\u5168\u3002<\/p>\n<p>\u5982\u9700\u66f4\u6df1\u5165\u7684 Python \u6559\u5b78\u8207\u8cc7\u6e90\uff0c\u8acb\u53c3\u8003 [\u6211\u7684 Vocus \u6559\u5b78\u6587](https:\/\/vocus.cc)\u3002<\/p>\n<h2>Q&#038;A\uff08\u5e38\u898b\u554f\u984c\u89e3\u7b54\uff09<\/h2>\n<p>**Q: \u70ba\u4ec0\u9ebc\u9700\u8981\u4f7f\u7528\u52a0\u5bc6\u4f86\u4fdd\u8b77\u6a94\u6848\uff1f**<br \/>\nA: \u52a0\u5bc6\u53ef\u4ee5\u9632\u6b62\u672a\u7d93\u6388\u6b0a\u7684\u4f7f\u7528\u8005\u8a2a\u554f\u4f60\u7684\u654f\u611f\u8cc7\u6599\uff0c\u4fdd\u969c\u8cc7\u8a0a\u7684\u5b89\u5168\u6027\u3002<\/p>\n<p>**Q: \u5982\u4f55\u78ba\u4fdd\u6211\u7684\u91d1\u9470\u5b89\u5168\uff1f**<br \/>\nA: \u8acb\u5c07\u91d1\u9470\u5132\u5b58\u5728\u5b89\u5168\u7684\u4f4d\u7f6e\uff0c\u4e26\u907f\u514d\u5c07\u5176\u8207\u52a0\u5bc6\u6a94\u6848\u653e\u5728\u4e00\u8d77\u3002<\/p>\n<p>**Q: \u53ef\u4ee5\u4f7f\u7528\u54ea\u4e9b\u5176\u4ed6\u51fd\u5f0f\u5eab\u4f86\u52a0\u5bc6\u6a94\u6848\uff1f**<br \/>\nA: \u9664\u4e86 `cryptography`\uff0c\u4f60\u9084\u53ef\u4ee5\u8003\u616e\u4f7f\u7528 `PyCrypto` \u6216 `PyCryptodome` \u7b49\u5eab\u4f86\u5be6\u73fe\u52a0\u5bc6\u529f\u80fd\u3002<br \/>\n&#8212;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5b78\u7fd2\u5982\u4f55\u4f7f\u7528Python\u5efa\u7acb\u6a94\u6848\u52a0\u5bc6\u7a0b\u5f0f\uff0c\u672c\u6587\u5c07\u63d0\u4f9b\u4e00\u6b65\u4e00\u6b65\u7684\u6307\u5357\uff0c\u8b93\u4f60\u53ef\u4ee5\u8f15\u9b06\u5efa\u7acb\u6a94\u6848\u52a0\u5bc6\u7a0b\u5f0f\uff0c\u8b93\u4f60\u7684\u6a94\u6848\u5b89\u5168\u7121\u865e\u3002<\/p>\n","protected":false},"author":1,"featured_media":2518,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[18],"tags":[15],"class_list":["post-2727","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python","tag-python"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"https:\/\/badgameshow.com\/steven\/wp-content\/uploads\/2022\/12\/DALL\u00b7E-2022-12-28-14.25.55-\u62f7\u8c9d2.png","jetpack-related-posts":[],"jetpack_shortlink":"https:\/\/wp.me\/pcFK27-HZ","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/2727","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/comments?post=2727"}],"version-history":[{"count":0,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/2727\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media\/2518"}],"wp:attachment":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media?parent=2727"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=2727"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=2727"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}