{"id":1114125,"date":"2025-01-08T17:54:15","date_gmt":"2025-01-08T09:54:15","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1114125.html"},"modified":"2025-01-08T17:54:18","modified_gmt":"2025-01-08T09:54:18","slug":"python%e5%a6%82%e4%bd%95%e8%a7%a3%e5%af%86%e4%b8%80%e6%ae%b5%e5%af%86%e6%96%87","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1114125.html","title":{"rendered":"Python\u5982\u4f55\u89e3\u5bc6\u4e00\u6bb5\u5bc6\u6587"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25075440\/fd301106-253c-436e-a61f-39d95a2537dd.webp\" alt=\"Python\u5982\u4f55\u89e3\u5bc6\u4e00\u6bb5\u5bc6\u6587\" \/><\/p>\n<p><p> <strong>Python\u89e3\u5bc6\u4e00\u6bb5\u5bc6\u6587\u7684\u65b9\u6cd5\u6709\u5f88\u591a<\/strong>\uff0c\u4f8b\u5982\u4f7f\u7528\u5185\u7f6e\u7684base64\u6a21\u5757\u3001cryptography\u5e93\u3001PyCrypto\u5e93\u7b49\u3002 <strong>\u5e38\u89c1\u7684\u89e3\u5bc6\u65b9\u5f0f\u5305\u62ec\u5bf9\u79f0\u52a0\u5bc6\u89e3\u5bc6\u3001\u975e\u5bf9\u79f0\u52a0\u5bc6\u89e3\u5bc6\u4ee5\u53ca\u54c8\u5e0c\u89e3\u5bc6<\/strong>\u3002\u5176\u4e2d\uff0c\u5bf9\u79f0\u52a0\u5bc6\u89e3\u5bc6\u6700\u5e38\u7528\u7684\u662fAES\u3001DES\u7b49\u7b97\u6cd5\uff0c\u975e\u5bf9\u79f0\u52a0\u5bc6\u89e3\u5bc6\u5e38\u7528RSA\u7b97\u6cd5\u3002\u63a5\u4e0b\u6765\uff0c\u6211\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528PyCrypto\u5e93\u5b9e\u73b0AES\u5bf9\u79f0\u52a0\u5bc6\u548c\u89e3\u5bc6\u3002<\/p>\n<\/p>\n<p><h2>\u4e00\u3001PyCrypto\u5e93\u7684\u5b89\u88c5\u4e0e\u57fa\u7840\u4ecb\u7ecd<\/h2>\n<\/p>\n<p><p>PyCrypto\u662fPython\u4e2d\u4e00\u4e2a\u975e\u5e38\u5f3a\u5927\u7684\u52a0\u5bc6\u548c\u89e3\u5bc6\u5e93\u3002\u5b83\u652f\u6301\u591a\u79cd\u52a0\u5bc6\u7b97\u6cd5\uff0c\u5305\u62ecAES\u3001DES\u3001RSA\u7b49\u3002\u5b89\u88c5PyCrypto\u975e\u5e38\u7b80\u5355\uff0c\u53ea\u9700\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-sh\">pip install pycryptodome<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5b89\u88c5\u5b8c\u6210\u540e\uff0c\u6211\u4eec\u5c31\u53ef\u4ee5\u5f00\u59cb\u4f7f\u7528PyCrypto\u8fdb\u884c\u52a0\u5bc6\u548c\u89e3\u5bc6\u64cd\u4f5c\u4e86\u3002<\/p>\n<\/p>\n<p><h2>\u4e8c\u3001AES\u5bf9\u79f0\u52a0\u5bc6\u548c\u89e3\u5bc6<\/h2>\n<\/p>\n<p><h3>1\u3001\u751f\u6210\u5bc6\u94a5\u548c\u521d\u59cb\u5316\u5411\u91cf<\/h3>\n<\/p>\n<p><p>\u5728AES\u5bf9\u79f0\u52a0\u5bc6\u4e2d\uff0c\u5bc6\u94a5\u548c\u521d\u59cb\u5316\u5411\u91cf\uff08IV\uff09\u662f\u975e\u5e38\u91cd\u8981\u7684\u4e24\u4e2a\u53c2\u6570\u3002\u5bc6\u94a5\u7528\u4e8e\u52a0\u5bc6\u548c\u89e3\u5bc6\u6570\u636e\uff0c\u800cIV\u5219\u7528\u4e8e\u786e\u4fdd\u76f8\u540c\u7684\u660e\u6587\u5728\u6bcf\u6b21\u52a0\u5bc6\u65f6\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.Random import get_random_bytes<\/p>\n<p>key = get_random_bytes(16)  # \u751f\u6210\u4e00\u4e2a16\u5b57\u8282\u7684\u5bc6\u94a5<\/p>\n<p>iv = get_random_bytes(16)   # \u751f\u6210\u4e00\u4e2a16\u5b57\u8282\u7684IV<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>2\u3001\u52a0\u5bc6\u6570\u636e<\/h3>\n<\/p>\n<p><p>\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528AES\u7684CBC\u6a21\u5f0f\u6765\u52a0\u5bc6\u6570\u636e\u3002CBC\u6a21\u5f0f\u9700\u8981\u4e00\u4e2a\u5757\u5927\u5c0f\u4e3a16\u5b57\u8282\u7684\u660e\u6587\uff0c\u5982\u679c\u660e\u6587\u7684\u957f\u5ea6\u4e0d\u662f16\u7684\u500d\u6570\uff0c\u6211\u4eec\u9700\u8981\u5bf9\u5176\u8fdb\u884c\u586b\u5145\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from Crypto.Util.Padding import pad<\/p>\n<p>cipher = AES.new(key, AES.MODE_CBC, iv)<\/p>\n<p>data = b&quot;Hello, this is a secret message!&quot;<\/p>\n<p>padded_data = pad(data, AES.block_size)  # \u586b\u5145\u6570\u636e<\/p>\n<p>ciphertext = cipher.encrypt(padded_data)  # \u52a0\u5bc6\u6570\u636e<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>3\u3001\u89e3\u5bc6\u6570\u636e<\/h3>\n<\/p>\n<p><p>\u89e3\u5bc6\u6570\u636e\u7684\u8fc7\u7a0b\u4e0e\u52a0\u5bc6\u6570\u636e\u76f8\u53cd\u3002\u6211\u4eec\u9700\u8981\u4f7f\u7528\u76f8\u540c\u7684\u5bc6\u94a5\u548cIV\uff0c\u5e76\u5728\u89e3\u5bc6\u540e\u5bf9\u6570\u636e\u8fdb\u884c\u89e3\u586b\u5145\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from Crypto.Util.Padding import unpad<\/p>\n<p>cipher = AES.new(key, AES.MODE_CBC, iv)<\/p>\n<p>decrypted_data = cipher.decrypt(ciphertext)  # \u89e3\u5bc6\u6570\u636e<\/p>\n<p>unpadded_data = unpad(decrypted_data, AES.block_size)  # \u89e3\u586b\u5145\u6570\u636e<\/p>\n<p>print(unpadded_data.decode())  # \u8f93\u51fa\u89e3\u5bc6\u540e\u7684\u660e\u6587<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u4e09\u3001RSA\u975e\u5bf9\u79f0\u52a0\u5bc6\u548c\u89e3\u5bc6<\/h2>\n<\/p>\n<p><h3>1\u3001\u751f\u6210\u516c\u94a5\u548c\u79c1\u94a5<\/h3>\n<\/p>\n<p><p>\u5728RSA\u975e\u5bf9\u79f0\u52a0\u5bc6\u4e2d\uff0c\u6211\u4eec\u9700\u8981\u751f\u6210\u4e00\u5bf9\u516c\u94a5\u548c\u79c1\u94a5\u3002\u516c\u94a5\u7528\u4e8e\u52a0\u5bc6\u6570\u636e\uff0c\u79c1\u94a5\u7528\u4e8e\u89e3\u5bc6\u6570\u636e\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from Crypto.PublicKey import RSA<\/p>\n<p>key = RSA.generate(2048)<\/p>\n<p>private_key = key.export_key()<\/p>\n<p>public_key = key.publickey().export_key()<\/p>\n<p>with open(&quot;private.pem&quot;, &quot;wb&quot;) as f:<\/p>\n<p>    f.write(private_key)<\/p>\n<p>with open(&quot;public.pem&quot;, &quot;wb&quot;) as f:<\/p>\n<p>    f.write(public_key)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>2\u3001\u52a0\u5bc6\u6570\u636e<\/h3>\n<\/p>\n<p><p>\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u516c\u94a5\u52a0\u5bc6\u6570\u636e\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from Crypto.Cipher import PKCS1_OAEP<\/p>\n<p>with open(&quot;public.pem&quot;, &quot;rb&quot;) as f:<\/p>\n<p>    public_key = RSA.import_key(f.read())<\/p>\n<p>cipher_rsa = PKCS1_OAEP.new(public_key)<\/p>\n<p>data = b&quot;Hello, this is a secret message!&quot;<\/p>\n<p>ciphertext = cipher_rsa.encrypt(data)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>3\u3001\u89e3\u5bc6\u6570\u636e<\/h3>\n<\/p>\n<p><p>\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u79c1\u94a5\u89e3\u5bc6\u6570\u636e\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">with open(&quot;private.pem&quot;, &quot;rb&quot;) as f:<\/p>\n<p>    private_key = RSA.import_key(f.read())<\/p>\n<p>cipher_rsa = PKCS1_OAEP.new(private_key)<\/p>\n<p>decrypted_data = cipher_rsa.decrypt(ciphertext)<\/p>\n<p>print(decrypted_data.decode())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u56db\u3001\u603b\u7ed3<\/h2>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u793a\u4f8b\uff0c\u6211\u4eec\u53ef\u4ee5\u770b\u5230\uff0c\u4f7f\u7528PyCrypto\u5e93\u8fdb\u884c\u52a0\u5bc6\u548c\u89e3\u5bc6\u64cd\u4f5c\u662f\u975e\u5e38\u7b80\u5355\u548c\u76f4\u89c2\u7684\u3002<strong>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u9009\u62e9\u5408\u9002\u7684\u52a0\u5bc6\u7b97\u6cd5\u548c\u6a21\u5f0f\u975e\u5e38\u91cd\u8981<\/strong>\u3002\u5bf9\u4e8e\u9700\u8981\u9ad8\u5b89\u5168\u6027\u7684\u573a\u666f\uff0c\u53ef\u4ee5\u9009\u62e9AES\u5bf9\u79f0\u52a0\u5bc6\uff0c\u5bf9\u4e8e\u9700\u8981\u516c\u94a5\u5206\u53d1\u7684\u573a\u666f\uff0c\u53ef\u4ee5\u9009\u62e9RSA\u975e\u5bf9\u79f0\u52a0\u5bc6\u3002\u5e0c\u671b\u8fd9\u7bc7\u6587\u7ae0\u5bf9\u4f60\u4e86\u89e3\u548c\u4f7f\u7528Python\u8fdb\u884c\u5bc6\u6587\u89e3\u5bc6\u6709\u6240\u5e2e\u52a9\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u9009\u62e9\u5408\u9002\u7684\u89e3\u5bc6\u7b97\u6cd5\uff1f<\/strong><br \/>\u5728\u89e3\u5bc6\u5bc6\u6587\u65f6\uff0c\u9009\u62e9\u5408\u9002\u7684\u89e3\u5bc6\u7b97\u6cd5\u81f3\u5173\u91cd\u8981\u3002\u4e0d\u540c\u7684\u52a0\u5bc6\u7b97\u6cd5\u4f7f\u7528\u4e0d\u540c\u7684\u5bc6\u94a5\u548c\u89e3\u5bc6\u65b9\u6cd5\u3002\u4f8b\u5982\uff0cAES\u548cDES\u662f\u5e38\u89c1\u7684\u5bf9\u79f0\u52a0\u5bc6\u7b97\u6cd5\uff0c\u800cRSA\u5219\u662f\u975e\u5bf9\u79f0\u52a0\u5bc6\u7b97\u6cd5\u3002\u9700\u8981\u786e\u4fdd\u60a8\u77e5\u9053\u5bc6\u6587\u662f\u4f7f\u7528\u54ea\u79cd\u7b97\u6cd5\u52a0\u5bc6\u7684\uff0c\u5e76\u83b7\u53d6\u76f8\u5e94\u7684\u89e3\u5bc6\u5bc6\u94a5\u3002<\/p>\n<p><strong>\u9700\u8981\u54ea\u4e9b\u5e93\u6765\u6267\u884c\u89e3\u5bc6\u64cd\u4f5c\uff1f<\/strong><br \/>Python\u4e2d\u6709\u591a\u4e2a\u5e93\u53ef\u4ee5\u5e2e\u52a9\u60a8\u6267\u884c\u89e3\u5bc6\u64cd\u4f5c\u3002\u5e38\u7528\u7684\u5e93\u5305\u62ec<code>pycryptodome<\/code>\u548c<code>cryptography<\/code>\u3002\u8fd9\u4e9b\u5e93\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u529f\u80fd\uff0c\u652f\u6301\u591a\u79cd\u52a0\u5bc6\u548c\u89e3\u5bc6\u7b97\u6cd5\uff0c\u60a8\u53ef\u4ee5\u6839\u636e\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u5e93\u6765\u5b9e\u73b0\u89e3\u5bc6\u3002<\/p>\n<p><strong>\u5982\u4f55\u5904\u7406\u89e3\u5bc6\u8fc7\u7a0b\u4e2d\u51fa\u73b0\u7684\u9519\u8bef\uff1f<\/strong><br \/>\u5728\u89e3\u5bc6\u5bc6\u6587\u65f6\uff0c\u53ef\u80fd\u4f1a\u9047\u5230\u5404\u79cd\u9519\u8bef\uff0c\u4f8b\u5982\u5bc6\u94a5\u4e0d\u5339\u914d\u6216\u683c\u5f0f\u9519\u8bef\u7b49\u3002\u4e3a\u4e86\u5904\u7406\u8fd9\u4e9b\u9519\u8bef\uff0c\u5efa\u8bae\u5728\u89e3\u5bc6\u4ee3\u7801\u4e2d\u4f7f\u7528\u5f02\u5e38\u5904\u7406\u673a\u5236\uff08\u5982try-except\u8bed\u53e5\uff09\uff0c\u4ee5\u4fbf\u6355\u83b7\u5e76\u5904\u7406\u53ef\u80fd\u51fa\u73b0\u7684\u9519\u8bef\uff0c\u4ece\u800c\u63d0\u9ad8\u7a0b\u5e8f\u7684\u5065\u58ee\u6027\u548c\u7528\u6237\u4f53\u9a8c\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u89e3\u5bc6\u4e00\u6bb5\u5bc6\u6587\u7684\u65b9\u6cd5\u6709\u5f88\u591a\uff0c\u4f8b\u5982\u4f7f\u7528\u5185\u7f6e\u7684base64\u6a21\u5757\u3001cryptography\u5e93\u3001PyCryp [&hellip;]","protected":false},"author":3,"featured_media":1114135,"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\/1114125"}],"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=1114125"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1114125\/revisions"}],"predecessor-version":[{"id":1114136,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1114125\/revisions\/1114136"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1114135"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1114125"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1114125"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1114125"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}