{"id":5570,"date":"2023-12-02T20:02:00","date_gmt":"2023-12-02T12:02:00","guid":{"rendered":"https:\/\/ixyzero.com\/blog\/?p=5570"},"modified":"2023-11-29T20:41:57","modified_gmt":"2023-11-29T12:41:57","slug":"%e7%94%a8-python-%e8%bf%9b%e8%a1%8c-blowfish-%e5%8a%a0%e8%a7%a3%e5%af%86","status":"publish","type":"post","link":"https:\/\/ixyzero.com\/blog\/archives\/5570.html","title":{"rendered":"\u7528 Python \u8fdb\u884c Blowfish \u52a0\u89e3\u5bc6"},"content":{"rendered":"\n<p>=Start=<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u7f18\u7531\uff1a<\/h4>\n\n\n\n<p>\u7b80\u5355\u6574\u7406\u4e00\u4e0b Blowfish \u5bc6\u7801\u7b97\u6cd5\u7684\u76f8\u5173\u77e5\u8bc6\uff0c\u65b9\u4fbf\u540e\u9762\u6709\u9700\u8981\u7684\u65f6\u5019\u53c2\u8003\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u6b63\u6587\uff1a<\/h4>\n\n\n\n<h5 class=\"wp-block-heading\">\u53c2\u8003\u89e3\u7b54\uff1a<\/h5>\n\n\n\n<h4 class=\"wp-block-heading\">Blowfish \u5bc6\u7801\u7b97\u6cd5\u4ecb\u7ecd<\/h4>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p><strong>Blowfish\u662f\u4e00\u4e2a\u5bf9\u79f0\u5bc6\u94a5\u5206\u7ec4\u5bc6\u7801\u7b97\u6cd5\uff0c\u7531 Bruce Schneier \u4e8e1993\u5e74\u8bbe\u8ba1\u3002<\/strong>Blowfish\u7b97\u6cd5\u7531\u4e8e\u5206\u7ec4\u957f\u5ea6\u592a\u5c0f\u5df2\u88ab\u8ba4\u4e3a\u4e0d\u5b89\u5168\uff0cSchneier\u66f4\u5efa\u8bae\u5728\u73b0\u4ee3\u5e94\u7528\u4e2d\u4f7f\u7528 Twofish \u5bc6\u7801\u3002\u6216\u8005\u4f7f\u7528 AES \u7b97\u6cd5\u8fdb\u884c\u52a0\u89e3\u5bc6\u3002<\/p>\n\n\n\n<p>Schneier\u8bbe\u8ba1\u7684Blowfish\u7b97\u6cd5\u7528\u9014\u5e7f\u6cdb\uff0c\u610f\u5728\u66ff\u4ee3\u8001\u65e7\u7684DES\u53ca\u907f\u514d\u5176\u4ed6\u7b97\u6cd5\u7684\u95ee\u9898\u4e0e\u9650\u5236\u3002Blowfish\u521a\u521a\u7814\u53d1\u51fa\u7684\u65f6\u5019\uff0c\u5927\u90e8\u5206\u5176\u4ed6\u52a0\u5bc6\u7b97\u6cd5\u662f\u4e13\u5229\u6240\u6709\u7684\u6216\u5c5e\u4e8e\u5546\u4e1a(\u653f\u5e9c)\u673a\u5bc6\uff0c\u6240\u4ee5\u53d1\u5c55\u8d77\u6765\u975e\u5e38\u53d7\u9650\u5236\u3002Schneier\u5219\u58f0\u660eBlowfish\u7684\u4f7f\u7528\u6ca1\u6709\u4efb\u4f55\u9650\u5236\uff0c\u4efb\u4f55\u56fd\u5bb6\u4efb\u4f55\u4eba\u4efb\u4f55\u65f6\u5019\u90fd\u53ef\u4ee5\u968f\u610f\u4f7f\u7528Blowfish\u7b97\u6cd5\u3002<\/p>\n<\/blockquote>\n<\/blockquote>\n\n\n\n<h5 class=\"wp-block-heading\">Blowfish\u7684\u586b\u5145\u6a21\u5f0f<\/h5>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p><strong>\u5757\uff08\u5206\u7ec4\uff09\u5bc6\u7801\u53ea\u80fd\u5bf9\u56fa\u5b9a\u957f\u5ea6\u7684\u6570\u636e\u5757\u8fdb\u884c\u5904\u7406<\/strong>\uff0c\u800c\u6d88\u606f\u7684\u957f\u5ea6\u901a\u5e38\u662f\u53ef\u53d8\u7684\uff0c\u56e0\u6b64\u4e0d\u540c\u7684\u5de5\u4f5c\u6a21\u5f0f\u5bf9\u5e94\u7684\u586b\u5145\u6a21\u5f0f\u4e0d\u540c\uff0c\u5f53\u60a8\u9009\u62e9NONE\u4e0d\u586b\u5145\u65f6\uff0c\u5219\u8981\u4fdd\u8bc1\u52a0\u5bc6\u6587\u672c\u957f\u5ea6\u4e3a8\u7684\u500d\u6570\uff0c\u5426\u5219\u51fa\u73b0\u89e3\u5bc6\/\u52a0\u5bc6\u5f02\u5e38\u3002<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u586b\u5145\u533a\u522b\uff1a\u5728ECB\u3001CBC\u5de5\u4f5c\u6a21\u5f0f\u4e0b\u6700\u540e\u4e00\u5757\u8981\u5728\u52a0\u5bc6\u524d\u8fdb\u884c\u586b\u5145\uff0c\u5176\u5b83\u4e0d\u7528\u9009\u62e9\u586b\u5145\u6a21\u5f0f\uff1b<\/li>\n\n\n\n<li>\u586b\u5145\u6a21\u5f0f\uff1aBlowfish\u652f\u6301\u7684\u586b\u5145\u6a21\u5f0f\u4e3aPKCS5\u3001PKCS7\u548cNONE\u3002\u5176\u4e2dPKCS7\u6807\u51c6\u662f\u4e3b\u6d41\u52a0\u5bc6\u7b97\u6cd5\u90fd\u9075\u5faa\u7684\u6570\u636e\u586b\u5145\u7b97\u6cd5\u3002Blowfish\u6807\u51c6\u89c4\u5b9a\u7684\u533a\u5757\u957f\u5ea6\u4e3a\u56fa\u5b9a\u503c64Bit\uff0c\u63a8\u8350PKCS7\u3002<\/li>\n<\/ul>\n<\/blockquote>\n<\/blockquote>\n\n\n\n<h5 class=\"wp-block-heading\">Blowfish\u5bc6\u94a5KEY\u548c\u521d\u59cb\u5316\u5411\u91cfIV<\/h5>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p><strong>\u521d\u59cb\u5316\u5411\u91cfIV\u53ef\u4ee5\u6709\u6548\u63d0\u5347\u5b89\u5168\u6027\uff0c\u4f46\u662f\u5728\u5b9e\u9645\u7684\u4f7f\u7528\u573a\u666f\u4e2d\uff0c\u5b83\u4e0d\u80fd\u50cf\u5bc6\u94a5KEY\u90a3\u6837\u76f4\u63a5\u4fdd\u5b58\u5728\u914d\u7f6e\u6587\u4ef6\u6216\u56fa\u5b9a\u5199\u6b7b\u5728\u4ee3\u7801\u4e2d\uff0c\u4e00\u822c\u6b63\u786e\u7684\u5904\u7406\u65b9\u5f0f\u4e3a\uff1a\u5728\u52a0\u5bc6\u7aef\u5c06IV\u8bbe\u7f6e\u4e3a\u4e00\u4e2a8\u4f4d\u7684\u968f\u673a\u503c\uff0c\u7136\u540e\u548c\u52a0\u5bc6\u6587\u672c\u4e00\u8d77\u8fd4\u7ed9\u89e3\u5bc6\u7aef\u5373\u53ef\u3002<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u533a\u5757\u957f\u5ea6\uff1aBlowfish\u6807\u51c6\u89c4\u5b9a\u533a\u5757\u957f\u5ea6\u53ea\u6709\u4e00\u4e2a\u503c\uff0c\u56fa\u5b9a\u4e3a64Bit\uff0c\u5bf9\u5e94\u7684\u5b57\u8282\u4e3a8\u4f4d\uff1b<\/li>\n\n\n\n<li>\u5bc6\u94a5KEY\uff1a\u5bc6\u94a5\u957f\u5ea6\u533a\u95f4\u4e3a32-448\uff0c\u5bf9\u5e944-56\u4f4d\uff0c\u8be5\u5b57\u6bb5\u4e0d\u80fd\u516c\u5f00\u4f20\u8f93\uff0c\u7528\u4e8e\u52a0\u5bc6\u548c\u89e3\u5bc6\uff1b<\/li>\n\n\n\n<li>\u521d\u59cb\u5316\u5411\u91cfIV\uff1a\u8be5\u5b57\u6bb5\u53ef\u4ee5\u516c\u5f00\uff0c\u7528\u4e8e\u5c06\u52a0\u5bc6\u968f\u673a\u5316\u3002\u540c\u6837\u7684\u660e\u6587\u88ab\u591a\u6b21\u52a0\u5bc6\u4e5f\u4f1a\u4ea7\u751f\u4e0d\u540c\u7684\u5bc6\u6587\uff0c\u907f\u514d\u4e86\u8f83\u6162\u7684\u91cd\u65b0\u4ea7\u751f\u5bc6\u94a5\u7684\u8fc7\u7a0b\uff0c\u521d\u59cb\u5316\u5411\u91cf\u4e0e\u5bc6\u94a5\u76f8\u6bd4\u6709\u4e0d\u540c\u7684\u5b89\u5168\u6027\u9700\u6c42\uff0c\u56e0\u6b64IV\u901a\u5e38\u65e0\u987b\u4fdd\u5bc6\u3002\u7136\u800c\u5728\u5927\u591a\u6570\u60c5\u51b5\u4e2d\uff0c\u4e0d\u5e94\u5f53\u5728\u4f7f\u7528\u540c\u4e00\u5bc6\u94a5\u7684\u60c5\u51b5\u4e0b\u4e24\u6b21\u4f7f\u7528\u540c\u4e00\u4e2aIV\uff0c\u4e00\u822c\u521d\u59cb\u5316\u5411\u91cfIV\u4e3a8\u4f4d\u7684\u968f\u673a\u503c\u3002<\/li>\n<\/ul>\n<\/blockquote>\n<\/blockquote>\n\n\n\n<h5 class=\"wp-block-heading\">Blowfish \u7684\u4e3b\u8981\u7279\u70b9<\/h5>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<ul class=\"wp-block-list\">\n<li>Block Cipher: Blowfish is a block cipher encryption algorithm that operates on 64-bit blocks of plaintext at a time. Blowfish \u662f\u4e00\u79cd\u5206\u5757\u5bc6\u7801\u52a0\u5bc6\u7b97\u6cd5\uff0c\u6bcf\u6b21\u5bf9 64 \u4f4d\u7684\u660e\u6587\u5757\u8fdb\u884c\u64cd\u4f5c\u3002<\/li>\n\n\n\n<li>Symmetric Key Encryption: Blowfish uses a symmetric key encryption system, meaning that the same key is used for both encryption and decryption. Blowfish \u4f7f\u7528\u5bf9\u79f0\u5bc6\u94a5\u52a0\u5bc6\u7cfb\u7edf\uff0c\u8fd9\u610f\u5473\u7740\u52a0\u5bc6\u548c\u89e3\u5bc6\u90fd\u4f7f\u7528\u76f8\u540c\u7684\u5bc6\u94a5\u3002<\/li>\n\n\n\n<li>Variable Key Size: Blowfish allows for variable key sizes of up to 448 bits, making it more secure compared to other encryption algorithms. Blowfish \u5141\u8bb8\u9ad8\u8fbe 448 \u4f4d\u7684\u53ef\u53d8\u5bc6\u94a5\u5927\u5c0f\uff0c\u4e0e\u5176\u4ed6\u52a0\u5bc6\u7b97\u6cd5\u76f8\u6bd4\u66f4\u52a0\u5b89\u5168\u3002<\/li>\n\n\n\n<li>Feistel Cipher: Blowfish uses the Feistel Cipher structure, which splits the plaintext into two halves and then iteratively encrypts each half using a series of mathematical operations. Blowfish \u4f7f\u7528 Feistel \u5bc6\u7801\u7ed3\u6784\uff0c\u5b83\u5c06\u660e\u6587\u5206\u6210\u4e24\u534a\uff0c\u7136\u540e\u901a\u8fc7\u4e00\u7cfb\u5217\u6570\u5b66\u8fd0\u7b97\u5bf9\u6bcf\u4e00\u534a\u8fdb\u884c\u8fed\u4ee3\u52a0\u5bc6\u3002<\/li>\n<\/ul>\n<\/blockquote>\n<\/blockquote>\n\n\n\n<h4 class=\"wp-block-heading\">\u7528 Python \u8fdb\u884c Blowfish \u52a0\u89e3\u5bc6<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>from Crypto.Cipher import Blowfish\nfrom struct import pack\nimport base64\n\ndef blowfish_str(encrypt_or_decrypt, msg, key):\n    if encrypt_or_decrypt == \"encrypt\":\n        bs = Blowfish.block_size\n        key = bytes(key, encoding='utf-8')\n        cipher = Blowfish.new(key, Blowfish.MODE_CBC)\n        plaintext = msg.encode('utf-8')\n        plen = bs - len(plaintext) % bs\n        padding = &#91;plen]*plen\n        padding = pack('b'*plen, *padding)\n        ciphertext = cipher.iv + cipher.encrypt(plaintext + padding) #\u52a0\u5bc6\u8fd4\u56de\u7684\u6570\u636e\u7c7b\u578b\u662f bytes \u7c7b\u578b\n        return ciphertext\n\n    elif encrypt_or_decrypt == \"decrypt\":\n        bs = Blowfish.block_size\n        key = bytes(key, encoding='utf-8')\n        ciphertext = base64.b64decode(msg) #\u548c\u52a0\u5bc6\u4e4b\u540e\u7684\u64cd\u4f5c\u6709\u5173\n        iv = ciphertext&#91;:bs]\n        ciphertext = ciphertext&#91;bs:]\n        cipher = Blowfish.new(key, Blowfish.MODE_CBC, iv)\n        plaintext = cipher.decrypt(ciphertext) #\u89e3\u5bc6\u8fd4\u56de\u7684\u6570\u636e\u662f bytes \u7c7b\u578b\n        last_byte = plaintext&#91;-1]\n        plaintext = plaintext&#91;:- (last_byte if type(last_byte) is int else ord(last_byte))]\n        return plaintext\n\ndef encrypt_decrypt_blowfish():\n    key = 'password_here'\n\n    with open('raw_data.txt') as fp:\n        plaintext = fp.read()\n    ciphertext = blowfish_str('encrypt', plaintext, key)\n    with open('data_blowfish_encrypt.txt','w') as fp:\n        fp.write(base64.b64encode(ciphertext).decode())\n\n    with open('data_blowfish_encrypt.txt') as fp:\n        encrypt_data = fp.read()\n    decrypt_data = blowfish_str('decrypt', encrypt_data, key)\n    # decrypt_data = blowfish_str('decrypt', base64.b64encode(ciphertext), key)\n    with open('data_blowfish_decrypt.txt','w') as fp:\n        fp.write(decrypt_data.decode())\n#encrypt_decrypt_blowfish<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>from Crypto.Cipher import Blowfish\nfrom Crypto import Random\n\ndef encrypt(key, plaintext):\n    iv = Random.new().read(Blowfish.block_size)\n    cipher = Blowfish.new(key, Blowfish.MODE_CBC, iv)\n    padded_plaintext = _pad_string(plaintext)\n    ciphertext = cipher.encrypt(padded_plaintext)\n    return iv + ciphertext\n\ndef decrypt(key, ciphertext):\n    iv = ciphertext&#91;:Blowfish.block_size]\n    cipher = Blowfish.new(key, Blowfish.MODE_CBC, iv)\n    padded_plaintext = cipher.decrypt(ciphertext&#91;Blowfish.block_size:])\n    plaintext = _unpad_string(padded_plaintext)\n    return plaintext\n\ndef _pad_string(s):\n    padding_size = Blowfish.block_size - len(s) % Blowfish.block_size\n    padding = chr(padding_size) * padding_size\n    return s + padding\n\ndef _unpad_string(s):\n    padding_size = ord(s&#91;-1])\n    return s&#91;:-padding_size]\n\nkey = b'secret_key'\nplaintext = 'This is a secret message.'\nencrypted_data = encrypt(key, plaintext)\nprint('Encrypted data:', encrypted_data.hex())\ndecrypted_data = decrypt(key, encrypted_data)\nprint('Decrypted data:', decrypted_data)<\/code><\/pre>\n\n\n\n<h5 class=\"wp-block-heading\">\u53c2\u8003\u94fe\u63a5\uff1a<\/h5>\n\n\n\n<p>Blowfish (cipher)<br><a href=\"https:\/\/en.wikipedia.org\/wiki\/Blowfish_(cipher)\">https:\/\/en.wikipedia.org\/wiki\/Blowfish_(cipher)<\/a><\/p>\n\n\n\n<p>Understanding Blowfish Encryption Algorithm | 2023 #\u6709\u4ee3\u7801\uff0cnice<br><a href=\"https:\/\/cyberw1ng.medium.com\/understanding-blowfish-encryption-algorithm-2023-24eb8f69f85b\">https:\/\/cyberw1ng.medium.com\/understanding-blowfish-encryption-algorithm-2023-24eb8f69f85b<\/a><\/p>\n\n\n\n<p>How to decrypt using Blowfish in Pycrypto?<br><a href=\"https:\/\/stackoverflow.com\/questions\/35042164\/how-to-decrypt-using-blowfish-in-pycrypto\">https:\/\/stackoverflow.com\/questions\/35042164\/how-to-decrypt-using-blowfish-in-pycrypto<\/a><\/p>\n\n\n\n<p>Blowfish<br><a href=\"https:\/\/pycryptodome.readthedocs.io\/en\/latest\/src\/cipher\/blowfish.html\">https:\/\/pycryptodome.readthedocs.io\/en\/latest\/src\/cipher\/blowfish.html<\/a><br><a href=\"https:\/\/pycryptodome.readthedocs.io\/en\/latest\/src\/cipher\/classic.html#cbc-mode\">https:\/\/pycryptodome.readthedocs.io\/en\/latest\/src\/cipher\/classic.html#cbc-mode<\/a><\/p>\n\n\n\n<p>Blowfish \u52a0\u5bc6\u7b97\u6cd5<br><a href=\"https:\/\/leeyuxun.github.io\/Blowfish%E5%8A%A0%E5%AF%86%E7%AE%97%E6%B3%95.html\">https:\/\/leeyuxun.github.io\/Blowfish%E5%8A%A0%E5%AF%86%E7%AE%97%E6%B3%95.html<\/a><\/p>\n\n\n\n<p>Blowfish\u52a0\u5bc6\u7b97\u6cd5<br><a href=\"https:\/\/try8.cn\/tool\/cipher\/blowfish\">https:\/\/try8.cn\/tool\/cipher\/blowfish<\/a><\/p>\n\n\n\n<p>A simple Python implementation for Blowfish Encryption Algorithm<br><a href=\"https:\/\/github.com\/ananya2407\/Blowfish-Algorithm\">https:\/\/github.com\/ananya2407\/Blowfish-Algorithm<\/a><\/p>\n\n\n\n<p>=END=<\/p>\n","protected":false},"excerpt":{"rendered":"<p>=Start= \u7f18\u7531\uff1a \u7b80\u5355\u6574\u7406\u4e00\u4e0b Blowfish \u5bc6\u7801\u7b97\u6cd5\u7684\u76f8\u5173\u77e5\u8bc6\uff0c\u65b9\u4fbf\u540e\u9762\u6709\u9700\u8981\u7684\u65f6\u5019\u53c2\u8003\u3002 \u6b63\u6587\uff1a [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[23,7,25],"tags":[2076,1204,2073,8,2068,2077,2075],"class_list":["post-5570","post","type-post","status-publish","format-standard","hentry","category-knowledgebase-2","category-programing","category-security","tag-blowfish","tag-cipher","tag-encryption","tag-python","tag-snippets","tag-2077","tag-2075"],"views":1829,"_links":{"self":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/5570","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/comments?post=5570"}],"version-history":[{"count":1,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/5570\/revisions"}],"predecessor-version":[{"id":5571,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/5570\/revisions\/5571"}],"wp:attachment":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/media?parent=5570"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/categories?post=5570"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/tags?post=5570"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}