{"id":998747,"date":"2024-12-27T09:35:43","date_gmt":"2024-12-27T01:35:43","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/998747.html"},"modified":"2024-12-27T09:35:45","modified_gmt":"2024-12-27T01:35:45","slug":"python%e5%87%af%e6%92%92%e5%af%86%e7%a0%81%e5%a6%82%e4%bd%95%e7%bc%96%e5%86%99","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/998747.html","title":{"rendered":"python\u51ef\u6492\u5bc6\u7801\u5982\u4f55\u7f16\u5199"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25074043\/5a4256ef-5bfe-4ec0-8e9b-c70e99788ff5.webp\" alt=\"python\u51ef\u6492\u5bc6\u7801\u5982\u4f55\u7f16\u5199\" \/><\/p>\n<p><p> <strong>\u7f16\u5199Python\u51ef\u6492\u5bc6\u7801\u7684\u8fc7\u7a0b\u5305\u62ec\uff1a\u9009\u62e9\u5bc6\u94a5\u3001\u52a0\u5bc6\u7b97\u6cd5\u3001\u89e3\u5bc6\u7b97\u6cd5\u3001\u5904\u7406\u8f93\u5165\u8f93\u51fa\u3002\u9009\u62e9\u4e00\u4e2a\u9002\u5408\u7684\u5bc6\u94a5\u662f\u81f3\u5173\u91cd\u8981\u7684\uff0c\u56e0\u4e3a\u5bc6\u94a5\u51b3\u5b9a\u4e86\u6bcf\u4e2a\u5b57\u6bcd\u5728\u5b57\u6bcd\u8868\u4e2d\u79fb\u52a8\u7684\u4f4d\u6570\u3002\u52a0\u5bc6\u7b97\u6cd5\u901a\u8fc7\u5c06\u660e\u6587\u4e2d\u7684\u6bcf\u4e2a\u5b57\u6bcd\u504f\u79fb\u5bc6\u94a5\u6570\u6765\u751f\u6210\u5bc6\u6587\uff0c\u89e3\u5bc6\u7b97\u6cd5\u5219\u901a\u8fc7\u9006\u5411\u504f\u79fb\u5c06\u5bc6\u6587\u8fd8\u539f\u4e3a\u660e\u6587\u3002\u5904\u7406\u8f93\u5165\u8f93\u51fa\u5305\u62ec\u8bfb\u53d6\u7528\u6237\u8f93\u5165\u5e76\u8f93\u51fa\u52a0\u5bc6\u6216\u89e3\u5bc6\u540e\u7684\u6587\u672c\u3002<\/strong>\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5c06\u8be6\u7ec6\u63a2\u8ba8\u5982\u4f55\u5b9e\u73b0\u8fd9\u4e9b\u6b65\u9aa4\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u9009\u62e9\u5bc6\u94a5<\/p>\n<\/p>\n<p><p>\u5728\u51ef\u6492\u5bc6\u7801\u4e2d\uff0c\u5bc6\u94a5\u662f\u4e00\u4e2a\u6574\u6570\uff0c\u8868\u793a\u5b57\u6bcd\u8868\u4e2d\u5b57\u7b26\u7684\u4f4d\u79fb\u91cf\u3002\u9009\u62e9\u5bc6\u94a5\u65f6\uff0c\u5e94\u786e\u4fdd\u5b83\u57281\u523025\u4e4b\u95f4\uff0c\u56e0\u4e3a\u5b57\u6bcd\u8868\u670926\u4e2a\u5b57\u6bcd\u3002\u9009\u62e9\u5408\u9002\u7684\u5bc6\u94a5\u80fd\u591f\u589e\u52a0\u5bc6\u7801\u7684\u96be\u5ea6\uff0c\u907f\u514d\u88ab\u8f7b\u6613\u7834\u89e3\u3002<\/p>\n<\/p>\n<p><p>\u9009\u62e9\u5bc6\u94a5\u65f6\uff0c\u53ef\u4ee5\u901a\u8fc7\u6570\u5b66\u8ba1\u7b97\u6216\u968f\u673a\u6570\u751f\u6210\u5668\u6765\u9009\u53d6\u3002\u4f8b\u5982\uff0c\u4f7f\u7528Python\u7684<code>random<\/code>\u6a21\u5757\u53ef\u4ee5\u751f\u6210\u4e00\u4e2a\u968f\u673a\u5bc6\u94a5\u3002\u8fd9\u79cd\u65b9\u6cd5\u53ef\u4ee5\u5728\u4e00\u5b9a\u7a0b\u5ea6\u4e0a\u63d0\u9ad8\u5b89\u5168\u6027\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import random<\/p>\n<p>def generate_key():<\/p>\n<p>    return random.randint(1, 25)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e8c\u3001\u5b9e\u73b0\u52a0\u5bc6\u7b97\u6cd5<\/p>\n<\/p>\n<p><p>\u52a0\u5bc6\u7b97\u6cd5\u662f\u51ef\u6492\u5bc6\u7801\u7684\u6838\u5fc3\u3002\u57fa\u672c\u601d\u8def\u662f\u5c06\u660e\u6587\u4e2d\u7684\u6bcf\u4e2a\u5b57\u6bcd\u6839\u636e\u5bc6\u94a5\u8fdb\u884c\u4f4d\u79fb\u3002\u5bf9\u4e8e\u5b57\u6bcd\u8868\u4e4b\u5916\u7684\u5b57\u7b26\uff0c\u5982\u6570\u5b57\u6216\u6807\u70b9\u7b26\u53f7\uff0c\u5219\u4e0d\u8fdb\u884c\u52a0\u5bc6\u3002<\/p>\n<\/p>\n<p><p>\u4e0b\u9762\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u52a0\u5bc6\u51fd\u6570\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def encrypt(text, key):<\/p>\n<p>    encrypted_text = &quot;&quot;<\/p>\n<p>    for char in text:<\/p>\n<p>        if char.isalpha():<\/p>\n<p>            shift = key<\/p>\n<p>            if char.islower():<\/p>\n<p>                encrypted_text += chr((ord(char) - 97 + shift) % 26 + 97)<\/p>\n<p>            elif char.isupper():<\/p>\n<p>                encrypted_text += chr((ord(char) - 65 + shift) % 26 + 65)<\/p>\n<p>        else:<\/p>\n<p>            encrypted_text += char<\/p>\n<p>    return encrypted_text<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u51fd\u6570\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528<code>ord<\/code>\u51fd\u6570\u5c06\u5b57\u7b26\u8f6c\u6362\u4e3aASCII\u7801\uff0c\u518d\u901a\u8fc7\u6570\u5b66\u8fd0\u7b97\u5b8c\u6210\u5b57\u6bcd\u7684\u4f4d\u79fb\uff0c\u6700\u540e\u4f7f\u7528<code>chr<\/code>\u51fd\u6570\u5c06\u7ed3\u679c\u8f6c\u56de\u5b57\u7b26\u3002<\/p>\n<\/p>\n<p><p>\u4e09\u3001\u5b9e\u73b0\u89e3\u5bc6\u7b97\u6cd5<\/p>\n<\/p>\n<p><p>\u89e3\u5bc6\u7b97\u6cd5\u662f\u52a0\u5bc6\u7b97\u6cd5\u7684\u9006\u8fc7\u7a0b\u3002\u901a\u8fc7\u5c06\u5bc6\u6587\u4e2d\u7684\u6bcf\u4e2a\u5b57\u6bcd\u9006\u5411\u4f4d\u79fb\u5bc6\u94a5\u6570\uff0c\u53ef\u4ee5\u8fd8\u539f\u51fa\u539f\u59cb\u7684\u660e\u6587\u3002<\/p>\n<\/p>\n<p><p>\u89e3\u5bc6\u51fd\u6570\u7684\u5b9e\u73b0\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def decrypt(text, key):<\/p>\n<p>    decrypted_text = &quot;&quot;<\/p>\n<p>    for char in text:<\/p>\n<p>        if char.isalpha():<\/p>\n<p>            shift = key<\/p>\n<p>            if char.islower():<\/p>\n<p>                decrypted_text += chr((ord(char) - 97 - shift) % 26 + 97)<\/p>\n<p>            elif char.isupper():<\/p>\n<p>                decrypted_text += chr((ord(char) - 65 - shift) % 26 + 65)<\/p>\n<p>        else:<\/p>\n<p>            decrypted_text += char<\/p>\n<p>    return decrypted_text<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u89e3\u5bc6\u51fd\u6570\u4e0e\u52a0\u5bc6\u51fd\u6570\u7c7b\u4f3c\uff0c\u53ea\u662f\u5728\u4f4d\u79fb\u65f6\u4f7f\u7528\u51cf\u6cd5\u800c\u4e0d\u662f\u52a0\u6cd5\u3002<\/p>\n<\/p>\n<p><p>\u56db\u3001\u5904\u7406\u8f93\u5165\u8f93\u51fa<\/p>\n<\/p>\n<p><p>\u5904\u7406\u8f93\u5165\u8f93\u51fa\u662f\u4f7f\u7a0b\u5e8f\u66f4\u52a0\u7528\u6237\u53cb\u597d\u7684\u5173\u952e\u3002\u53ef\u4ee5\u8bbe\u8ba1\u4e00\u4e2a\u7b80\u5355\u7684\u547d\u4ee4\u884c\u754c\u9762\uff0c\u8ba9\u7528\u6237\u8f93\u5165\u9700\u8981\u52a0\u5bc6\u6216\u89e3\u5bc6\u7684\u6587\u672c\u4ee5\u53ca\u9009\u62e9\u4f7f\u7528\u7684\u5bc6\u94a5\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def m<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>n():<\/p>\n<p>    choice = input(&quot;Do you want to encrypt or decrypt? (e\/d): &quot;).lower()<\/p>\n<p>    text = input(&quot;Enter your text: &quot;)<\/p>\n<p>    key = int(input(&quot;Enter your key (1-25): &quot;))<\/p>\n<p>    if choice == &#39;e&#39;:<\/p>\n<p>        encrypted_text = encrypt(text, key)<\/p>\n<p>        print(&quot;Encrypted text:&quot;, encrypted_text)<\/p>\n<p>    elif choice == &#39;d&#39;:<\/p>\n<p>        decrypted_text = decrypt(text, key)<\/p>\n<p>        print(&quot;Decrypted text:&quot;, decrypted_text)<\/p>\n<p>    else:<\/p>\n<p>        print(&quot;Invalid choice. Please enter &#39;e&#39; to encrypt or &#39;d&#39; to decrypt.&quot;)<\/p>\n<p>if __name__ == &quot;__main__&quot;:<\/p>\n<p>    main()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u4e2a<code>main<\/code>\u51fd\u6570\u53ef\u4ee5\u5904\u7406\u7528\u6237\u8f93\u5165\uff0c\u5e76\u6839\u636e\u7528\u6237\u9009\u62e9\u8c03\u7528\u76f8\u5e94\u7684\u52a0\u5bc6\u6216\u89e3\u5bc6\u51fd\u6570\u3002\u7528\u6237\u53ef\u4ee5\u9009\u62e9\u64cd\u4f5c\u7c7b\u578b\u3001\u8f93\u5165\u6587\u672c\u548c\u5bc6\u94a5\uff0c\u7a0b\u5e8f\u5c06\u8f93\u51fa\u5904\u7406\u540e\u7684\u7ed3\u679c\u3002<\/p>\n<\/p>\n<p><p>\u4e94\u3001\u4f18\u5316\u4e0e\u6269\u5c55<\/p>\n<\/p>\n<p><p>\u867d\u7136\u51ef\u6492\u5bc6\u7801\u662f\u4e00\u79cd\u57fa\u7840\u7684\u52a0\u5bc6\u65b9\u6cd5\uff0c\u4f46\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u4e00\u4e9b\u4f18\u5316\u548c\u6269\u5c55\u6765\u589e\u5f3a\u5176\u529f\u80fd\u548c\u5b89\u5168\u6027\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u589e\u52a0\u8f93\u5165\u9a8c\u8bc1<\/strong>\uff1a\u786e\u4fdd\u7528\u6237\u8f93\u5165\u7684\u5bc6\u94a5\u5728\u6709\u6548\u8303\u56f4\u5185\uff0c\u4e14\u9009\u62e9\u4e86\u6709\u6548\u7684\u64cd\u4f5c\u7c7b\u578b\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u652f\u6301\u591a\u79cd\u5b57\u7b26\u96c6<\/strong>\uff1a\u901a\u8fc7\u6269\u5c55\u5b57\u6bcd\u8868\u6765\u652f\u6301\u5176\u4ed6\u5b57\u7b26\u96c6\uff0c\u5982\u897f\u91cc\u5c14\u5b57\u6bcd\u3001\u5e0c\u814a\u5b57\u6bcd\u7b49\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u7ed3\u5408\u5176\u4ed6\u52a0\u5bc6\u6280\u672f<\/strong>\uff1a\u4e0e\u5176\u4ed6\u52a0\u5bc6\u6280\u672f\u7ed3\u5408\u4f7f\u7528\uff0c\u5982Vigen\u00e8re\u5bc6\u7801\u3001Substitution\u5bc6\u7801\u7b49\uff0c\u4ee5\u63d0\u9ad8\u52a0\u5bc6\u5b89\u5168\u6027\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u5b9e\u73b0\u56fe\u5f62\u7528\u6237\u754c\u9762\uff08GUI\uff09<\/strong>\uff1a\u4f7f\u7528<code>tkinter<\/code>\u7b49\u5e93\u4e3a\u7a0b\u5e8f\u6dfb\u52a0\u56fe\u5f62\u7528\u6237\u754c\u9762\uff0c\u4f7f\u5176\u66f4\u52a0\u76f4\u89c2\u548c\u6613\u7528\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u6279\u91cf\u5904\u7406\u6587\u4ef6<\/strong>\uff1a\u6269\u5c55\u7a0b\u5e8f\u529f\u80fd\uff0c\u4f7f\u5176\u80fd\u591f\u52a0\u5bc6\u6216\u89e3\u5bc6\u6587\u4ef6\u5185\u5bb9\uff0c\u652f\u6301\u591a\u6587\u4ef6\u6279\u5904\u7406\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u901a\u8fc7\u8fd9\u4e9b\u4f18\u5316\u548c\u6269\u5c55\uff0c\u51ef\u6492\u5bc6\u7801\u5728\u5b66\u4e60\u548c\u7406\u89e3\u52a0\u5bc6\u6280\u672f\u65b9\u9762\u4ecd\u7136\u5177\u6709\u5f88\u9ad8\u7684\u6559\u5b66\u4ef7\u503c\u3002\u5c3d\u7ba1\u4e0d\u9002\u7528\u4e8e\u73b0\u4ee3\u5b89\u5168\u9700\u6c42\uff0c\u4f46\u5176\u7b80\u5355\u6027\u548c\u6613\u7528\u6027\u4f7f\u5176\u6210\u4e3a\u5b66\u4e60\u52a0\u5bc6\u6280\u672f\u7684\u7406\u60f3\u8d77\u70b9\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u5b9e\u73b0\u51ef\u6492\u5bc6\u7801\u7684\u7f16\u7801\u548c\u89e3\u7801\uff1f<\/strong><br \/>\u51ef\u6492\u5bc6\u7801\u662f\u4e00\u79cd\u7b80\u5355\u7684\u66ff\u6362\u5bc6\u7801\uff0c\u91c7\u7528\u5b57\u6bcd\u8868\u4e2d\u7684\u4f4d\u7f6e\u79fb\u52a8\u6765\u52a0\u5bc6\u4fe1\u606f\u3002\u5728Python\u4e2d\uff0c\u60a8\u53ef\u4ee5\u901a\u8fc7\u5b9a\u4e49\u4e00\u4e2a\u51fd\u6570\u6765\u5b9e\u73b0\u7f16\u7801\u548c\u89e3\u7801\u3002\u53ef\u4ee5\u4f7f\u7528ASCII\u503c\u6765\u5904\u7406\u5b57\u6bcd\u7684\u79fb\u52a8\u3002\u4f8b\u5982\uff0c\u60a8\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a\u51fd\u6570\uff0c\u63a5\u53d7\u6587\u672c\u548c\u504f\u79fb\u91cf\u4f5c\u4e3a\u53c2\u6570\uff0c\u901a\u8fc7\u5faa\u73af\u904d\u5386\u6bcf\u4e2a\u5b57\u7b26\u5e76\u6839\u636e\u504f\u79fb\u91cf\u8c03\u6574\u5176\u4f4d\u7f6e\uff0c\u4ece\u800c\u751f\u6210\u52a0\u5bc6\u6216\u89e3\u5bc6\u540e\u7684\u7ed3\u679c\u3002<\/p>\n<p><strong>\u51ef\u6492\u5bc6\u7801\u7684\u504f\u79fb\u91cf\u53ef\u4ee5\u8bbe\u7f6e\u4e3a\u591a\u5c11\uff1f<\/strong><br \/>\u504f\u79fb\u91cf\u901a\u5e38\u8bbe\u7f6e\u4e3a1\u523025\u4e4b\u95f4\u7684\u6574\u6570\u503c\u3002\u504f\u79fb\u91cf\u51b3\u5b9a\u4e86\u5b57\u6bcd\u5728\u5b57\u6bcd\u8868\u4e2d\u79fb\u52a8\u7684\u4f4d\u6570\u3002\u6bd4\u5982\uff0c\u504f\u79fb\u91cf\u4e3a3\u65f6\uff0c\u5b57\u6bcd&#39;A&#39;\u4f1a\u88ab\u52a0\u5bc6\u4e3a&#39;D&#39;\u3002\u5982\u679c\u9700\u8981\u66f4\u590d\u6742\u7684\u52a0\u5bc6\uff0c\u60a8\u53ef\u4ee5\u9009\u62e9\u66f4\u5927\u7684\u504f\u79fb\u91cf\uff0c\u4f46\u901a\u5e38\u5efa\u8bae\u4fdd\u6301\u57281\u523025\u4e4b\u95f4\uff0c\u4ee5\u786e\u4fdd\u5bc6\u7801\u7684\u7b80\u5355\u6027\u548c\u6613\u4e8e\u7406\u89e3\u3002<\/p>\n<p><strong>\u5728\u4f7f\u7528\u51ef\u6492\u5bc6\u7801\u65f6\uff0c\u5982\u4f55\u5904\u7406\u975e\u5b57\u6bcd\u5b57\u7b26\uff1f<\/strong><br \/>\u5728\u5b9e\u73b0\u51ef\u6492\u5bc6\u7801\u65f6\uff0c\u975e\u5b57\u6bcd\u5b57\u7b26\uff08\u5982\u7a7a\u683c\u3001\u6570\u5b57\u548c\u6807\u70b9\u7b26\u53f7\uff09\u901a\u5e38\u4fdd\u6301\u4e0d\u53d8\u3002\u60a8\u53ef\u4ee5\u5728\u7f16\u7801\u548c\u89e3\u7801\u8fc7\u7a0b\u4e2d\u6dfb\u52a0\u6761\u4ef6\u5224\u65ad\uff0c\u786e\u4fdd\u53ea\u6709\u5b57\u6bcd\u5b57\u7b26\u88ab\u79fb\u52a8\uff0c\u800c\u5176\u4ed6\u5b57\u7b26\u5219\u76f4\u63a5\u6dfb\u52a0\u5230\u7ed3\u679c\u4e2d\u3002\u8fd9\u79cd\u65b9\u6cd5\u53ef\u4ee5\u4f7f\u52a0\u5bc6\u8fc7\u7a0b\u66f4\u7075\u6d3b\uff0c\u5e76\u786e\u4fdd\u4fe1\u606f\u7684\u53ef\u8bfb\u6027\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u7f16\u5199Python\u51ef\u6492\u5bc6\u7801\u7684\u8fc7\u7a0b\u5305\u62ec\uff1a\u9009\u62e9\u5bc6\u94a5\u3001\u52a0\u5bc6\u7b97\u6cd5\u3001\u89e3\u5bc6\u7b97\u6cd5\u3001\u5904\u7406\u8f93\u5165\u8f93\u51fa\u3002\u9009\u62e9\u4e00\u4e2a\u9002\u5408\u7684\u5bc6\u94a5\u662f\u81f3\u5173\u91cd\u8981\u7684 [&hellip;]","protected":false},"author":3,"featured_media":998755,"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\/998747"}],"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=998747"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/998747\/revisions"}],"predecessor-version":[{"id":998756,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/998747\/revisions\/998756"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/998755"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=998747"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=998747"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=998747"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}