{"id":1175327,"date":"2025-01-15T17:30:25","date_gmt":"2025-01-15T09:30:25","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1175327.html"},"modified":"2025-01-15T17:30:25","modified_gmt":"2025-01-15T09:30:25","slug":"python%e4%b8%ad%e5%a6%82%e4%bd%95%e5%bc%95%e5%85%a5crypto%e5%ba%93","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1175327.html","title":{"rendered":"python\u4e2d\u5982\u4f55\u5f15\u5165crypto\u5e93"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25110812\/4c07d460-ce44-4656-86e3-9598a2097b09.webp\" alt=\"python\u4e2d\u5982\u4f55\u5f15\u5165crypto\u5e93\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\u5f15\u5165crypto\u5e93\u53ef\u4ee5\u4f7f\u7528PyCrypto\u5e93\u3001Cryptography\u5e93\u3001PyCryptodome\u5e93\uff0c\u5177\u4f53\u53d6\u51b3\u4e8e\u4f60\u7684\u9700\u6c42\u548c\u73af\u5883\u3002\u672c\u6587\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u5b89\u88c5\u548c\u4f7f\u7528\u8fd9\u4e09\u79cd\u5e93\uff0c\u5e76\u91cd\u70b9\u4ecb\u7ecdPyCryptodome\u5e93\u7684\u4f7f\u7528\u3002<\/strong><\/p>\n<\/p>\n<p><p><strong>\u4e00\u3001\u5b89\u88c5PyCrypto\u5e93<\/strong><\/p>\n<p>PyCrypto\u5e93\u662fPython\u4e2d\u6700\u65e9\u4e14\u529f\u80fd\u8f83\u4e3a\u9f50\u5168\u7684\u52a0\u5bc6\u5e93\u4e4b\u4e00\uff0c\u4f46\u662f\u7531\u4e8e\u5176\u505c\u6b62\u7ef4\u62a4\uff0c\u63a8\u8350\u4f7f\u7528\u66ff\u4ee3\u7684PyCryptodome\u5e93\u3002<\/p>\n<\/p>\n<ol>\n<li>\u5728\u547d\u4ee4\u884c\u4e2d\u4f7f\u7528pip\u547d\u4ee4\u5b89\u88c5PyCrypto\uff1a<\/li>\n<\/ol>\n<p><pre><code class=\"language-bash\">pip install pycrypto<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u4e8c\u3001\u5b89\u88c5Cryptography\u5e93<\/strong><\/p>\n<p>Cryptography\u5e93\u662f\u53e6\u4e00\u4e2a\u6d41\u884c\u7684\u52a0\u5bc6\u5e93\uff0c\u529f\u80fd\u5f3a\u5927\u4e14\u6613\u4e8e\u4f7f\u7528\uff0c\u63a8\u8350\u7528\u4e8e\u65b0\u7684\u9879\u76ee\u3002<\/p>\n<\/p>\n<ol>\n<li>\u5728\u547d\u4ee4\u884c\u4e2d\u4f7f\u7528pip\u547d\u4ee4\u5b89\u88c5Cryptography\uff1a<\/li>\n<\/ol>\n<p><pre><code class=\"language-bash\">pip install cryptography<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li>\u5b89\u88c5\u5b8c\u6210\u540e\uff0c\u53ef\u4ee5\u5728\u4ee3\u7801\u4e2d\u5f15\u5165Cryptography\u5e93\uff1a<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">from cryptography.hazmat.primitives import hashes<\/p>\n<p>from cryptography.hazmat.primitives.kdf.pbkdf2 import PBKDF2HMAC<\/p>\n<p>from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u4e09\u3001\u5b89\u88c5\u548c\u4f7f\u7528PyCryptodome\u5e93<\/strong><\/p>\n<p>PyCryptodome\u662fPyCrypto\u7684\u4e00\u4e2a\u5206\u652f\uff0c\u589e\u52a0\u4e86\u66f4\u591a\u7684\u529f\u80fd\u5e76\u4fee\u590d\u4e86\u4e00\u4e9b\u5b89\u5168\u95ee\u9898\uff0c\u63a8\u8350\u4f5c\u4e3aPyCrypto\u7684\u66ff\u4ee3\u5e93\u3002<\/p>\n<\/p>\n<ol>\n<li>\u5728\u547d\u4ee4\u884c\u4e2d\u4f7f\u7528pip\u547d\u4ee4\u5b89\u88c5PyCryptodome\uff1a<\/li>\n<\/ol>\n<p><pre><code class=\"language-bash\">pip install pycryptodome<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li>\u5b89\u88c5\u5b8c\u6210\u540e\uff0c\u53ef\u4ee5\u5728\u4ee3\u7801\u4e2d\u5f15\u5165PyCryptodome\u5e93\uff1a<\/li>\n<\/ol>\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>from Crypto.Protocol.KDF import PBKDF2<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u8be6\u7ec6\u63cf\u8ff0PyCryptodome\u5e93\u7684\u4f7f\u7528\uff1a<\/strong><\/p>\n<p>PyCryptodome\u5e93\u662f\u4e00\u79cd\u529f\u80fd\u5f3a\u5927\u7684\u52a0\u5bc6\u5e93\uff0c\u5b83\u63d0\u4f9b\u4e86\u5bf9\u5bf9\u79f0\u52a0\u5bc6\u3001\u975e\u5bf9\u79f0\u52a0\u5bc6\u3001\u54c8\u5e0c\u548c\u6d88\u606f\u6458\u8981\u7b49\u591a\u79cd\u52a0\u5bc6\u7b97\u6cd5\u7684\u652f\u6301\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u4f7f\u7528PyCryptodome\u5e93\u8fdb\u884cAES\u52a0\u5bc6\u548c\u89e3\u5bc6\u7684\u793a\u4f8b\u3002<\/p>\n<\/p>\n<ol>\n<li><strong>\u751f\u6210\u5bc6\u94a5\u548c\u521d\u59cb\u5316\u5411\u91cf\uff08IV\uff09<\/strong><\/p>\n<p>\u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u751f\u6210\u4e00\u4e2a\u5bc6\u94a5\u548c\u521d\u59cb\u5316\u5411\u91cf\u3002\u5bc6\u94a5\u7528\u4e8e\u52a0\u5bc6\u548c\u89e3\u5bc6\uff0c\u800c\u521d\u59cb\u5316\u5411\u91cf\u7528\u4e8e\u786e\u4fdd\u76f8\u540c\u7684\u660e\u6587\u5728\u4e0d\u540c\u7684\u52a0\u5bc6\u64cd\u4f5c\u4e2d\u751f\u6210\u4e0d\u540c\u7684\u5bc6\u6587\u3002<\/li>\n<\/p>\n<\/ol>\n<p><pre><code class=\"language-python\">key = get_random_bytes(16)  # \u751f\u621016\u5b57\u8282\u7684\u5bc6\u94a5<\/p>\n<p>iv = get_random_bytes(16)   # \u751f\u621016\u5b57\u8282\u7684\u521d\u59cb\u5316\u5411\u91cf<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u52a0\u5bc6\u6570\u636e<\/strong><\/p>\n<p>\u4f7f\u7528AES\u7b97\u6cd5\u8fdb\u884c\u52a0\u5bc6\u64cd\u4f5c\u3002AES\u662f\u4e00\u79cd\u5bf9\u79f0\u52a0\u5bc6\u7b97\u6cd5\uff0c\u610f\u5473\u7740\u76f8\u540c\u7684\u5bc6\u94a5\u7528\u4e8e\u52a0\u5bc6\u548c\u89e3\u5bc6\u3002<\/li>\n<\/p>\n<\/ol>\n<p><pre><code class=\"language-python\">cipher = AES.new(key, AES.MODE_CBC, iv)  # \u521b\u5efaAES\u52a0\u5bc6\u5bf9\u8c61<\/p>\n<p>pl<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>ntext = b&#39;This is a secret message&#39;  # \u660e\u6587\u6570\u636e<\/p>\n<p>ciphertext = cipher.encrypt(plaintext.ljust(32))  # \u52a0\u5bc6\u660e\u6587\u6570\u636e\uff0c\u586b\u5145\u523032\u5b57\u8282<\/p>\n<p>print(&quot;\u5bc6\u6587:&quot;, ciphertext)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"3\">\n<li><strong>\u89e3\u5bc6\u6570\u636e<\/strong><\/p>\n<p>\u4f7f\u7528\u76f8\u540c\u7684\u5bc6\u94a5\u548c\u521d\u59cb\u5316\u5411\u91cf\u8fdb\u884c\u89e3\u5bc6\u64cd\u4f5c\u3002<\/li>\n<\/p>\n<\/ol>\n<p><pre><code class=\"language-python\">decipher = AES.new(key, AES.MODE_CBC, iv)  # \u521b\u5efaAES\u89e3\u5bc6\u5bf9\u8c61<\/p>\n<p>decrypted_text = decipher.decrypt(ciphertext).strip()  # \u89e3\u5bc6\u5bc6\u6587\u6570\u636e\u5e76\u53bb\u9664\u586b\u5145<\/p>\n<p>print(&quot;\u89e3\u5bc6\u540e\u7684\u660e\u6587:&quot;, decrypted_text)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u56db\u3001\u52a0\u5bc6\u548c\u89e3\u5bc6\u7684\u5e94\u7528\u573a\u666f<\/strong><\/p>\n<p>\u52a0\u5bc6\u548c\u89e3\u5bc6\u5728\u73b0\u4ee3\u8ba1\u7b97\u673a\u5b89\u5168\u4e2d\u6709\u5e7f\u6cdb\u7684\u5e94\u7528\u3002\u4ee5\u4e0b\u662f\u51e0\u4e2a\u5e38\u89c1\u7684\u5e94\u7528\u573a\u666f\uff1a<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u6570\u636e\u5b58\u50a8\u5b89\u5168<\/strong><\/p>\n<p>\u5728\u6570\u636e\u5e93\u6216\u6587\u4ef6\u7cfb\u7edf\u4e2d\u5b58\u50a8\u654f\u611f\u6570\u636e\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528\u52a0\u5bc6\u6280\u672f\u4fdd\u62a4\u8fd9\u4e9b\u6570\u636e\u3002\u5373\u4f7f\u653b\u51fb\u8005\u83b7\u5f97\u4e86\u5b58\u50a8\u4ecb\u8d28\uff0c\u4ed6\u4eec\u4e5f\u65e0\u6cd5\u8bfb\u53d6\u52a0\u5bc6\u7684\u6570\u636e\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u6570\u636e\u4f20\u8f93\u5b89\u5168<\/strong><\/p>\n<p>\u5728\u7f51\u7edc\u4e0a\u4f20\u8f93\u654f\u611f\u6570\u636e\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528\u52a0\u5bc6\u6280\u672f\u4fdd\u62a4\u6570\u636e\u7684\u673a\u5bc6\u6027\u548c\u5b8c\u6574\u6027\u3002\u4f8b\u5982\uff0cHTTPS\u534f\u8bae\u4f7f\u7528SSL\/TLS\u52a0\u5bc6\u6570\u636e\u4f20\u8f93\uff0c\u786e\u4fdd\u6570\u636e\u5728\u4f20\u8f93\u8fc7\u7a0b\u4e2d\u4e0d\u88ab\u7a83\u542c\u6216\u7be1\u6539\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u7528\u6237\u8ba4\u8bc1<\/strong><\/p>\n<p>\u52a0\u5bc6\u6280\u672f\u5e7f\u6cdb\u7528\u4e8e\u7528\u6237\u8ba4\u8bc1\u7cfb\u7edf\u4e2d\u3002\u4f8b\u5982\uff0c\u5b58\u50a8\u7528\u6237\u5bc6\u7801\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528\u54c8\u5e0c\u7b97\u6cd5\u52a0\u5bc6\u5bc6\u7801\uff0c\u4ee5\u9632\u6b62\u5bc6\u7801\u6cc4\u9732\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u6570\u5b57\u7b7e\u540d<\/strong><\/p>\n<p>\u6570\u5b57\u7b7e\u540d\u4f7f\u7528\u975e\u5bf9\u79f0\u52a0\u5bc6\u6280\u672f\u9a8c\u8bc1\u6570\u636e\u7684\u771f\u5b9e\u6027\u548c\u5b8c\u6574\u6027\u3002\u6570\u5b57\u7b7e\u540d\u5e7f\u6cdb\u7528\u4e8e\u7535\u5b50\u90ae\u4ef6\u3001\u8f6f\u4ef6\u5206\u53d1\u548c\u91d1\u878d\u4ea4\u6613\u4e2d\uff0c\u786e\u4fdd\u6570\u636e\u672a\u88ab\u7be1\u6539\u4e14\u6765\u81ea\u5408\u6cd5\u7684\u53d1\u9001\u8005\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p><strong>\u4e94\u3001\u5e38\u89c1\u7684\u5bf9\u79f0\u52a0\u5bc6\u7b97\u6cd5<\/strong><\/p>\n<p>\u5bf9\u79f0\u52a0\u5bc6\u7b97\u6cd5\u4f7f\u7528\u76f8\u540c\u7684\u5bc6\u94a5\u8fdb\u884c\u52a0\u5bc6\u548c\u89e3\u5bc6\u3002\u4ee5\u4e0b\u662f\u51e0\u4e2a\u5e38\u89c1\u7684\u5bf9\u79f0\u52a0\u5bc6\u7b97\u6cd5\uff1a<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>AES\uff08\u9ad8\u7ea7\u52a0\u5bc6\u6807\u51c6\uff09<\/strong><\/p>\n<p>AES\u662f\u4e00\u79cd\u5e7f\u6cdb\u4f7f\u7528\u7684\u5bf9\u79f0\u52a0\u5bc6\u7b97\u6cd5\uff0c\u652f\u6301128\u3001192\u548c256\u4f4d\u5bc6\u94a5\u957f\u5ea6\u3002AES\u5177\u6709\u9ad8\u6548\u6027\u548c\u5b89\u5168\u6027\uff0c\u5e7f\u6cdb\u5e94\u7528\u4e8e\u5404\u79cd\u52a0\u5bc6\u573a\u666f\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>DES\uff08\u6570\u636e\u52a0\u5bc6\u6807\u51c6\uff09<\/strong><\/p>\n<p>DES\u662f\u4e00\u79cd\u8f83\u65e9\u7684\u5bf9\u79f0\u52a0\u5bc6\u7b97\u6cd5\uff0c\u4f7f\u752856\u4f4d\u5bc6\u94a5\u3002\u7531\u4e8e\u5bc6\u94a5\u957f\u5ea6\u8f83\u77ed\uff0cDES\u4e0d\u518d\u88ab\u8ba4\u4e3a\u662f\u5b89\u5168\u7684\uff0c\u5df2\u88abAES\u53d6\u4ee3\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>Triple DES\uff083DES\uff09<\/strong><\/p>\n<p>Triple DES\u662fDES\u7684\u589e\u5f3a\u7248\uff0c\u901a\u8fc7\u4e09\u6b21\u52a0\u5bc6\u548c\u89e3\u5bc6\u64cd\u4f5c\u589e\u5f3a\u4e86\u5b89\u5168\u6027\u3002\u5c3d\u7ba1\u6bd4DES\u5b89\u5168\uff0c\u4f463DES\u7684\u6548\u7387\u8f83\u4f4e\uff0c\u4e5f\u9010\u6e10\u88abAES\u53d6\u4ee3\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>Blowfish<\/strong><\/p>\n<p>Blowfish\u662f\u4e00\u79cd\u5bf9\u79f0\u52a0\u5bc6\u7b97\u6cd5\uff0c\u652f\u630132\u5230448\u4f4d\u7684\u53ef\u53d8\u5bc6\u94a5\u957f\u5ea6\u3002Blowfish\u5177\u6709\u9ad8\u6548\u6027\u548c\u5b89\u5168\u6027\uff0c\u9002\u7528\u4e8e\u591a\u79cd\u52a0\u5bc6\u573a\u666f\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p><strong>\u516d\u3001\u5e38\u89c1\u7684\u975e\u5bf9\u79f0\u52a0\u5bc6\u7b97\u6cd5<\/strong><\/p>\n<p>\u975e\u5bf9\u79f0\u52a0\u5bc6\u7b97\u6cd5\u4f7f\u7528\u4e00\u5bf9\u5bc6\u94a5\uff1a\u516c\u94a5\u7528\u4e8e\u52a0\u5bc6\uff0c\u79c1\u94a5\u7528\u4e8e\u89e3\u5bc6\u3002\u4ee5\u4e0b\u662f\u51e0\u4e2a\u5e38\u89c1\u7684\u975e\u5bf9\u79f0\u52a0\u5bc6\u7b97\u6cd5\uff1a<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>RSA<\/strong><\/p>\n<p>RSA\u662f\u4e00\u79cd\u5e7f\u6cdb\u4f7f\u7528\u7684\u975e\u5bf9\u79f0\u52a0\u5bc6\u7b97\u6cd5\uff0c\u652f\u6301\u591a\u79cd\u5bc6\u94a5\u957f\u5ea6\u3002RSA\u5e7f\u6cdb\u5e94\u7528\u4e8e\u6570\u636e\u52a0\u5bc6\u3001\u6570\u5b57\u7b7e\u540d\u548c\u5bc6\u94a5\u4ea4\u6362\u7b49\u573a\u666f\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>ECC\uff08\u692d\u5706\u66f2\u7ebf\u52a0\u5bc6\uff09<\/strong><\/p>\n<p>ECC\u662f\u4e00\u79cd\u65b0\u578b\u7684\u975e\u5bf9\u79f0\u52a0\u5bc6\u7b97\u6cd5\uff0c\u5177\u6709\u8f83\u9ad8\u7684\u5b89\u5168\u6027\u548c\u8f83\u5c0f\u7684\u5bc6\u94a5\u957f\u5ea6\u3002ECC\u5728\u79fb\u52a8\u8bbe\u5907\u548c\u8d44\u6e90\u53d7\u9650\u7684\u73af\u5883\u4e2d\u5f97\u5230\u5e7f\u6cdb\u5e94\u7528\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>DSA\uff08\u6570\u5b57\u7b7e\u540d\u7b97\u6cd5\uff09<\/strong><\/p>\n<p>DSA\u662f\u4e00\u79cd\u4e13\u7528\u4e8e\u6570\u5b57\u7b7e\u540d\u7684\u975e\u5bf9\u79f0\u52a0\u5bc6\u7b97\u6cd5\uff0c\u5e7f\u6cdb\u5e94\u7528\u4e8e\u7535\u5b50\u7b7e\u540d\u548c\u8eab\u4efd\u9a8c\u8bc1\u7b49\u573a\u666f\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p><strong>\u4e03\u3001\u54c8\u5e0c\u548c\u6d88\u606f\u6458\u8981\u7b97\u6cd5<\/strong><\/p>\n<p>\u54c8\u5e0c\u7b97\u6cd5\u7528\u4e8e\u5c06\u4efb\u610f\u957f\u5ea6\u7684\u6570\u636e\u6620\u5c04\u4e3a\u56fa\u5b9a\u957f\u5ea6\u7684\u54c8\u5e0c\u503c\u3002\u54c8\u5e0c\u7b97\u6cd5\u5e7f\u6cdb\u5e94\u7528\u4e8e\u6570\u636e\u5b8c\u6574\u6027\u9a8c\u8bc1\u3001\u6570\u5b57\u7b7e\u540d\u548c\u5bc6\u7801\u5b58\u50a8\u7b49\u573a\u666f\u3002\u4ee5\u4e0b\u662f\u51e0\u4e2a\u5e38\u89c1\u7684\u54c8\u5e0c\u7b97\u6cd5\uff1a<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>MD5<\/strong><\/p>\n<p>MD5\u662f\u4e00\u79cd\u8f83\u65e9\u7684\u54c8\u5e0c\u7b97\u6cd5\uff0c\u751f\u6210128\u4f4d\u7684\u54c8\u5e0c\u503c\u3002\u7531\u4e8e\u5b58\u5728\u5b89\u5168\u6f0f\u6d1e\uff0cMD5\u4e0d\u518d\u88ab\u8ba4\u4e3a\u662f\u5b89\u5168\u7684\uff0c\u5df2\u9010\u6e10\u88abSHA-256\u7b49\u7b97\u6cd5\u53d6\u4ee3\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>SHA-1<\/strong><\/p>\n<p>SHA-1\u662f\u4e00\u79cd\u5e7f\u6cdb\u4f7f\u7528\u7684\u54c8\u5e0c\u7b97\u6cd5\uff0c\u751f\u6210160\u4f4d\u7684\u54c8\u5e0c\u503c\u3002\u5c3d\u7ba1SHA-1\u8f83MD5\u5b89\u5168\uff0c\u4f46\u4e5f\u5b58\u5728\u5b89\u5168\u6f0f\u6d1e\uff0c\u9010\u6e10\u88abSHA-256\u7b49\u7b97\u6cd5\u53d6\u4ee3\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>SHA-256<\/strong><\/p>\n<p>SHA-256\u662fSHA-2\u5bb6\u65cf\u7684\u4e00\u5458\uff0c\u751f\u6210256\u4f4d\u7684\u54c8\u5e0c\u503c\u3002SHA-256\u5177\u6709\u8f83\u9ad8\u7684\u5b89\u5168\u6027\uff0c\u5e7f\u6cdb\u5e94\u7528\u4e8e\u6570\u636e\u5b8c\u6574\u6027\u9a8c\u8bc1\u548c\u5bc6\u7801\u5b58\u50a8\u7b49\u573a\u666f\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>SHA-3<\/strong><\/p>\n<p>SHA-3\u662fSHA\u5bb6\u65cf\u7684\u6700\u65b0\u6210\u5458\uff0c\u5177\u6709\u66f4\u9ad8\u7684\u5b89\u5168\u6027\u548c\u7075\u6d3b\u6027\u3002SHA-3\u652f\u6301\u591a\u79cd\u54c8\u5e0c\u957f\u5ea6\uff0c\u9002\u7528\u4e8e\u591a\u79cd\u5e94\u7528\u573a\u666f\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p><strong>\u516b\u3001\u5bc6\u94a5\u7ba1\u7406<\/strong><\/p>\n<p>\u5bc6\u94a5\u7ba1\u7406\u662f\u52a0\u5bc6\u7cfb\u7edf\u4e2d\u7684\u5173\u952e\u73af\u8282\uff0c\u5305\u62ec\u5bc6\u94a5\u7684\u751f\u6210\u3001\u5b58\u50a8\u3001\u5206\u53d1\u548c\u9500\u6bc1\u7b49\u3002\u4ee5\u4e0b\u662f\u51e0\u4e2a\u5bc6\u94a5\u7ba1\u7406\u7684\u6700\u4f73\u5b9e\u8df5\uff1a<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u5bc6\u94a5\u751f\u6210<\/strong><\/p>\n<p>\u4f7f\u7528\u5b89\u5168\u7684\u968f\u673a\u6570\u751f\u6210\u5668\u751f\u6210\u5bc6\u94a5\uff0c\u786e\u4fdd\u5bc6\u94a5\u7684\u552f\u4e00\u6027\u548c\u4e0d\u53ef\u9884\u6d4b\u6027\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u4f7f\u7528PyCryptodome\u5e93\u7684get_random_bytes\u51fd\u6570\u751f\u6210\u5bc6\u94a5\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u5bc6\u94a5\u5b58\u50a8<\/strong><\/p>\n<p>\u5c06\u5bc6\u94a5\u5b58\u50a8\u5728\u5b89\u5168\u7684\u4f4d\u7f6e\uff0c\u9632\u6b62\u672a\u7ecf\u6388\u6743\u7684\u8bbf\u95ee\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u4f7f\u7528\u786c\u4ef6\u5b89\u5168\u6a21\u5757\uff08HSM\uff09\u6216\u64cd\u4f5c\u7cfb\u7edf\u63d0\u4f9b\u7684\u5bc6\u94a5\u5b58\u50a8\u670d\u52a1\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u5bc6\u94a5\u5206\u53d1<\/strong><\/p>\n<p>\u4f7f\u7528\u5b89\u5168\u7684\u5bc6\u94a5\u5206\u53d1\u673a\u5236\uff0c\u786e\u4fdd\u5bc6\u94a5\u5728\u4f20\u8f93\u8fc7\u7a0b\u4e2d\u4e0d\u88ab\u7a83\u542c\u6216\u7be1\u6539\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u4f7f\u7528\u975e\u5bf9\u79f0\u52a0\u5bc6\u7b97\u6cd5\u8fdb\u884c\u5bc6\u94a5\u4ea4\u6362\uff0c\u786e\u4fdd\u5bc6\u94a5\u7684\u5b89\u5168\u5206\u53d1\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u5bc6\u94a5\u9500\u6bc1<\/strong><\/p>\n<p>\u5728\u5bc6\u94a5\u4e0d\u518d\u9700\u8981\u65f6\uff0c\u786e\u4fdd\u5b89\u5168\u9500\u6bc1\u5bc6\u94a5\uff0c\u9632\u6b62\u5bc6\u94a5\u6cc4\u9732\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u5c06\u5bc6\u94a5\u4ece\u5185\u5b58\u4e2d\u6e05\u9664\uff0c\u5e76\u4f7f\u7528\u5b89\u5168\u5220\u9664\u5de5\u5177\u5220\u9664\u5b58\u50a8\u4ecb\u8d28\u4e0a\u7684\u5bc6\u94a5\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p><strong>\u4e5d\u3001\u603b\u7ed3<\/strong><\/p>\n<p>\u52a0\u5bc6\u6280\u672f\u5728\u73b0\u4ee3\u8ba1\u7b97\u673a\u5b89\u5168\u4e2d\u8d77\u7740\u81f3\u5173\u91cd\u8981\u7684\u4f5c\u7528\u3002\u672c\u6587\u8be6\u7ec6\u4ecb\u7ecd\u4e86\u5728Python\u4e2d\u5f15\u5165crypto\u5e93\u7684\u4e09\u79cd\u65b9\u5f0f\uff0c\u5e76\u91cd\u70b9\u4ecb\u7ecd\u4e86PyCryptodome\u5e93\u7684\u4f7f\u7528\u3002\u901a\u8fc7\u5b66\u4e60\u548c\u5e94\u7528\u8fd9\u4e9b\u52a0\u5bc6\u6280\u672f\uff0c\u53ef\u4ee5\u6709\u6548\u4fdd\u62a4\u6570\u636e\u7684\u673a\u5bc6\u6027\u3001\u5b8c\u6574\u6027\u548c\u771f\u5b9e\u6027\u3002\u5e0c\u671b\u672c\u6587\u5bf9\u60a8\u6709\u6240\u5e2e\u52a9\uff0c\u80fd\u591f\u5728\u5b9e\u9645\u9879\u76ee\u4e2d\u5e94\u7528\u52a0\u5bc6\u6280\u672f\uff0c\u63d0\u5347\u7cfb\u7edf\u7684\u5b89\u5168\u6027\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u5b89\u88c5crypto\u5e93\uff1f<\/strong><br \/>\u8981\u5728Python\u4e2d\u4f7f\u7528crypto\u5e93\uff0c\u60a8\u9700\u8981\u9996\u5148\u5b89\u88c5\u5b83\u3002\u53ef\u4ee5\u901a\u8fc7Python\u7684\u5305\u7ba1\u7406\u5de5\u5177pip\u6765\u5b9e\u73b0\u3002\u5728\u547d\u4ee4\u884c\u4e2d\u8f93\u5165\u4ee5\u4e0b\u547d\u4ee4\uff1a<code>pip install pycryptodome<\/code>\u3002\u5b89\u88c5\u5b8c\u6210\u540e\uff0c\u60a8\u5c31\u53ef\u4ee5\u5728\u4ee3\u7801\u4e2d\u5f15\u5165\u8be5\u5e93\u3002<\/p>\n<p><strong>crypto\u5e93\u63d0\u4f9b\u4e86\u54ea\u4e9b\u52a0\u5bc6\u7b97\u6cd5\uff1f<\/strong><br \/>crypto\u5e93\uff08\u7279\u522b\u662fpycryptodome\uff09\u652f\u6301\u591a\u79cd\u52a0\u5bc6\u7b97\u6cd5\uff0c\u5305\u62ec\u5bf9\u79f0\u52a0\u5bc6\uff08\u5982AES\u548cDES\uff09\u3001\u975e\u5bf9\u79f0\u52a0\u5bc6\uff08\u5982RSA\u548cDSA\uff09\u4ee5\u53ca\u54c8\u5e0c\u51fd\u6570\uff08\u5982SHA-256\u548cMD5\uff09\u3002\u8fd9\u4e9b\u7b97\u6cd5\u53ef\u4ee5\u6ee1\u8db3\u4e0d\u540c\u7684\u5b89\u5168\u9700\u6c42\uff0c\u9002\u7528\u4e8e\u6570\u636e\u52a0\u5bc6\u3001\u7b7e\u540d\u7b49\u591a\u79cd\u573a\u666f\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728\u4ee3\u7801\u4e2d\u4f7f\u7528crypto\u5e93\u8fdb\u884c\u57fa\u672c\u52a0\u5bc6\u64cd\u4f5c\uff1f<\/strong><br \/>\u5728\u4ee3\u7801\u4e2d\u5f15\u5165crypto\u5e93\u540e\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u793a\u4f8b\u8fdb\u884c\u57fa\u672c\u7684AES\u52a0\u5bc6\u64cd\u4f5c\uff1a<\/p>\n<pre><code class=\"language-python\">from Crypto.Cipher import AES\nfrom Crypto.Random import get_random_bytes\n\nkey = get_random_bytes(16)  # \u751f\u621016\u5b57\u8282\u7684\u968f\u673a\u5bc6\u94a5\ncipher = AES.new(key, AES.MODE_EAX)  # \u521b\u5efa\u4e00\u4e2aAES\u52a0\u5bc6\u5668\nnonce = cipher.nonce  # \u83b7\u53d6\u968f\u673a\u6570\nciphertext, tag = cipher.encrypt_and_digest(b&#39;Hello World&#39;)  # \u52a0\u5bc6\u6570\u636e\n<\/code><\/pre>\n<p>\u6b64\u793a\u4f8b\u5c55\u793a\u4e86\u5982\u4f55\u751f\u6210\u5bc6\u94a5\u3001\u521b\u5efa\u52a0\u5bc6\u5668\u5e76\u52a0\u5bc6\u4e00\u6bb5\u6570\u636e\uff0c\u60a8\u53ef\u4ee5\u6839\u636e\u9700\u6c42\u8c03\u6574\u4ee3\u7801\u4ee5\u5b9e\u73b0\u66f4\u590d\u6742\u7684\u529f\u80fd\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u5f15\u5165crypto\u5e93\u53ef\u4ee5\u4f7f\u7528PyCrypto\u5e93\u3001Cryptography\u5e93\u3001PyCryptodo [&hellip;]","protected":false},"author":3,"featured_media":0,"comment_status":"","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1175327"}],"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=1175327"}],"version-history":[{"count":0,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1175327\/revisions"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1175327"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1175327"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1175327"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}