{"id":1114492,"date":"2025-01-08T17:57:51","date_gmt":"2025-01-08T09:57:51","guid":{"rendered":""},"modified":"2025-01-08T17:58:12","modified_gmt":"2025-01-08T09:58:12","slug":"python%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8md5%e5%92%8caes%e5%8a%a0%e5%af%86","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1114492.html","title":{"rendered":"python\u5982\u4f55\u4f7f\u7528MD5\u548cAES\u52a0\u5bc6"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25075639\/78212229-acc0-4d2e-a3e6-6124403b880d.webp\" alt=\"python\u5982\u4f55\u4f7f\u7528MD5\u548cAES\u52a0\u5bc6\" \/><\/p>\n<p><p> <strong>Python\u53ef\u4ee5\u4f7f\u7528MD5\u548cAES\u52a0\u5bc6\u6765\u4fdd\u62a4\u6570\u636e\u7684\u5b8c\u6574\u6027\u548c\u5b89\u5168\u6027\u3002\u9996\u5148\uff0cMD5\u662f\u4e00\u79cd\u5e38\u7528\u7684\u54c8\u5e0c\u51fd\u6570\uff0c\u7528\u4e8e\u751f\u6210\u6570\u636e\u7684\u552f\u4e00\u54c8\u5e0c\u503c\uff0c\u786e\u4fdd\u6570\u636e\u672a\u88ab\u7be1\u6539\u3002\u5176\u6b21\uff0cAES\u662f\u4e00\u79cd\u5bf9\u79f0\u52a0\u5bc6\u7b97\u6cd5\uff0c\u7528\u4e8e\u5728\u6570\u636e\u4f20\u8f93\u8fc7\u7a0b\u4e2d\u4fdd\u62a4\u6570\u636e\u7684\u673a\u5bc6\u6027\u3002\u4ee5\u4e0b\u662f\u5982\u4f55\u5728Python\u4e2d\u4f7f\u7528\u8fd9\u4e24\u79cd\u52a0\u5bc6\u65b9\u6cd5\u7684\u8be6\u7ec6\u8bf4\u660e\uff1a<\/strong><\/p>\n<\/p>\n<p><p><strong>MD5\u54c8\u5e0c\u51fd\u6570\u3001AES\u5bf9\u79f0\u52a0\u5bc6<\/strong><\/p>\n<\/p>\n<p><h3>\u4e00\u3001MD5\u54c8\u5e0c\u51fd\u6570<\/h3>\n<\/p>\n<p><p>MD5\uff08Message-Digest Algorithm 5\uff09\u662f\u4e00\u79cd\u5e7f\u6cdb\u4f7f\u7528\u7684\u54c8\u5e0c\u51fd\u6570\uff0c\u53ef\u4ee5\u751f\u6210\u4e00\u4e2a128\u4f4d\u7684\u54c8\u5e0c\u503c\uff0832\u4e2a\u5341\u516d\u8fdb\u5236\u5b57\u7b26\uff09\u3002\u5c3d\u7ba1MD5\u5df2\u7ecf\u88ab\u8bc1\u660e\u4e0d\u591f\u5b89\u5168\uff0c\u4f46\u5b83\u4ecd\u7136\u5728\u8bb8\u591a\u5e94\u7528\u4e2d\u7528\u4e8e\u9a8c\u8bc1\u6570\u636e\u5b8c\u6574\u6027\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u5bfc\u5165\u5e93<\/h4>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u4f7f\u7528hashlib\u5e93\u6765\u8fdb\u884cMD5\u54c8\u5e0c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import hashlib<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u751f\u6210MD5\u54c8\u5e0c<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">def generate_md5_hash(data):<\/p>\n<p>    md5_hash = hashlib.md5(data.encode())<\/p>\n<p>    return md5_hash.hexdigest()<\/p>\n<p>data = &quot;Hello, World!&quot;<\/p>\n<p>md5_result = generate_md5_hash(data)<\/p>\n<p>print(f&quot;MD5 Hash: {md5_result}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u521b\u5efa\u4e86\u4e00\u4e2a\u51fd\u6570<code>generate_md5_hash<\/code>\uff0c\u5b83\u63a5\u53d7\u4e00\u4e2a\u5b57\u7b26\u4e32\u8f93\u5165\u5e76\u8fd4\u56de\u5b83\u7684MD5\u54c8\u5e0c\u503c\u3002<\/p>\n<\/p>\n<p><h4>3\u3001\u9a8c\u8bc1\u6570\u636e\u5b8c\u6574\u6027<\/h4>\n<\/p>\n<p><p>\u4f60\u53ef\u4ee5\u4f7f\u7528\u751f\u6210\u7684MD5\u54c8\u5e0c\u503c\u6765\u9a8c\u8bc1\u6570\u636e\u7684\u5b8c\u6574\u6027\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def verify_md5_hash(data, hash_value):<\/p>\n<p>    return generate_md5_hash(data) == hash_value<\/p>\n<p>is_valid = verify_md5_hash(data, md5_result)<\/p>\n<p>print(f&quot;Data is valid: {is_valid}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001AES\u5bf9\u79f0\u52a0\u5bc6<\/h3>\n<\/p>\n<p><p>AES\uff08Advanced Encryption Standard\uff09\u662f\u4e00\u79cd\u5bf9\u79f0\u52a0\u5bc6\u7b97\u6cd5\uff0c\u652f\u6301128\u4f4d\u3001192\u4f4d\u548c256\u4f4d\u5bc6\u94a5\u957f\u5ea6\u3002\u5b83\u662f\u76ee\u524d\u6700\u5e38\u7528\u7684\u52a0\u5bc6\u7b97\u6cd5\u4e4b\u4e00\uff0c\u7528\u4e8e\u4fdd\u62a4\u6570\u636e\u7684\u673a\u5bc6\u6027\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u5bfc\u5165\u5e93<\/h4>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u4f7f\u7528PyCryptodome\u5e93\u6765\u8fdb\u884cAES\u52a0\u5bc6\u3002\u9996\u5148\u9700\u8981\u5b89\u88c5\u8fd9\u4e2a\u5e93\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install pycryptodome<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u7136\u540e\u5bfc\u5165\u76f8\u5173\u6a21\u5757\uff1a<\/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>import base64<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u751f\u6210\u5bc6\u94a5\u548cIV<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">key = get_random_bytes(16)  # \u751f\u6210\u4e00\u4e2a16\u5b57\u8282\u7684\u5bc6\u94a5\uff08128\u4f4d\uff09<\/p>\n<p>iv = get_random_bytes(16)   # \u751f\u6210\u4e00\u4e2a16\u5b57\u8282\u7684\u521d\u59cb\u5316\u5411\u91cf\uff08IV\uff09<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3\u3001\u52a0\u5bc6\u6570\u636e<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">def encrypt_aes(data, key, iv):<\/p>\n<p>    cipher = AES.new(key, AES.MODE_CBC, iv)<\/p>\n<p>    # \u586b\u5145\u6570\u636e\u523016\u5b57\u8282\u7684\u500d\u6570<\/p>\n<p>    pad_len = 16 - len(data) % 16<\/p>\n<p>    data += chr(pad_len) * pad_len<\/p>\n<p>    encrypted_data = cipher.encrypt(data.encode())<\/p>\n<p>    return base64.b64encode(iv + encrypted_data).decode(&#39;utf-8&#39;)<\/p>\n<p>data = &quot;Hello, World!&quot;<\/p>\n<p>encrypted_data = encrypt_aes(data, key, iv)<\/p>\n<p>print(f&quot;Encrypted Data: {encrypted_data}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u521b\u5efa\u4e86\u4e00\u4e2a\u51fd\u6570<code>encrypt_aes<\/code>\uff0c\u5b83\u63a5\u53d7\u4e00\u4e2a\u5b57\u7b26\u4e32\u8f93\u5165\u3001\u5bc6\u94a5\u548cIV\uff0c\u5e76\u8fd4\u56de\u52a0\u5bc6\u540e\u7684\u6570\u636e\u3002\u6211\u4eec\u4f7f\u7528CBC\uff08Cipher Block Ch<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>ning\uff09\u6a21\u5f0f\u8fdb\u884c\u52a0\u5bc6\uff0c\u5e76\u4f7f\u7528PKCS7\u586b\u5145\u6765\u786e\u4fdd\u6570\u636e\u957f\u5ea6\u662f16\u5b57\u8282\u7684\u500d\u6570\u3002<\/p>\n<\/p>\n<p><h4>4\u3001\u89e3\u5bc6\u6570\u636e<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">def decrypt_aes(encrypted_data, key):<\/p>\n<p>    encrypted_data = base64.b64decode(encrypted_data)<\/p>\n<p>    iv = encrypted_data[:16]<\/p>\n<p>    encrypted_data = encrypted_data[16:]<\/p>\n<p>    cipher = AES.new(key, AES.MODE_CBC, iv)<\/p>\n<p>    decrypted_data = cipher.decrypt(encrypted_data).decode(&#39;utf-8&#39;)<\/p>\n<p>    pad_len = ord(decrypted_data[-1])<\/p>\n<p>    return decrypted_data[:-pad_len]<\/p>\n<p>decrypted_data = decrypt_aes(encrypted_data, key)<\/p>\n<p>print(f&quot;Decrypted Data: {decrypted_data}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u521b\u5efa\u4e86\u4e00\u4e2a\u51fd\u6570<code>decrypt_aes<\/code>\uff0c\u5b83\u63a5\u53d7\u52a0\u5bc6\u540e\u7684\u6570\u636e\u548c\u5bc6\u94a5\uff0c\u5e76\u8fd4\u56de\u89e3\u5bc6\u540e\u7684\u6570\u636e\u3002\u6211\u4eec\u9996\u5148\u63d0\u53d6IV\uff0c\u7136\u540e\u4f7f\u7528\u76f8\u540c\u7684\u5bc6\u94a5\u548cIV\u8fdb\u884c\u89e3\u5bc6\uff0c\u6700\u540e\u79fb\u9664\u586b\u5145\u6570\u636e\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p><strong>MD5\u54c8\u5e0c\u51fd\u6570\u548cAES\u5bf9\u79f0\u52a0\u5bc6\u5728Python\u4e2d\u7684\u4f7f\u7528\u975e\u5e38\u7b80\u5355\u548c\u9ad8\u6548\u3002MD5\u7528\u4e8e\u9a8c\u8bc1\u6570\u636e\u7684\u5b8c\u6574\u6027\uff0c\u800cAES\u7528\u4e8e\u4fdd\u62a4\u6570\u636e\u7684\u673a\u5bc6\u6027\u3002<\/strong>\u867d\u7136MD5\u4e0d\u518d\u88ab\u8ba4\u4e3a\u662f\u5b89\u5168\u7684\u54c8\u5e0c\u7b97\u6cd5\uff0c\u4f46\u5728\u67d0\u4e9b\u5e94\u7528\u4e2d\u5b83\u4ecd\u7136\u6709\u7528\u3002AES\u662f\u76ee\u524d\u6700\u5e38\u7528\u548c\u6700\u5b89\u5168\u7684\u5bf9\u79f0\u52a0\u5bc6\u7b97\u6cd5\u4e4b\u4e00\u3002\u901a\u8fc7\u7ed3\u5408\u4f7f\u7528\u8fd9\u4e24\u79cd\u65b9\u6cd5\uff0c\u53ef\u4ee5\u786e\u4fdd\u6570\u636e\u5728\u4f20\u8f93\u548c\u5b58\u50a8\u8fc7\u7a0b\u4e2d\u7684\u5b89\u5168\u6027\u548c\u5b8c\u6574\u6027\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u5b9e\u9645\u5e94\u7528\u4e2d\u7684\u6ce8\u610f\u4e8b\u9879<\/h3>\n<\/p>\n<p><h4>1\u3001MD5\u7684\u5b89\u5168\u6027\u95ee\u9898<\/h4>\n<\/p>\n<p><p>\u867d\u7136MD5\u5728\u8bb8\u591a\u60c5\u51b5\u4e0b\u4ecd\u7136\u6709\u7528\uff0c\u4f46\u5b83\u5bb9\u6613\u53d7\u5230\u78b0\u649e\u653b\u51fb\u3002\u56e0\u6b64\uff0c\u5efa\u8bae\u5728\u9700\u8981\u9ad8\u5b89\u5168\u6027\u7684\u5e94\u7528\u4e2d\u4f7f\u7528\u66f4\u5b89\u5168\u7684\u54c8\u5e0c\u7b97\u6cd5\uff0c\u5982SHA-256\u3002<\/p>\n<\/p>\n<p><h4>2\u3001AES\u7684\u5bc6\u94a5\u7ba1\u7406<\/h4>\n<\/p>\n<p><p>\u5bc6\u94a5\u7ba1\u7406\u662fAES\u52a0\u5bc6\u8fc7\u7a0b\u4e2d\u6700\u5173\u952e\u7684\u4e00\u73af\u3002\u786e\u4fdd\u5bc6\u94a5\u7684\u5b89\u5168\u5b58\u50a8\u548c\u4f20\u8f93\u81f3\u5173\u91cd\u8981\uff0c\u53ef\u4ee5\u4f7f\u7528\u5b89\u5168\u7684\u5bc6\u94a5\u7ba1\u7406\u7cfb\u7edf\uff08KMS\uff09\u6765\u7ba1\u7406\u5bc6\u94a5\u3002<\/p>\n<\/p>\n<p><h4>3\u3001\u586b\u5145\u65b9\u5f0f<\/h4>\n<\/p>\n<p><p>\u5728AES\u52a0\u5bc6\u8fc7\u7a0b\u4e2d\uff0c\u6570\u636e\u9700\u8981\u586b\u5145\u523016\u5b57\u8282\u7684\u500d\u6570\u3002\u5e38\u7528\u7684\u586b\u5145\u65b9\u5f0f\u5305\u62ecPKCS7\u548cZero Padding\uff0c\u9009\u62e9\u5408\u9002\u7684\u586b\u5145\u65b9\u5f0f\u53ef\u4ee5\u63d0\u9ad8\u52a0\u5bc6\u7684\u5b89\u5168\u6027\u548c\u517c\u5bb9\u6027\u3002<\/p>\n<\/p>\n<p><h4>4\u3001\u521d\u59cb\u5316\u5411\u91cf\uff08IV\uff09<\/h4>\n<\/p>\n<p><p>\u521d\u59cb\u5316\u5411\u91cf\uff08IV\uff09\u5728AES\u52a0\u5bc6\u4e2d\u8d77\u7740\u81f3\u5173\u91cd\u8981\u7684\u4f5c\u7528\u3002IV\u7684\u968f\u673a\u6027\u53ef\u4ee5\u786e\u4fdd\u76f8\u540c\u7684\u660e\u6587\u5728\u6bcf\u6b21\u52a0\u5bc6\u65f6\u4ea7\u751f\u4e0d\u540c\u7684\u5bc6\u6587\uff0c\u63d0\u9ad8\u52a0\u5bc6\u7684\u5b89\u5168\u6027\u3002IV\u4e0d\u9700\u8981\u4fdd\u5bc6\uff0c\u4f46\u9700\u8981\u5728\u89e3\u5bc6\u65f6\u4f7f\u7528\u76f8\u540c\u7684IV\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u5b9e\u4f8b\u4ee3\u7801\u6c47\u603b<\/h3>\n<\/p>\n<p><pre><code class=\"language-python\">import hashlib<\/p>\n<p>from Crypto.Cipher import AES<\/p>\n<p>from Crypto.Random import get_random_bytes<\/p>\n<p>import base64<\/p>\n<h2><strong>MD5\u54c8\u5e0c\u51fd\u6570<\/strong><\/h2>\n<p>def generate_md5_hash(data):<\/p>\n<p>    md5_hash = hashlib.md5(data.encode())<\/p>\n<p>    return md5_hash.hexdigest()<\/p>\n<p>def verify_md5_hash(data, hash_value):<\/p>\n<p>    return generate_md5_hash(data) == hash_value<\/p>\n<h2><strong>AES\u5bf9\u79f0\u52a0\u5bc6<\/strong><\/h2>\n<p>def encrypt_aes(data, key, iv):<\/p>\n<p>    cipher = AES.new(key, AES.MODE_CBC, iv)<\/p>\n<p>    pad_len = 16 - len(data) % 16<\/p>\n<p>    data += chr(pad_len) * pad_len<\/p>\n<p>    encrypted_data = cipher.encrypt(data.encode())<\/p>\n<p>    return base64.b64encode(iv + encrypted_data).decode(&#39;utf-8&#39;)<\/p>\n<p>def decrypt_aes(encrypted_data, key):<\/p>\n<p>    encrypted_data = base64.b64decode(encrypted_data)<\/p>\n<p>    iv = encrypted_data[:16]<\/p>\n<p>    encrypted_data = encrypted_data[16:]<\/p>\n<p>    cipher = AES.new(key, AES.MODE_CBC, iv)<\/p>\n<p>    decrypted_data = cipher.decrypt(encrypted_data).decode(&#39;utf-8&#39;)<\/p>\n<p>    pad_len = ord(decrypted_data[-1])<\/p>\n<p>    return decrypted_data[:-pad_len]<\/p>\n<h2><strong>\u793a\u4f8b<\/strong><\/h2>\n<p>data = &quot;Hello, World!&quot;<\/p>\n<h2><strong>MD5\u54c8\u5e0c<\/strong><\/h2>\n<p>md5_result = generate_md5_hash(data)<\/p>\n<p>print(f&quot;MD5 Hash: {md5_result}&quot;)<\/p>\n<p>is_valid = verify_md5_hash(data, md5_result)<\/p>\n<p>print(f&quot;Data is valid: {is_valid}&quot;)<\/p>\n<h2><strong>AES\u52a0\u5bc6\/\u89e3\u5bc6<\/strong><\/h2>\n<p>key = get_random_bytes(16)<\/p>\n<p>iv = get_random_bytes(16)<\/p>\n<p>encrypted_data = encrypt_aes(data, key, iv)<\/p>\n<p>print(f&quot;Encrypted Data: {encrypted_data}&quot;)<\/p>\n<p>decrypted_data = decrypt_aes(encrypted_data, key)<\/p>\n<p>print(f&quot;Decrypted Data: {decrypted_data}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u4e0a\u8ff0\u4ee3\u7801\u793a\u4f8b\uff0c\u53ef\u4ee5\u66f4\u597d\u5730\u7406\u89e3\u548c\u638c\u63e1MD5\u54c8\u5e0c\u51fd\u6570\u548cAES\u5bf9\u79f0\u52a0\u5bc6\u5728Python\u4e2d\u7684\u4f7f\u7528\u65b9\u6cd5\u3002\u5e0c\u671b\u8fd9\u4e9b\u5185\u5bb9\u5bf9\u4f60\u6709\u6240\u5e2e\u52a9\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u4ec0\u4e48\u662fMD5\u548cAES\u52a0\u5bc6\uff0c\u5b83\u4eec\u5404\u81ea\u7684\u7528\u9014\u662f\u4ec0\u4e48\uff1f<\/strong><br \/>MD5\uff08Message-Digest Algorithm 5\uff09\u662f\u4e00\u79cd\u5e7f\u6cdb\u4f7f\u7528\u7684\u54c8\u5e0c\u51fd\u6570\uff0c\u901a\u5e38\u7528\u4e8e\u786e\u4fdd\u6570\u636e\u7684\u5b8c\u6574\u6027\u3002\u5b83\u5c06\u4efb\u610f\u5927\u5c0f\u7684\u6570\u636e\u8f93\u5165\u8f6c\u6362\u4e3a\u56fa\u5b9a\u5927\u5c0f\u7684128\u4f4d\u54c8\u5e0c\u503c\uff0c\u5e38\u7528\u4e8e\u9a8c\u8bc1\u6587\u4ef6\u6216\u6d88\u606f\u662f\u5426\u88ab\u7be1\u6539\u3002AES\uff08Advanced Encryption Standard\uff09\u662f\u4e00\u79cd\u5bf9\u79f0\u52a0\u5bc6\u7b97\u6cd5\uff0c\u4e3b\u8981\u7528\u4e8e\u52a0\u5bc6\u6570\u636e\u4ee5\u786e\u4fdd\u5176\u673a\u5bc6\u6027\u3002AES\u80fd\u591f\u5904\u7406\u4e0d\u540c\u957f\u5ea6\u7684\u5bc6\u94a5\uff08128\u4f4d\u3001192\u4f4d\u548c256\u4f4d\uff09\uff0c\u5728\u6570\u636e\u4fdd\u62a4\u548c\u5b89\u5168\u4f20\u8f93\u4e2d\u88ab\u5e7f\u6cdb\u5e94\u7528\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u5982\u4f55\u5b9e\u73b0MD5\u548cAES\u52a0\u5bc6\uff1f<\/strong><br \/>\u8981\u5728Python\u4e2d\u4f7f\u7528MD5\u548cAES\u52a0\u5bc6\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528\u5185\u7f6e\u7684<code>hashlib<\/code>\u5e93\u6765\u751f\u6210MD5\u54c8\u5e0c\u503c\uff0c\u800c\u4f7f\u7528<code>pycryptodome<\/code>\u5e93\u6765\u6267\u884cAES\u52a0\u5bc6\u3002\u9996\u5148\uff0c\u5b89\u88c5<code>pycryptodome<\/code>\u5e93\uff0c\u4f7f\u7528<code>pip install pycryptodome<\/code>\u3002\u751f\u6210MD5\u54c8\u5e0c\u503c\u7684\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a  <\/p>\n<pre><code class=\"language-python\">import hashlib\n\ndata = &quot;\u8981\u52a0\u5bc6\u7684\u5b57\u7b26\u4e32&quot;\nmd5_hash = hashlib.md5(data.encode()).hexdigest()\nprint(&quot;MD5\u54c8\u5e0c\u503c\uff1a&quot;, md5_hash)\n<\/code><\/pre>\n<p>\u800cAES\u52a0\u5bc6\u7684\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a  <\/p>\n<pre><code class=\"language-python\">from Crypto.Cipher import AES\nfrom Crypto.Util.Padding import pad, unpad\nimport os\n\nkey = os.urandom(16)  # \u751f\u6210\u968f\u673a16\u5b57\u8282\u5bc6\u94a5\ncipher = AES.new(key, AES.MODE_CBC)\ndata = b&quot;\u8981\u52a0\u5bc6\u7684\u6d88\u606f&quot;\nct_bytes = cipher.encrypt(pad(data, AES.block_size))\nprint(&quot;\u52a0\u5bc6\u540e\u7684\u6570\u636e\uff1a&quot;, ct_bytes)\n<\/code><\/pre>\n<p>\u786e\u4fdd\u5728\u4f7f\u7528AES\u52a0\u5bc6\u65f6\uff0c\u5904\u7406\u597d\u5bc6\u94a5\u548c\u521d\u59cb\u5316\u5411\u91cf\uff08IV\uff09\u7684\u5b89\u5168\u6027\u3002<\/p>\n<p><strong>\u5728\u8fdb\u884cMD5\u548cAES\u52a0\u5bc6\u65f6\u9700\u8981\u6ce8\u610f\u54ea\u4e9b\u5b89\u5168\u95ee\u9898\uff1f<\/strong><br \/>\u5728\u4f7f\u7528MD5\u65f6\uff0c\u7531\u4e8e\u5176\u5df2\u88ab\u8bc1\u660e\u5b58\u5728\u78b0\u649e\u653b\u51fb\u7684\u98ce\u9669\uff0c\u5efa\u8bae\u907f\u514d\u5728\u5b89\u5168\u6027\u8981\u6c42\u8f83\u9ad8\u7684\u573a\u5408\u4f7f\u7528MD5\u3002\u5bf9\u4e8eAES\u52a0\u5bc6\uff0c\u786e\u4fdd\u5bc6\u94a5\u7684\u5f3a\u5ea6\u548c\u968f\u673a\u6027\u81f3\u5173\u91cd\u8981\uff0c\u540c\u65f6\u8981\u4f7f\u7528\u5b89\u5168\u7684\u6a21\u5f0f\uff08\u5982CBC\u6216GCM\uff09\u4ee5\u9632\u6b62\u6f5c\u5728\u7684\u653b\u51fb\u3002\u6b64\u5916\uff0c\u5bc6\u94a5\u7ba1\u7406\u540c\u6837\u91cd\u8981\uff0c\u5207\u52ff\u786c\u7f16\u7801\u5728\u4ee3\u7801\u4e2d\uff0c\u5e94\u8be5\u4f7f\u7528\u5b89\u5168\u7684\u65b9\u6cd5\u6765\u5b58\u50a8\u548c\u4f20\u9012\u5bc6\u94a5\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u53ef\u4ee5\u4f7f\u7528MD5\u548cAES\u52a0\u5bc6\u6765\u4fdd\u62a4\u6570\u636e\u7684\u5b8c\u6574\u6027\u548c\u5b89\u5168\u6027\u3002\u9996\u5148\uff0cMD5\u662f\u4e00\u79cd\u5e38\u7528\u7684\u54c8\u5e0c\u51fd\u6570\uff0c\u7528\u4e8e\u751f\u6210\u6570 [&hellip;]","protected":false},"author":3,"featured_media":1114527,"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\/1114492"}],"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=1114492"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1114492\/revisions"}],"predecessor-version":[{"id":1114540,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1114492\/revisions\/1114540"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1114527"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1114492"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1114492"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1114492"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}