{"id":312925,"date":"2024-05-20T22:51:30","date_gmt":"2024-05-20T14:51:30","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/312925.html"},"modified":"2024-05-20T22:51:41","modified_gmt":"2024-05-20T14:51:41","slug":"%e6%80%8e%e4%b9%88%e7%94%a8-python-%e4%bb%a3%e7%a0%81%e5%ae%9e%e7%8e%b0%e4%b8%80%e4%b8%aa%e7%ae%80%e5%8d%95%e7%9a%84%e5%87%af%e6%92%92%e5%8a%a0%e5%af%86%e7%ae%97%e6%b3%95-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/312925.html","title":{"rendered":"\u600e\u4e48\u7528 Python \u4ee3\u7801\u5b9e\u73b0\u4e00\u4e2a\u7b80\u5355\u7684\u51ef\u6492\u52a0\u5bc6\u7b97\u6cd5"},"content":{"rendered":"<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/26233718\/8e8e7c7f-2c7f-4903-897c-d9eb15a6ff37.webp\" alt=\"\u600e\u4e48\u7528 Python \u4ee3\u7801\u5b9e\u73b0\u4e00\u4e2a\u7b80\u5355\u7684\u51ef\u6492\u52a0\u5bc6\u7b97\u6cd5\" \/><\/p>\n<p><p>\u51ef\u6492\u52a0\u5bc6\u7b97\u6cd5\u662f\u4e00\u79cd\u7b80\u5355\u7684\u66ff\u6362\u5f0f\u5bc6\u7801\uff0c\u901a\u8fc7\u5c06\u5b57\u6bcd\u8868\u4e2d\u7684\u6bcf\u4e2a\u5b57\u6bcd\u79fb\u52a8\u56fa\u5b9a\u6570\u76ee\u7684\u4f4d\u7f6e\u6765\u52a0\u5bc6\u4fe1\u606f\u3002<strong>\u5177\u4f53\u6765\u8bf4\uff0c\u82e5\u8bbe\u5b9a\u79fb\u4f4d\u6570\u4e3a3\uff0c\u90a3\u4e48&#039;A&#039;\u52a0\u5bc6\u540e\u53d8\u6210&#039;D&#039;\u3001&#039;B&#039;\u53d8\u6210&#039;E&#039;\uff0c\u4ee5\u6b64\u7c7b\u63a8<\/strong>\u3002\u5728\u5b57\u6bcd\u8868\u672b\u7aef\u5219\u56de\u73af\u5230\u5b57\u6bcd\u8868\u5f00\u5934\uff0c\u5982&#039;Z&#039;\u52a0\u5bc6\u540e\u53d8\u4e3a&#039;C&#039;\u3002\u5bf9\u4e8e\u5c0f\u5199\u5b57\u6bcd\u540c\u7406\uff0c&#039;a&#039;\u52a0\u5bc6\u540e\u53d8\u6210&#039;d&#039;\u3002\u6b64\u7b97\u6cd5\u5728Python\u4e2d\u53ef\u901a\u8fc7\u51e0\u79cd\u65b9\u6cd5\u5b9e\u73b0\uff0c\u5176\u4e2d\u4e00\u79cd\u662f\u4f7f\u7528ASCII\u7801\u7684\u8f6c\u6362\uff0c\u53e6\u4e00\u79cd\u662f\u5efa\u7acb\u5b57\u6bcd\u8868\u6620\u5c04\u5173\u7cfb\u3002\u5728\u6b64\u5c06\u5c55\u5f00\u8be6\u7ec6\u63cf\u8ff0\u57fa\u4e8eASCII\u7801\u8f6c\u6362\u7684\u52a0\u5bc6\u8fc7\u7a0b\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u786e\u5b9a\u52a0\u5bc6\u6b65\u9aa4<\/h3>\n<\/p>\n<p><p>\u5b9e\u73b0\u51ef\u6492\u52a0\u5bc6\u7b97\u6cd5\u7684\u7b2c\u4e00\u6b65\u662f\u89c4\u5212\u52a0\u5bc6\u7684\u57fa\u672c\u6b65\u9aa4\uff0c\u8fd9\u4e9b\u6b65\u9aa4\u5305\u62ec\uff1a<\/p>\n<\/p>\n<ol>\n<li>\u63a5\u6536\u5f85\u52a0\u5bc6\u7684\u660e\u6587\u548c\u79fb\u4f4d\u6570\uff08\u504f\u79fb\u91cf\uff09\u3002<\/li>\n<li>\u904d\u5386\u660e\u6587\u4e2d\u7684\u6bcf\u4e2a\u5b57\u7b26\u3002<\/li>\n<li>\u5bf9\u5b57\u6bcd\u5b57\u7b26\u8fdb\u884c\u504f\u79fb\uff0c\u975e\u5b57\u6bcd\u5b57\u7b26\u4fdd\u6301\u4e0d\u53d8\u3002<\/li>\n<li>\u6c47\u603b\u5904\u7406\u540e\u7684\u5b57\u7b26\uff0c\u5f62\u6210\u52a0\u5bc6\u6587\u672c\u3002<\/li>\n<\/ol>\n<p><h3>\u4e8c\u3001ASCII\u7801\u8f6c\u6362\u65b9\u6cd5<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\u5904\u7406\u5b57\u7b26\u65f6\uff0c\u53ef\u4ee5\u5229\u7528\u5b57\u7b26\u4e0e\u5176\u5bf9\u5e94\u7684ASCII\u7801\u4e4b\u95f4\u7684\u8f6c\u6362\u3002\u4ee5\u4e0b\u662f\u5177\u4f53\u65b9\u6cd5\uff1a<\/p>\n<\/p>\n<ol>\n<li>\u4f7f\u7528<code>ord()<\/code>\u51fd\u6570\u53ef\u4ee5\u83b7\u53d6\u5b57\u7b26\u7684ASCII\u7801\u503c\u3002<\/li>\n<li>\u8f6c\u6362\u540e\u7684\u503c\u5982\u679c\u9700\u8981\u56de\u5230\u5b57\u6bcd\u7684\u8303\u56f4\u5185\uff0c\u53ef\u80fd\u9700\u8981\u8fdb\u884c\u53d6\u6a21\u64cd\u4f5c\u3002<\/li>\n<li>\u4f7f\u7528<code>chr()<\/code>\u51fd\u6570\u53ef\u4ee5\u5c06ASCII\u7801\u503c\u8f6c\u56de\u5b57\u7b26\u3002<\/li>\n<\/ol>\n<p><h3>\u4e09\u3001\u4ee3\u7801\u5b9e\u73b0<\/h3>\n<\/p>\n<p><h4>\u51fd\u6570\u5b9a\u4e49<\/h4>\n<\/p>\n<p><p>\u6211\u4eec\u5c06\u4ee5\u4e00\u4e2a\u51fd\u6570\u7684\u5f62\u5f0f\u5b9a\u4e49\u51ef\u6492\u52a0\u5bc6\u7684\u903b\u8f91\uff0c\u51fd\u6570\u63a5\u6536\u7684\u53c2\u6570\u5305\u62ec\u5f85\u52a0\u5bc6\u7684\u660e\u6587\u548c\u79fb\u4f4d\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def caesar_cipher_encrypt(pl<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>ntext, shift):<\/p>\n<p>    encrypted_text = &quot;&quot;<\/p>\n<p>    for char in plaintext:<\/p>\n<p>        if char.isalpha(): # \u68c0\u67e5\u662f\u5426\u4e3a\u5b57\u6bcd<\/p>\n<p>            # \u83b7\u53d6\u5b57\u6bcd\u5728\u5b57\u6bcd\u8868\u4e2d\u7684\u504f\u79fb\u8d77\u59cb\u503c\uff08\u5927\u5199\u6216\u5c0f\u5199\uff09<\/p>\n<p>            start = ord(&#039;A&#039;) if char.isupper() else ord(&#039;a&#039;)<\/p>\n<p>            # ASCII\u8f6c\u6362\uff0c\u52a0\u5bc6\u6838\u5fc3<\/p>\n<p>            encrypted_char = chr(start + (ord(char) - start + shift) % 26)<\/p>\n<p>            encrypted_text += encrypted_char<\/p>\n<p>        else:<\/p>\n<p>            # \u975e\u5b57\u6bcd\u5b57\u7b26\u4e0d\u53d8<\/p>\n<p>            encrypted_text += char<\/p>\n<p>    return encrypted_text<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u8c03\u7528\u51fd\u6570<\/h4>\n<\/p>\n<p><p>\u52a0\u5bc6\u4e00\u4e2a\u7b80\u5355\u7684\u660e\u6587\u53ef\u4ee5\u901a\u8fc7\u8c03\u7528\u4e4b\u524d\u5b9a\u4e49\u7684<code>caesar_cipher_encrypt<\/code>\u51fd\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">plain_text = &quot;Hello, World!&quot;<\/p>\n<p>shift_value = 3<\/p>\n<p>encrypted_text = caesar_cipher_encrypt(plain_text, shift_value)<\/p>\n<p>print(&quot;\u52a0\u5bc6\u540e\u7684\u6587\u672c:&quot;, encrypted_text)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u89e3\u5bc6\u8fc7\u7a0b<\/h3>\n<\/p>\n<p><p>\u4e0e\u52a0\u5bc6\u76f8\u4f3c\uff0c\u89e3\u5bc6\u53ea\u9700\u5c06\u79fb\u4f4d\u6570\u8bbe\u7f6e\u4e3a\u52a0\u5bc6\u65f6\u79fb\u4f4d\u6570\u7684\u76f8\u53cd\u6570\u5373\u53ef\u3002\u4ee5\u4e0b\u662f\u89e3\u5bc6\u51fd\u6570\u7684\u4ee3\u7801\u5b9e\u73b0\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def caesar_cipher_decrypt(encrypted_text, shift):<\/p>\n<p>    return caesar_cipher_encrypt(encrypted_text, -shift)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8c03\u7528\u89e3\u5bc6\u51fd\u6570\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">decrypted_text = caesar_cipher_decrypt(encrypted_text, shift_value)<\/p>\n<p>print(&quot;\u89e3\u5bc6\u540e\u7684\u6587\u672c:&quot;, decrypted_text)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u8003\u8651\u7279\u6b8a\u60c5\u51b5<\/h3>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u53ef\u80fd\u8fd8\u9700\u8003\u8651\u4e00\u4e9b\u7279\u6b8a\u60c5\u51b5\uff0c\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<ol>\n<li>\u79fb\u4f4d\u6570\u53ef\u80fd\u5927\u4e8e26\u6216\u8005\u4e3a\u8d1f\u6570\uff0c\u8fd9\u65f6\u9700\u8981\u5bf9\u5176\u8fdb\u884c\u5904\u7406\uff0c\u4ee5\u786e\u4fdd\u52a0\u5bc6\u903b\u8f91\u7684\u6b63\u786e\u6027\u3002<\/li>\n<li>\u8f93\u5165\u7684\u660e\u6587\u4e2d\u53ef\u80fd\u5305\u542b\u975e\u5b57\u6bcd\u5b57\u7b26\uff0c\u5982\u6570\u5b57\u3001\u7b26\u53f7\u3001\u7a7a\u683c\u7b49\uff0c\u8fd9\u9700\u8981\u5728\u4ee3\u7801\u4e2d\u5224\u5b9a\u5e76\u4fdd\u6301\u539f\u6837\u8f93\u51fa\u3002<\/li>\n<\/ol>\n<p><h3>\u516d\u3001\u5b8c\u6574\u4ee3\u7801\u793a\u4f8b<\/h3>\n<\/p>\n<p><p>\u7ed3\u5408\u4e0a\u8ff0\u6240\u6709\u8981\u70b9\uff0c\u53ef\u4ee5\u5f97\u51fa\u5b8c\u6574\u7684\u51ef\u6492\u52a0\u5bc6\u7b97\u6cd5\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def caesar_cipher_encrypt(plaintext, shift):<\/p>\n<p>    # \u786e\u4fdd\u504f\u79fb\u91cf\u662f\u6b63\u6574\u6570\u4e14\u5c0f\u4e8e26<\/p>\n<p>    shift = shift % 26<\/p>\n<p>    encrypted_text = &quot;&quot;<\/p>\n<p>    for char in plaintext:<\/p>\n<p>        # \u53ea\u5904\u7406\u5b57\u6bcd\u5b57\u7b26<\/p>\n<p>        if char.isalpha():<\/p>\n<p>            start = ord(&#039;A&#039;) if char.isupper() else ord(&#039;a&#039;)<\/p>\n<p>            encrypted_char = chr(start + (ord(char) - start + shift) % 26)<\/p>\n<p>            encrypted_text += encrypted_char<\/p>\n<p>        else:<\/p>\n<p>            encrypted_text += char<\/p>\n<p>    return encrypted_text<\/p>\n<p>def caesar_cipher_decrypt(encrypted_text, shift):<\/p>\n<p>    return caesar_cipher_encrypt(encrypted_text, -shift)<\/p>\n<h2><strong>\u6d4b\u8bd5\u52a0\u5bc6\u548c\u89e3\u5bc6<\/strong><\/h2>\n<p>plain_text = &quot;Hello, World!&quot;<\/p>\n<p>shift_value = 3<\/p>\n<p>encrypted_text = caesar_cipher_encrypt(plain_text, shift_value)<\/p>\n<p>decrypted_text = caesar_cipher_decrypt(encrypted_text, shift_value)<\/p>\n<p>print(&quot;\u539f\u59cb\u6587\u672c:&quot;, plain_text)<\/p>\n<p>print(&quot;\u52a0\u5bc6\u540e\u7684\u6587\u672c:&quot;, encrypted_text)<\/p>\n<p>print(&quot;\u89e3\u5bc6\u540e\u7684\u6587\u672c:&quot;, decrypted_text)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u4e0a\u8ff0\u4ee3\u7801\uff0c\u6211\u4eec\u5b9e\u73b0\u4e86\u57fa\u4e8ePython\u7684\u7b80\u5355\u51ef\u6492\u52a0\u5bc6\u7b97\u6cd5\uff0c\u52a0\u5bc6\u548c\u89e3\u5bc6\u529f\u80fd\u90fd\u80fd\u591f\u6b63\u5e38\u5de5\u4f5c\uff0c\u5e76\u8003\u8651\u5230\u4e86\u5404\u79cd\u8fb9\u754c\u6761\u4ef6\u548c\u7279\u6b8a\u60c5\u51b5\u3002\u8fd9\u79cd\u7b97\u6cd5\u9002\u7528\u4e8e\u5bf9\u6587\u672c\u8fdb\u884c\u57fa\u672c\u7684\u52a0\u5bc6\uff0c\u4f46\u7531\u4e8e\u5176\u7b80\u5355\u6027\u5e76\u4e0d\u9002\u7528\u4e8e\u9700\u8981\u9ad8\u5b89\u5168\u6027\u7684\u573a\u666f\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p><strong>\u5982\u4f55\u4f7f\u7528Python\u7f16\u5199\u4e00\u4e2a\u7b80\u5355\u7684\u51ef\u6492\u52a0\u5bc6\u7b97\u6cd5\uff1f<\/strong><\/p>\n<ol>\n<li>\n<p><strong>\u4ec0\u4e48\u662f\u51ef\u6492\u52a0\u5bc6\u7b97\u6cd5\uff1f<\/strong><br \/>\n\u51ef\u6492\u52a0\u5bc6\u7b97\u6cd5\u662f\u4e00\u79cd\u7b80\u5355\u7684\u66ff\u6362\u5bc6\u7801\uff0c\u5b83\u57fa\u4e8e\u5b57\u6bcd\u7684\u504f\u79fb\u3002\u6bcf\u4e2a\u5b57\u6bcd\u88ab\u66ff\u6362\u4e3a\u5b57\u6bcd\u8868\u4e2d\u56fa\u5b9a\u504f\u79fb\u91cf\u7684\u540e\u7eed\u5b57\u6bcd\u3002\u4f8b\u5982\uff0c\u5982\u679c\u504f\u79fb\u91cf\u4e3a1\uff0c\u5219&#039;A&#039;\u5c06\u88ab\u66ff\u6362\u4e3a&#039;B&#039;\uff0c&#039;B&#039;\u5c06\u88ab\u66ff\u6362\u4e3a&#039;C&#039;\uff0c\u4ee5\u6b64\u7c7b\u63a8\u3002<\/p>\n<\/li>\n<li>\n<p><strong>\u5982\u4f55\u5b9e\u73b0\u51ef\u6492\u52a0\u5bc6\u7b97\u6cd5\u7684Python\u4ee3\u7801\uff1f<\/strong><br \/>\n\u6b65\u9aa4\u5982\u4e0b\uff1a<\/p>\n<ul>\n<li>\u9996\u5148\uff0c\u521b\u5efa\u4e00\u4e2a\u51fd\u6570\uff0c\u4f20\u5165\u660e\u6587\u548c\u504f\u79fb\u91cf\u4f5c\u4e3a\u53c2\u6570\u3002<\/li>\n<li>\u7136\u540e\uff0c\u5b9a\u4e49\u4e00\u4e2a\u7a7a\u5b57\u7b26\u4e32\u6765\u5b58\u50a8\u52a0\u5bc6\u540e\u7684\u5bc6\u6587\u3002<\/li>\n<li>\u63a5\u4e0b\u6765\uff0c\u904d\u5386\u660e\u6587\u4e2d\u7684\u6bcf\u4e2a\u5b57\u7b26\u3002<\/li>\n<li>\u5bf9\u6bcf\u4e2a\u5b57\u7b26\u8fdb\u884c\u5224\u65ad\uff0c\u5982\u679c\u662f\u5b57\u6bcd\uff0c\u5219\u8fdb\u884c\u504f\u79fb\u52a0\u5bc6\u3002<\/li>\n<li>\u52a0\u5bc6\u540e\u7684\u5b57\u7b26\u6dfb\u52a0\u5230\u5bc6\u6587\u5b57\u7b26\u4e32\u4e2d\u3002<\/li>\n<li>\u8fd4\u56de\u52a0\u5bc6\u540e\u7684\u5bc6\u6587\u3002<\/li>\n<\/ul>\n<p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u51ef\u6492\u52a0\u5bc6\u7b97\u6cd5\u7684Python\u4ee3\u7801\u793a\u4f8b\uff1a<\/p>\n<pre><code class=\"language-python\">def caesar_encrypt(plaintext, offset):\n    ciphertext = &quot;&quot;\n    for char in plaintext:\n        if char.isalpha():\n            if char.islower():\n                ciphertext += chr((ord(char) - ord(&#039;a&#039;) + offset) % 26 + ord(&#039;a&#039;))\n            else:\n                ciphertext += chr((ord(char) - ord(&#039;A&#039;) + offset) % 26 + ord(&#039;A&#039;))\n        else:\n            ciphertext += char\n    return ciphertext\n<\/code><\/pre>\n<\/li>\n<li>\n<p><strong>\u5982\u4f55\u4f7f\u7528\u51ef\u6492\u52a0\u5bc6\u7b97\u6cd5\u52a0\u5bc6\u548c\u89e3\u5bc6\u6587\u672c\uff1f<\/strong><br \/>\n\u8981\u52a0\u5bc6\u6587\u672c\uff0c\u8c03\u7528<code>caesar_encrypt<\/code>\u51fd\u6570\u5e76\u4f20\u5165\u660e\u6587\u548c\u504f\u79fb\u91cf\uff0c\u7136\u540e\u5c06\u8fd4\u56de\u7684\u52a0\u5bc6\u6587\u672c\u6253\u5370\u51fa\u6765\u3002\u8981\u89e3\u5bc6\u6587\u672c\uff0c\u53ea\u9700\u4f7f\u7528\u4e0e\u52a0\u5bc6\u65f6\u76f8\u53cd\u7684\u504f\u79fb\u91cf\u8c03\u7528<code>caesar_encrypt<\/code>\u51fd\u6570\u5373\u53ef\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"language-python\">plaintext = &quot;Hello, World!&quot;\noffset = 3\nencrypted_text = caesar_encrypt(plaintext, offset)\ndecrypted_text = caesar_encrypt(encrypted_text, -offset)\nprint(&quot;\u52a0\u5bc6\u540e\u7684\u6587\u672c:&quot;, encrypted_text)\nprint(&quot;\u89e3\u5bc6\u540e\u7684\u6587\u672c:&quot;, decrypted_text)\n<\/code><\/pre>\n<\/li>\n<\/ol>\n","protected":false},"excerpt":{"rendered":"\u51ef\u6492\u52a0\u5bc6\u7b97\u6cd5\u662f\u4e00\u79cd\u7b80\u5355\u7684\u66ff\u6362\u5f0f\u5bc6\u7801\uff0c\u901a\u8fc7\u5c06\u5b57\u6bcd\u8868\u4e2d\u7684\u6bcf\u4e2a\u5b57\u6bcd\u79fb\u52a8\u56fa\u5b9a\u6570\u76ee\u7684\u4f4d\u7f6e\u6765\u52a0\u5bc6\u4fe1\u606f\u3002\u5177\u4f53\u6765\u8bf4\uff0c\u82e5\u8bbe\u5b9a\u79fb\u4f4d [&hellip;]","protected":false},"author":3,"featured_media":312937,"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\/312925"}],"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=312925"}],"version-history":[{"count":0,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/312925\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/312937"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=312925"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=312925"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=312925"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}