{"id":5572,"date":"2023-12-03T09:42:00","date_gmt":"2023-12-03T01:42:00","guid":{"rendered":"https:\/\/ixyzero.com\/blog\/?p=5572"},"modified":"2023-11-29T20:43:47","modified_gmt":"2023-11-29T12:43:47","slug":"%e7%94%a8-python-%e8%bf%9b%e8%a1%8c-rc4-%e5%8a%a0%e8%a7%a3%e5%af%86","status":"publish","type":"post","link":"https:\/\/ixyzero.com\/blog\/archives\/5572.html","title":{"rendered":"\u7528 Python \u8fdb\u884c RC4 \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 RC4 \u52a0\u89e3\u5bc6\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<pre class=\"wp-block-code\"><code>pip install pycryptodome<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">RC4 \u5bc6\u7801\u7684\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>\u5728\u5bc6\u7801\u5b66\u4e2d\uff0cRC4\uff08\u4e5f\u88ab\u79f0\u4e3aARC4\u6216ARCFOUR\uff09\u662f\u4e00\u79cd\u6d41\u5f0f\u5bc6\u7801\u7b97\u6cd5\uff0c\u5bc6\u94a5\u957f\u5ea6\u53ef\u53d8\u3002\u5b83\u52a0\u89e3\u5bc6\u4f7f\u7528\u76f8\u540c\u7684\u5bc6\u94a5\uff0c\u56e0\u6b64\u4e5f\u5c5e\u4e8e\u5bf9\u79f0\u52a0\u5bc6\u7b97\u6cd5\u3002<\/strong>RC4\u662f\u6709\u7ebf\u7b49\u6548\u52a0\u5bc6\uff08WEP\uff09\u4e2d\u91c7\u7528\u7684\u52a0\u5bc6\u7b97\u6cd5\uff0c\u4e5f\u66fe\u7ecf\u662fTLS\u6240\u91c7\u7528\u7684\u7b97\u6cd5\u4e4b\u4e00\u3002<\/p>\n\n\n\n<p>RC4\u662f\u7531\u7f8e\u56fd\u5bc6\u7801\u5b66\u5bb6\u7f57\u7eb3\u5fb7\u00b7\u674e\u7ef4\u65af\u7279(Ronald Rivest)\u57281987\u5e74\u8bbe\u8ba1\u7684\u3002<\/p>\n\n\n\n<p>RC4\u5f00\u59cb\u65f6\u662f\u5546\u4e1a\u673a\u5bc6\uff0c\u6ca1\u6709\u516c\u5f00\u53d1\u8868\u51fa\u6765\uff0c\u4f46\u662f\u57281994\u5e749\u6708\u4efd\u7684\u65f6\u5019\uff0c\u5b83\u88ab\u4eba\u533f\u540d\u516c\u5f00\u5728\u4e86 Cypherpunks \u90ae\u4ef6\u5217\u8868\u4e0a\uff0c\u5f88\u5feb\u5b83\u5c31\u88ab\u53d1\u5230\u4e86 sci.crypt \u65b0\u95fb\u7ec4\u4e0a\uff0c\u968f\u540e\u4ece\u8fd9\u4f20\u64ad\u5230\u4e86\u4e92\u8054\u7f51\u7684\u8bb8\u591a\u7ad9\u70b9\u3002\u968f\u4e4b\u8d34\u51fa\u7684\u4ee3\u7801\u540e\u6765\u88ab\u8bc1\u660e\u662f\u771f\u5b9e\u7684\uff0c\u56e0\u4e3a\u5b83\u7684\u8f93\u51fa\u8ddf\u53d6\u5f97\u4e86RC4\u7248\u6743\u7684\u79c1\u6709\u8f6f\u4ef6\u7684\u8f93\u51fa\u662f\u5b8c\u5168\u76f8\u540c\u7684\u3002\u7531\u4e8e\u7b97\u6cd5\u5df2\u7ecf\u516c\u5f00\uff0cRC4\u4e5f\u5c31\u4e0d\u518d\u662f\u5546\u4e1a\u79d8\u5bc6\u4e86\uff0c\u53ea\u662f\u5b83\u7684\u540d\u5b57\u201cRC4\u201d\u4ecd\u7136\u662f\u4e00\u4e2a\u6ce8\u518c\u5546\u6807\u3002RC4\u7ecf\u5e38\u88ab\u79f0\u4f5c\u662f\u201cARCFOUR\u201d\u6216\u8005&#8221;ARC4&#8243;\uff08\u610f\u601d\u662f\u79f0\u4e3aRC4\uff09\uff0c\u8fd9\u6837\u6765\u907f\u514d\u5546\u6807\u4f7f\u7528\u7684\u95ee\u9898\u3002RSA Security\u4ece\u6765\u6ca1\u6709\u6b63\u5f0f\u516c\u5e03\u8fd9\u4e2a\u7b97\u6cd5\uff0c\u7f57\u7eb3\u5fb7\u00b7\u674e\u7ef4\u65af\u7279\u57282008\u5e74\u7684\u81ea\u5df1\u7684\u8bfe\u7a0b\u7b14\u8bb0\u4e2d\u7ed9\u51fa\u4e86\u4e00\u4e2a\u6307\u5411RC4\u7684\u82f1\u6587\u7ef4\u57fa\u767e\u79d1\u6587\u7ae0\u7684\u94fe\u63a5\uff0c\u5e76\u4e14\u57282014\u5e74\u7684\u6587\u4ef6\u4e2d\u786e\u8ba4\u4e86RC4\u53ca\u5176\u4ee3\u7801\u7684\u5386\u53f2\u3002<\/p>\n\n\n\n<p>RC4\u5df2\u7ecf\u6210\u4e3a\u4e00\u4e9b\u534f\u8bae\u548c\u6807\u51c6\u7684\u4e00\u90e8\u5206\uff0c\u59821997\u5e74\u7684WEP\u548c2003\u5e74\u7684WPA\uff1b\u548c1995\u5e74\u7684SSL\uff0c\u4ee5\u53ca1999\u5e74\u7684TLS\u30022015\u5e742\u6708\u7531 RFC 7465 \u89c4\u5b9a\u7981\u6b62RC4\u5728\u6240\u6709\u7248\u672c\u7684TLS\u4e2d\u4f7f\u7528\u3002<\/p>\n\n\n\n<p>RC4\u7531\u4f2a\u968f\u673a\u6570\u751f\u6210\u5668\u548c\u5f02\u6216\u8fd0\u7b97\u7ec4\u6210\u3002RC4\u7684\u5bc6\u94a5\u957f\u5ea6\u53ef\u53d8\uff0c\u8303\u56f4\u662f[1,255]\u3002RC4\u4e00\u4e2a\u5b57\u8282\u4e00\u4e2a\u5b57\u8282\u5730\u52a0\u89e3\u5bc6\u3002\u7ed9\u5b9a\u4e00\u4e2a\u5bc6\u94a5\uff0c\u4f2a\u968f\u673a\u6570\u751f\u6210\u5668\u63a5\u53d7\u5bc6\u94a5\u5e76\u4ea7\u751f\u4e00\u4e2aS\u76d2\u3002S\u76d2\u7528\u6765\u52a0\u5bc6\u6570\u636e\uff0c\u800c\u4e14\u5728\u52a0\u5bc6\u8fc7\u7a0b\u4e2dS\u76d2\u4f1a\u53d8\u5316\u3002<\/p>\n\n\n\n<p>\u7531\u4e8e\u5f02\u6216\u8fd0\u7b97\u7684\u5bf9\u5408\u6027\uff0cRC4\u52a0\u5bc6\u89e3\u5bc6\u4f7f\u7528\u540c\u4e00\u5957\u7b97\u6cd5\u3002<\/p>\n\n\n\n<p>ARC4\uff08Alleged RC4\uff09\u662f RC4\uff08Rivest&#8217;s Cipher version 4\uff09\u7684\u5b9e\u73b0\u3002<\/p>\n\n\n\n<p>ARC4 \u5bc6\u94a5\u7684\u957f\u5ea6\u4ece 8 \u4f4d\u5230 2048 \u4f4d\u4e0d\u7b49\u3002<\/p>\n<\/blockquote>\n<\/blockquote>\n\n\n\n<h4 class=\"wp-block-heading\">\u7528 Python \u8fdb\u884c RC4 \u52a0\u89e3\u5bc6\u7684\u6837\u4f8b\u4ee3\u7801<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>import base64\nfrom Crypto.Cipher import ARC4 as rc4cipher\n\ndef rc4_str(encrypt_or_decrypt, msg, key):\n    if encrypt_or_decrypt == \"encrypt\":\n        key = bytes(key, encoding='utf-8')\n        enc = rc4cipher.new(key)\n        res = enc.encrypt(msg.encode('utf-8')) # encrypt\u8f93\u5165\u9700\u8981\u662f bytes \u7c7b\u578b\n        print('type({0}) of rc4 {1} return'.format(type(res), encrypt_or_decrypt))\n        res = base64.b64encode(res) #base64\u7f16\u7801\uff0c\u5c06\u5185\u5bb9\u7f16\u7801\u4e3a\u53ef\u89c1\u5b57\u7b26\n        res = str(res,'utf8')\n        return res\n    elif encrypt_or_decrypt == \"decrypt\":\n        msg = base64.b64decode(msg) #base64\u89e3\u7801\uff0c\u8fd8\u539f\u5185\u5bb9\u4ee5\u8fdb\u884c\u89e3\u5bc6\n        key = bytes(key, encoding='utf-8')\n        enc = rc4cipher.new(key)\n        res = enc.decrypt(msg) #decrypt\u8f93\u51fa\u4e5f\u662f bytes \u7c7b\u578b\n        print('type({0}) of rc4 {1} return'.format(type(res), encrypt_or_decrypt))\n        res = str(res,'utf8')\n        return res\n\ndef encrypt_decrypt_rc4():\n    msg = '\u6d4b\u8bd5string'\n    key = 'password_here'\n    enc1 = rc4_str('encrypt', msg, key)\n    print(enc1)\n    dec1 = rc4_str('decrypt', enc1, key)\n    print(dec1)\n#encrypt_decrypt_rc4<\/code><\/pre>\n\n\n\n<h5 class=\"wp-block-heading\">\u53c2\u8003\u94fe\u63a5\uff1a<\/h5>\n\n\n\n<p>RC4<br><a href=\"https:\/\/en.wikipedia.org\/wiki\/RC4\">https:\/\/en.wikipedia.org\/wiki\/RC4<\/a><\/p>\n\n\n\n<p>How to decrypt a file that encrypted with rc4 using Python?<br><a href=\"https:\/\/stackoverflow.com\/questions\/29607753\/how-to-decrypt-a-file-that-encrypted-with-rc4-using-python\">https:\/\/stackoverflow.com\/questions\/29607753\/how-to-decrypt-a-file-that-encrypted-with-rc4-using-python<\/a><\/p>\n\n\n\n<p>ARC4<br><a href=\"https:\/\/pycryptodome.readthedocs.io\/en\/latest\/src\/cipher\/arc4.html\">https:\/\/pycryptodome.readthedocs.io\/en\/latest\/src\/cipher\/arc4.html<\/a><\/p>\n\n\n\n<p>python\u5b9e\u73b0rc4\u52a0\u5bc6\u89e3\u5bc6,base64\u8f93\u51fa<br><a href=\"https:\/\/cloud.tencent.com\/developer\/article\/1685707\">https:\/\/cloud.tencent.com\/developer\/article\/1685707<\/a><\/p>\n\n\n\n<p>RC4 doesn&#8217;t work correctly with openssl command?<br><a href=\"https:\/\/stackoverflow.com\/questions\/9329631\/rc4-doesnt-work-correctly-with-openssl-command\">https:\/\/stackoverflow.com\/questions\/9329631\/rc4-doesnt-work-correctly-with-openssl-command<\/a><\/p>\n\n\n\n<p>RC4 encryption java<br><a href=\"https:\/\/stackoverflow.com\/questions\/12289717\/rc4-encryption-java\">https:\/\/stackoverflow.com\/questions\/12289717\/rc4-encryption-java<\/a><\/p>\n\n\n\n<p>PyCryptodome install<br><a href=\"https:\/\/pycryptodome.readthedocs.io\/en\/latest\/src\/installation.html\">https:\/\/pycryptodome.readthedocs.io\/en\/latest\/src\/installation.html<\/a><\/p>\n\n\n\n<p>A self-contained cryptographic library for Python<br><a href=\"https:\/\/github.com\/Legrandin\/pycryptodome\">https:\/\/github.com\/Legrandin\/pycryptodome<\/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 RC4 \u52a0\u89e3\u5bc6\u7b97\u6cd5\u7684\u76f8\u5173\u77e5\u8bc6\uff0c\u65b9\u4fbf\u540e\u9762\u6709\u9700\u8981\u7684\u65f6\u5019\u53c2\u8003\u3002 \u6b63\u6587\uff1a \u53c2\u8003\u89e3 [&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":[1204,2073,8,2078,2068,2077,2075,969],"class_list":["post-5572","post","type-post","status-publish","format-standard","hentry","category-knowledgebase-2","category-programing","category-security","tag-cipher","tag-encryption","tag-python","tag-rc4","tag-snippets","tag-2077","tag-2075","tag-969"],"views":2386,"_links":{"self":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/5572","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=5572"}],"version-history":[{"count":1,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/5572\/revisions"}],"predecessor-version":[{"id":5573,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/5572\/revisions\/5573"}],"wp:attachment":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/media?parent=5572"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/categories?post=5572"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/tags?post=5572"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}