{"id":5568,"date":"2023-12-01T20:03:00","date_gmt":"2023-12-01T12:03:00","guid":{"rendered":"https:\/\/ixyzero.com\/blog\/?p=5568"},"modified":"2023-11-29T20:39:15","modified_gmt":"2023-11-29T12:39:15","slug":"%e7%94%a8python%e8%bf%9b%e8%a1%8cxor%e5%8a%a0%e8%a7%a3%e5%af%86","status":"publish","type":"post","link":"https:\/\/ixyzero.com\/blog\/archives\/5568.html","title":{"rendered":"\u7528Python\u8fdb\u884cXOR\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\u8bb0\u5f55\u4e00\u4e0b XOR \u64cd\u4f5c\uff08\u5305\u62ec\u52a0\u89e3\u5bc6\uff09\u548c\u7528Python\u8fdb\u884cXOR\u52a0\u89e3\u5bc6\u7684\u65b9\u6cd5\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\">\u5f02\u6216\u8fd0\u7b97\u7684\u542b\u4e49\u548c\u8fd0\u7b97\u5b9a\u5f8b<\/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>XOR \u4e3b\u8981\u7528\u6765\u5224\u65ad\u4e24\u4e2a\u503c\u662f\u5426\u4e0d\u540c\u3002<\/strong><\/p>\n\n\n\n<p>XOR \u4e00\u822c\u4f7f\u7528\u63d2\u5165\u7b26\u53f7\uff08caret\uff09^\u8868\u793a\u3002\u5982\u679c\u7ea6\u5b9a0 \u4e3a false\uff0c1 \u4e3a true\uff0c\u90a3\u4e48 XOR \u7684\u8fd0\u7b97\u771f\u503c\u8868\u5982\u4e0b\u3002<\/p>\n\n\n\n<p>0 ^ 0 = 0<br>0 ^ 1 = 1<br>1 ^ 0 = 1<br>1 ^ 1 = 0<\/p>\n\n\n\n<p>XOR \u8fd0\u7b97\u6709\u4ee5\u4e0b\u7684\u8fd0\u7b97\u5b9a\u5f8b\u3002<\/p>\n\n\n\n<p>\uff081\uff09\u4e00\u4e2a\u503c\u4e0e\u81ea\u8eab\u7684\u8fd0\u7b97\uff0c\u603b\u662f\u4e3a false\u3002<\/p>\n\n\n\n<p>x ^ x = 0<\/p>\n\n\n\n<p>\uff082\uff09\u4e00\u4e2a\u503c\u4e0e 0 \u7684\u8fd0\u7b97\uff0c\u603b\u662f\u7b49\u4e8e\u5176\u672c\u8eab\u3002<\/p>\n\n\n\n<p>x ^ 0 = x<\/p>\n\n\n\n<p>\uff083\uff09\u53ef\u4ea4\u6362\u6027<\/p>\n\n\n\n<p>x ^ y = y ^ x<\/p>\n\n\n\n<p>\uff084\uff09\u7ed3\u5408\u6027<\/p>\n\n\n\n<p>x ^ (y ^ z) = (x ^ y) ^ z<\/p>\n\n\n\n<p>\u6839\u636e\u4e0a\u9762\u7684\u8fd9\u4e9b\u8fd0\u7b97\u5b9a\u5f8b\uff0c\u53ef\u4ee5\u5f97\u5230\u5f02\u6216\u8fd0\u7b97\u7684\u5f88\u591a\u91cd\u8981\u5e94\u7528\u3002<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>\u7b80\u5316\u8ba1\u7b97<\/li>\n<\/ol>\n\n\n\n<p>\u591a\u4e2a\u503c\u7684\u5f02\u6216\u8fd0\u7b97\uff0c\u53ef\u4ee5\u6839\u636e\u8fd0\u7b97\u5b9a\u5f8b\u8fdb\u884c\u7b80\u5316\u3002<\/p>\n\n\n\n<p>a ^ b ^ c ^ a ^ b<br>= a ^ a ^ b ^ b ^ c<br>= 0 ^ 0 ^ c<br>= c<\/p>\n\n\n\n<ol class=\"wp-block-list\" start=\"2\">\n<li>\u4ea4\u6362\u503c<\/li>\n<\/ol>\n\n\n\n<p>\u4e24\u4e2a\u53d8\u91cf\u8fde\u7eed\u8fdb\u884c\u4e09\u6b21\u5f02\u6216\u8fd0\u7b97\uff0c\u53ef\u4ee5\u4e92\u76f8\u4ea4\u6362\u503c\u3002<\/p>\n\n\n\n<p>\u5047\u8bbe\u4e24\u4e2a\u53d8\u91cf\u662fx\u548cy\uff0c\u5404\u81ea\u7684\u503c\u662fa\u548cb\u3002\u4e0b\u9762\u5c31\u662fx\u548cy\u8fdb\u884c\u4e09\u6b21\u5f02\u6216\u8fd0\u7b97\uff0c\u6ce8\u91ca\u90e8\u5206\u662f\u6bcf\u6b21\u8fd0\u7b97\u540e\u4e24\u4e2a\u53d8\u91cf\u7684\u503c\u3002<\/p>\n\n\n\n<p>x = x ^ y \/\/ (a ^ b, b)<br>y = x ^ y \/\/ (a ^ b, a ^ b ^ b) =&gt; (a ^ b, a)<br>x = x ^ y \/\/ (a ^ b ^ a, a) =&gt; (b, a)<\/p>\n\n\n\n<p>\u8fd9\u662f\u4e24\u4e2a\u53d8\u91cf\u4ea4\u6362\u503c\u7684\u6700\u5feb\u65b9\u6cd5\uff0c\u4e0d\u9700\u8981\u4efb\u4f55\u989d\u5916\u7684\u7a7a\u95f4\u3002<\/p>\n\n\n\n<ol class=\"wp-block-list\" start=\"3\">\n<li>\u52a0\u5bc6<\/li>\n<\/ol>\n\n\n\n<p>\u5f02\u6216\u8fd0\u7b97\u53ef\u4ee5\u7528\u4e8e\u52a0\u5bc6\u3002<\/p>\n\n\n\n<p>\u7b2c\u4e00\u6b65\uff0c\u660e\u6587\uff08text\uff09\u4e0e\u5bc6\u94a5\uff08key\uff09\u8fdb\u884c\u5f02\u6216\u8fd0\u7b97\uff0c\u53ef\u4ee5\u5f97\u5230\u5bc6\u6587\uff08cipherText\uff09\u3002<\/p>\n\n\n\n<p>text ^ key = cipherText<\/p>\n\n\n\n<p>\u7b2c\u4e8c\u6b65\uff0c\u5bc6\u6587\u4e0e\u5bc6\u94a5\u518d\u6b21\u8fdb\u884c\u5f02\u6216\u8fd0\u7b97\uff0c\u5c31\u53ef\u4ee5\u8fd8\u539f\u6210\u660e\u6587\u3002<\/p>\n\n\n\n<p>cipherText ^ key = text<\/p>\n\n\n\n<p>\u539f\u7406\u5f88\u7b80\u5355\uff0c\u5982\u679c\u660e\u6587\u662f x\uff0c\u5bc6\u94a5\u662f y\uff0c\u90a3\u4e48 x \u8fde\u7eed\u4e0e y \u8fdb\u884c\u4e24\u6b21\u5f02\u6216\u8fd0\u7b97\uff0c\u5f97\u5230\u81ea\u8eab\u3002<\/p>\n\n\n\n<p>(x ^ y) ^ y<br>= x ^ (y ^ y)<br>= x ^ 0<br>= x<\/p>\n\n\n\n<ol class=\"wp-block-list\" start=\"4\">\n<li>\u6570\u636e\u5907\u4efd<\/li>\n<\/ol>\n\n\n\n<p>\u5f02\u6216\u8fd0\u7b97\u53ef\u4ee5\u7528\u4e8e\u6570\u636e\u5907\u4efd\u3002<\/p>\n\n\n\n<p>\u6587\u4ef6 x \u548c\u6587\u4ef6 y \u8fdb\u884c\u5f02\u6216\u8fd0\u7b97\uff0c\u4ea7\u751f\u4e00\u4e2a\u5907\u4efd\u6587\u4ef6 z\u3002<\/p>\n\n\n\n<p>x ^ y = z<\/p>\n\n\n\n<p>\u4ee5\u540e\uff0c\u65e0\u8bba\u662f\u6587\u4ef6 x \u6216\u6587\u4ef6 y \u635f\u574f\uff0c\u53ea\u8981\u4e0d\u662f\u4e24\u4e2a\u539f\u59cb\u6587\u4ef6\u540c\u65f6\u635f\u574f\uff0c\u5c31\u80fd\u6839\u636e\u53e6\u4e00\u4e2a\u6587\u4ef6\u548c\u5907\u4efd\u6587\u4ef6\uff0c\u8fdb\u884c\u8fd8\u539f\u3002<\/p>\n\n\n\n<p>x ^ z<br>= x ^ (x ^ y)<br>= (x ^ x) ^ y<br>= 0 ^ y<br>= y<\/p>\n\n\n\n<p>\u4e0a\u9762\u7684\u4f8b\u5b50\u662f y \u635f\u574f\uff0cx \u548c z \u8fdb\u884c\u5f02\u6216\u8fd0\u7b97\uff0c\u5c31\u80fd\u5f97\u5230 y\u3002<\/p>\n<\/blockquote>\n<\/blockquote>\n\n\n\n<h4 class=\"wp-block-heading\">\u7528Python 3\u8fdb\u884c\u5f02\u6216\u8fd0\u7b97<\/h4>\n\n\n\n<p>\u5728Python 3\u4e2d\u5bf9\u4e00\u6bb5 message \u5b57\u7b26\u4e32\u7528\u6307\u5b9a\u7684 key \u5b57\u7b26\u4e32\u8fdb\u884c XOR \u52a0\u5bc6<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#!\/usr\/bin\/env python3\n\nfrom itertools import cycle\n\ncryptedMessage = ''.join(chr(ord(c)^ord(k)) for c,k in zip(message, cycle(key)))<\/code><\/pre>\n\n\n\n<p>\u7b80\u5355\u5c01\u88c5\u7684\u4e00\u6bb5\u4ee3\u7801<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#!\/usr\/bin\/env python3\n# coding=utf-8\n\nfrom itertools import cycle\n\ndef xor_str(msg, key):\n    return ''.join(chr( ord(c)^ord(k) ) for c,k in zip(msg, cycle(key)))\n\ndef xor_file(input_file_path, output_file_path, key):\n    input_data = ''\n    with open(input_file_path) as fp:\n        input_data = fp.read()\n        print('file:{0}, char \\\\n count = {1}'.format(input_file_path, input_data.count('\\n')))\n\n    output_data = xor_str(input_data, key)\n    # \u4e0b\u9762\u4ee5\u9ed8\u8ba4\u7684\u5b57\u7b26\u4e32\u5f62\u5f0f\u5199\u5165\u6587\u4ef6\u65f6\uff0cXOR\u52a0\u5bc6\u4e4b\u540e\u7684\u6587\u4ef6\u5185\u5bb9\u5927\u6982\u7387\u662f\u4e71\u7801\u7684\u5f62\u5f0f\uff0c\u53ef\u4ee5\u8003\u8651\u5728\u5199\u5165\u4e4b\u524d\u5148 base64 \u7f16\u7801\u5904\u7406\u4e00\u4e0b\n    # \u7136\u540e\u4e0a\u9762\u8bfb\u53d6\u7684\u65f6\u5019\u518d base64 \u89e3\u7801\u5904\u7406\u5c31\u597d\n    with open(output_file_path, 'w') as fp:\n        fp.write(output_data)\n\ndef encrypt_decrypt_xor():\n    raw_file_path = 'raw_data.txt'\n    key_str = 'password_here'\n\n    xor_encrypt_file_path = 'data_xor_encrypt.txt'\n    xor_decrypt_file_path = 'data_xor_decrypt.txt'\n\n    # \u5bf9\u4e8e XOR \u7b97\u6cd5\u6765\u8bf4\uff0c\u52a0\u5bc6\u548c\u89e3\u5bc6\u662f\u76f8\u540c\u7684\u64cd\u4f5c\uff0c\u5bc6\u94a5\u4e5f\u76f8\u540c\n    xor_file(raw_file_path, xor_encrypt_file_path, key_str) #\u5bf9\u539f\u59cb\u6587\u4ef6\u7528\u5bc6\u94a5\u8fdb\u884cXOR\u52a0\u5bc6\u751f\u6210\u52a0\u5bc6\u6587\u4ef6\n    xor_file(xor_encrypt_file_path, xor_decrypt_file_path, key_str) #\u5bf9\u52a0\u5bc6\u6587\u4ef6\u7528\u5bc6\u94a5\u8fdb\u884cXOR\u89e3\u5bc6\u751f\u6210\u89e3\u5bc6\u6587\u4ef6\n\n    with open(raw_file_path) as fp1, open(xor_decrypt_file_path) as fp2:\n        raw_data = fp1.read()\n        decrypt_data = fp2.read()\n        print('xor decrypt data compare raw data: {0}'.format('equal' if raw_data == decrypt_data else 'not equal')) #\u5bf9\u6bd4\u4e00\u4e0b\u539f\u59cb\u6570\u636e\u548c\u89e3\u5bc6\u4e4b\u540e\u6570\u636e\u5185\u5bb9\u662f\u5426\u76f8\u7b49\n#encrypt_decrypt_xor<\/code><\/pre>\n\n\n\n<h5 class=\"wp-block-heading\">\u53c2\u8003\u94fe\u63a5\uff1a<\/h5>\n\n\n\n<p>\u5f02\u6216\u8fd0\u7b97 XOR \u6559\u7a0b<br><a href=\"https:\/\/mp.weixin.qq.com\/s\/dVOyUMaJRRIsND0yz4Llvg\">https:\/\/mp.weixin.qq.com\/s\/dVOyUMaJRRIsND0yz4Llvg<\/a><br><a href=\"https:\/\/www.ruanyifeng.com\/blog\/2021\/01\/_xor.html\">https:\/\/www.ruanyifeng.com\/blog\/2021\/01\/_xor.html<\/a><\/p>\n\n\n\n<p>\u5982\u4f55\u7406\u89e3\u300c\u5f02\u6216\uff08XOR\uff09\u300d\u8fd0\u7b97\u5728\u8ba1\u7b97\u673a\u79d1\u5b66\u4e2d\u7684\u91cd\u8981\u6027\uff1f<br><a href=\"https:\/\/www.zhihu.com\/question\/20484426\">https:\/\/www.zhihu.com\/question\/20484426<\/a><\/p>\n\n\n\n<p>\u5982\u4f55\u7406\u89e3\u300c\u5f02\u6216\u300d\u7684\u542b\u4e49\uff1f<br><a href=\"https:\/\/www.zhihu.com\/question\/31116687\">https:\/\/www.zhihu.com\/question\/31116687<\/a><\/p>\n\n\n\n<p>XOR \u52a0\u5bc6\u7b80\u4ecb<br><a href=\"https:\/\/www.ruanyifeng.com\/blog\/2017\/05\/xor.html\">https:\/\/www.ruanyifeng.com\/blog\/2017\/05\/xor.html<\/a><\/p>\n\n\n\n<p>XOR Python Text Encryption\/Decryption<br><a href=\"https:\/\/stackoverflow.com\/questions\/20557999\/xor-python-text-encryption-decryption\">https:\/\/stackoverflow.com\/questions\/20557999\/xor-python-text-encryption-decryption<\/a><br><a href=\"https:\/\/stackoverflow.com\/questions\/20557999\/xor-python-text-encryption-decryption\/65436166#65436166\">https:\/\/stackoverflow.com\/questions\/20557999\/xor-python-text-encryption-decryption\/65436166#65436166<\/a><\/p>\n\n\n\n<p>XOR cipher<br><a href=\"https:\/\/en.wikipedia.org\/wiki\/XOR_cipher\">https:\/\/en.wikipedia.org\/wiki\/XOR_cipher<\/a><\/p>\n\n\n\n<p>Simple python XOR encrypt\/decrypt<br><a href=\"https:\/\/gist.github.com\/nawie\/028c85e29503a5e2d8de106ebd972017\">https:\/\/gist.github.com\/nawie\/028c85e29503a5e2d8de106ebd972017<\/a><\/p>\n\n\n\n<p>=END=<\/p>\n","protected":false},"excerpt":{"rendered":"<p>=Start= \u7f18\u7531\uff1a \u7b80\u5355\u8bb0\u5f55\u4e00\u4e0b XOR \u64cd\u4f5c\uff08\u5305\u62ec\u52a0\u89e3\u5bc6\uff09\u548c\u7528Python\u8fdb\u884cXOR\u52a0\u89e3\u5bc6\u7684\u65b9\u6cd5\uff0c\u65b9\u4fbf\u540e [&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":[2074,2071,2073,618,8,2068,2072],"class_list":["post-5568","post","type-post","status-publish","format-standard","hentry","category-knowledgebase-2","category-programing","category-security","tag-cycle","tag-decryption","tag-encryption","tag-itertools","tag-python","tag-snippets","tag-xor"],"views":1227,"_links":{"self":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/5568","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=5568"}],"version-history":[{"count":1,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/5568\/revisions"}],"predecessor-version":[{"id":5569,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/5568\/revisions\/5569"}],"wp:attachment":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/media?parent=5568"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/categories?post=5568"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/tags?post=5568"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}