{"id":186,"date":"2026-04-07T20:23:03","date_gmt":"2026-04-07T12:23:03","guid":{"rendered":"https:\/\/www.sherlock666.cn\/?p=186"},"modified":"2026-04-07T20:23:03","modified_gmt":"2026-04-07T12:23:03","slug":"rc%e5%ae%b6%e6%97%8f%e5%8a%a0%e5%af%86%e7%ae%97%e6%b3%95%e8%af%a6%e8%a7%a3","status":"publish","type":"post","link":"https:\/\/www.sherlock666.cn\/index.php\/2026\/04\/07\/rc%e5%ae%b6%e6%97%8f%e5%8a%a0%e5%af%86%e7%ae%97%e6%b3%95%e8%af%a6%e8%a7%a3\/","title":{"rendered":"RC\u5bb6\u65cf\u52a0\u5bc6\u7b97\u6cd5\u8be6\u89e3"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">\u4e00\u3001\u5f15\u8a00<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">       \u8fd1\u671f\u4e5f\u662f\u5bf9\u52a0\u5bc6\u7b97\u6cd5\u4e5f\u662f\u505a\u4e86\u4e00\u5957\u8be6\u5c3d\u7684\u5b66\u4e60\uff0c\u4e3b\u8981\u662f\u524d\u671f\u5b66\u4e60\u8fc7\u7a0b\u4e2d\u4ec5\u8bb0\u5fc6\u4e86\u76f8\u5173\u7279\u5f81\uff0c\u5e76\u672a\u8fdb\u884c\u6df1\u5ea6\u7814\u7a76\uff0cRC\u5bb6\u65cf\u5c06\u4ee5\u4ee5\u4e0b\u987a\u5e8f\u5c55\u5f00\uff1aRC \u5bb6\u65cf\u5168\u8c8c \u2192 \u518d\u91cd\u70b9\u8bb2 RC4 \u539f\u7406\u3001\u9006\u5411\u8bc6\u522b\u3001\u4ee3\u7801\u5b9e\u73b0 \u2192 \u518d\u8865 RC2\/RC5\/RC6 \u7684\u7ed3\u6784\u7279\u5f81\u4e0e\u6700\u5c0f\u5b9e\u73b0\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u5e38\u89c1\u6210\u5458\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>RC2<\/strong>\uff1a\u5206\u7ec4\u5bc6\u7801<\/li>\n\n\n\n<li><strong>RC4<\/strong>\uff1a\u6d41\u5bc6\u7801\uff08\u6700\u4e3a\u5e38\u89c1\uff09<\/li>\n\n\n\n<li><strong>RC5<\/strong>\uff1a\u53c2\u6570\u5316\u5206\u7ec4\u5bc6\u7801<\/li>\n\n\n\n<li><strong>RC6<\/strong>\uff1aRC5 \u7684\u6539\u8fdb\u7248\uff0cAES \u5019\u9009\u7b97\u6cd5\u4e4b\u4e00<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">\u4e8c\u3001RC \u5bb6\u65cf\u603b\u4f53\u533a\u522b<\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>\u7b97\u6cd5<\/th><th>\u7c7b\u578b<\/th><th>\u6838\u5fc3\u7279\u70b9<\/th><th>CTF\u4e2d\u5e38\u89c1\u5ea6<\/th><th>\u9006\u5411\u8bc6\u522b\u96be\u5ea6<\/th><\/tr><\/thead><tbody><tr><td>RC2<\/td><td>\u5206\u7ec4\u5bc6\u7801<\/td><td>64-bit \u5206\u7ec4\uff0c\u5386\u53f2\u8f83\u8001<\/td><td>\u4f4e<\/td><td>\u4e2d<\/td><\/tr><tr><td>RC4<\/td><td>\u6d41\u5bc6\u7801<\/td><td>256\u5b57\u8282\u72b6\u6001\u6570\u7ec4\uff0cKSA+PRGA<\/td><td>\u5f88\u9ad8<\/td><td>\u4f4e<\/td><\/tr><tr><td>RC5<\/td><td>\u5206\u7ec4\u5bc6\u7801<\/td><td>\u53c2\u6570\u5316\u8bbe\u8ba1\uff0c\u6570\u636e\u76f8\u5173\u5faa\u73af\u79fb\u4f4d<\/td><td>\u4e2d<\/td><td>\u4e2d\u9ad8<\/td><\/tr><tr><td>RC6<\/td><td>\u5206\u7ec4\u5bc6\u7801<\/td><td>\u5728RC5\u57fa\u7840\u4e0a\u52a0\u5165\u4e58\u6cd5\uff0c128-bit\u5206\u7ec4<\/td><td>\u4e2d\u4f4e<\/td><td>\u9ad8<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">\u4e09\u3001RC4<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">RC4\u662f\u52a0\u89e3\u5bc6\u53ef\u9006\u7684\u52a0\u5bc6\u7b97\u6cd5\uff0c\u56e0\u6b64\u6211\u4eec\u53ea\u8bb2\u8ff0\u52a0\u5bc6\u8fc7\u7a0b\uff0cRC4\u52a0\u5bc6\u4e3b\u8981\u5b9e\u73b0\u4e24\u4e2a\u9636\u6bb5\uff1a<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>KSA\uff08Key Scheduling Algorithm\uff0c\u5bc6\u94a5\u8c03\u5ea6\uff09<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u521d\u59cb\u5316 <code>S[0..255]<\/code><\/li>\n\n\n\n<li>\u7528\u5bc6\u94a5\u4e0d\u65ad\u6253\u4e71 S \u76d2<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>PRGA\uff08Pseudo-Random Generation Algorithm\uff0c\u4f2a\u968f\u673a\u751f\u6210\uff09<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u4e0e\u660e\u6587\u5f02\u6216\u5f97\u5230\u5bc6\u6587<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u4e0d\u65ad\u4ea4\u6362 <code>S[i]<\/code>\u3001<code>S[j]<\/code><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u8f93\u51fa\u5bc6\u94a5\u6d41\u5b57\u8282<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">KSA<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><math xmlns=\"http:\/\/www.w3.org\/1998\/Math\/MathML\" display=\"block\"><semantics><mrow><mi>S<\/mi><mo stretchy=\"false\">[<\/mo><mi>i<\/mi><mo stretchy=\"false\">]<\/mo><mo>=<\/mo><mi>i<\/mi><mo separator=\"true\">,<\/mo><mspace width=\"1em\"><\/mspace><mi>i<\/mi><mo>=<\/mo><mn>0<\/mn><mo separator=\"true\">,<\/mo><mn>1<\/mn><mo separator=\"true\">,<\/mo><mo>\u2026<\/mo><mo separator=\"true\">,<\/mo><mn>255<\/mn><\/mrow><annotation encoding=\"application\/x-tex\">S[i] = i,\\quad i=0,1,\\dots,255<\/annotation><\/semantics><\/math><math xmlns=\"http:\/\/www.w3.org\/1998\/Math\/MathML\" display=\"block\"><semantics><mrow><mi>j<\/mi><mo>=<\/mo><mo stretchy=\"false\">(<\/mo><mi>j<\/mi><mo>+<\/mo><mi>S<\/mi><mo stretchy=\"false\">[<\/mo><mi>i<\/mi><mo stretchy=\"false\">]<\/mo><mo>+<\/mo><mi>K<\/mi><mo stretchy=\"false\">[<\/mo><mi>i<\/mi><mtext>\u200a<\/mtext><mo lspace=\"0.22em\" rspace=\"0.22em\"><mrow><mi mathvariant=\"normal\">m<\/mi><mi mathvariant=\"normal\">o<\/mi><mi mathvariant=\"normal\">d<\/mi><\/mrow><\/mo><mtext>\u200a<\/mtext><mi>k<\/mi><mi>e<\/mi><mi>y<\/mi><mi>l<\/mi><mi>e<\/mi><mi>n<\/mi><mo stretchy=\"false\">]<\/mo><mo stretchy=\"false\">)<\/mo><mtext>\u200a<\/mtext><mo lspace=\"0.22em\" rspace=\"0.22em\"><mrow><mi mathvariant=\"normal\">m<\/mi><mi mathvariant=\"normal\">o<\/mi><mi mathvariant=\"normal\">d<\/mi><\/mrow><\/mo><mtext>\u200a<\/mtext><mn>256<\/mn><\/mrow><annotation encoding=\"application\/x-tex\">j = (j + S[i] + K[i \\bmod keylen]) \\bmod 256<\/annotation><\/semantics><\/math><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u7136\u540e\u4ea4\u6362\uff1a<math xmlns=\"http:\/\/www.w3.org\/1998\/Math\/MathML\" display=\"block\"><semantics><mrow><mi>S<\/mi><mo stretchy=\"false\">[<\/mo><mi>i<\/mi><mo stretchy=\"false\">]<\/mo><mo separator=\"true\">,<\/mo><mi>S<\/mi><mo stretchy=\"false\">[<\/mo><mi>j<\/mi><mo stretchy=\"false\">]<\/mo><mo>=<\/mo><mi>S<\/mi><mo stretchy=\"false\">[<\/mo><mi>j<\/mi><mo stretchy=\"false\">]<\/mo><mo separator=\"true\">,<\/mo><mi>S<\/mi><mo stretchy=\"false\">[<\/mo><mi>i<\/mi><mo stretchy=\"false\">]<\/mo><\/mrow><annotation encoding=\"application\/x-tex\">S[i], S[j] = S[j], S[i]<\/annotation><\/semantics><\/math><\/p>\n\n\n\n<h3 class=\"wp-block-heading\">PRGA<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><math xmlns=\"http:\/\/www.w3.org\/1998\/Math\/MathML\" display=\"block\"><semantics><mrow><mi>i<\/mi><mo>=<\/mo><mo stretchy=\"false\">(<\/mo><mi>i<\/mi><mo>+<\/mo><mn>1<\/mn><mo stretchy=\"false\">)<\/mo><mtext>\u200a<\/mtext><mo lspace=\"0.22em\" rspace=\"0.22em\"><mrow><mi mathvariant=\"normal\">m<\/mi><mi mathvariant=\"normal\">o<\/mi><mi mathvariant=\"normal\">d<\/mi><\/mrow><\/mo><mtext>\u200a<\/mtext><mn>256<\/mn><\/mrow><annotation encoding=\"application\/x-tex\">i = (i + 1) \\bmod 256<\/annotation><\/semantics><\/math><math xmlns=\"http:\/\/www.w3.org\/1998\/Math\/MathML\" display=\"block\"><semantics><mrow><mi>j<\/mi><mo>=<\/mo><mo stretchy=\"false\">(<\/mo><mi>j<\/mi><mo>+<\/mo><mi>S<\/mi><mo stretchy=\"false\">[<\/mo><mi>i<\/mi><mo stretchy=\"false\">]<\/mo><mo stretchy=\"false\">)<\/mo><mtext>\u200a<\/mtext><mo lspace=\"0.22em\" rspace=\"0.22em\"><mrow><mi mathvariant=\"normal\">m<\/mi><mi mathvariant=\"normal\">o<\/mi><mi mathvariant=\"normal\">d<\/mi><\/mrow><\/mo><mtext>\u200a<\/mtext><mn>256<\/mn><\/mrow><annotation encoding=\"application\/x-tex\">j = (j + S[i]) \\bmod 256<\/annotation><\/semantics><\/math><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u4ea4\u6362\uff1a<math xmlns=\"http:\/\/www.w3.org\/1998\/Math\/MathML\" display=\"block\"><semantics><mrow><mi>S<\/mi><mo stretchy=\"false\">[<\/mo><mi>i<\/mi><mo stretchy=\"false\">]<\/mo><mo separator=\"true\">,<\/mo><mi>S<\/mi><mo stretchy=\"false\">[<\/mo><mi>j<\/mi><mo stretchy=\"false\">]<\/mo><mo>=<\/mo><mi>S<\/mi><mo stretchy=\"false\">[<\/mo><mi>j<\/mi><mo stretchy=\"false\">]<\/mo><mo separator=\"true\">,<\/mo><mi>S<\/mi><mo stretchy=\"false\">[<\/mo><mi>i<\/mi><mo stretchy=\"false\">]<\/mo><\/mrow><annotation encoding=\"application\/x-tex\">S[i], S[j] = S[j], S[i]<\/annotation><\/semantics><\/math><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u8f93\u51fa\u7d22\u5f15\uff1a<math xmlns=\"http:\/\/www.w3.org\/1998\/Math\/MathML\" display=\"block\"><semantics><mrow><mi>t<\/mi><mo>=<\/mo><mo stretchy=\"false\">(<\/mo><mi>S<\/mi><mo stretchy=\"false\">[<\/mo><mi>i<\/mi><mo stretchy=\"false\">]<\/mo><mo>+<\/mo><mi>S<\/mi><mo stretchy=\"false\">[<\/mo><mi>j<\/mi><mo stretchy=\"false\">]<\/mo><mo stretchy=\"false\">)<\/mo><mtext>\u200a<\/mtext><mo lspace=\"0.22em\" rspace=\"0.22em\"><mrow><mi mathvariant=\"normal\">m<\/mi><mi mathvariant=\"normal\">o<\/mi><mi mathvariant=\"normal\">d<\/mi><\/mrow><\/mo><mtext>\u200a<\/mtext><mn>256<\/mn><\/mrow><annotation encoding=\"application\/x-tex\">t = (S[i] + S[j]) \\bmod 256<\/annotation><\/semantics><\/math><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u5bc6\u94a5\u6d41\u5b57\u8282\uff1a<math xmlns=\"http:\/\/www.w3.org\/1998\/Math\/MathML\" display=\"block\"><semantics><mrow><mi>k<\/mi><mo>=<\/mo><mi>S<\/mi><mo stretchy=\"false\">[<\/mo><mi>t<\/mi><mo stretchy=\"false\">]<\/mo><\/mrow><annotation encoding=\"application\/x-tex\">k = S[t]<\/annotation><\/semantics><\/math><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u6700\u7ec8\uff1a<math xmlns=\"http:\/\/www.w3.org\/1998\/Math\/MathML\" display=\"block\"><semantics><mrow><mi>c<\/mi><mi>i<\/mi><mi>p<\/mi><mi>h<\/mi><mi>e<\/mi><mi>r<\/mi><mo stretchy=\"false\">[<\/mo><mi>i<\/mi><mo stretchy=\"false\">]<\/mo><mo>=<\/mo><mi>p<\/mi><mi>l<\/mi><mi>a<\/mi><mi>i<\/mi><mi>n<\/mi><mo stretchy=\"false\">[<\/mo><mi>i<\/mi><mo stretchy=\"false\">]<\/mo><mo>\u2295<\/mo><mi>k<\/mi><\/mrow><annotation encoding=\"application\/x-tex\">cipher[i] = plain[i] \\oplus k<\/annotation><\/semantics><\/math><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd9\u4e48\u8bf4\u53ef\u80fd\u6709\u4e9b\u62bd\u8c61\uff0c\u6211\u4eec\u76f4\u63a5\u7528\u4e00\u6bb5\u4ee3\u7801\u6765\u5c55\u793a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><strong>def rc4_crypt(key: bytes, data: bytes) -> bytes:\n    # KSA\n    S = list(range(256))\n    j = 0\n    key_len = len(key)\n\n    for i in range(256):\n        j = (j + S&#91;i] + key&#91;i % key_len]) &amp; 0xFF\n        S&#91;i], S&#91;j] = S&#91;j], S&#91;i]\n\n    # PRGA\n    i = 0\n    j = 0\n    out = bytearray()\n\n    for byte in data:\n        i = (i + 1) &amp; 0xFF\n        j = (j + S&#91;i]) &amp; 0xFF\n        S&#91;i], S&#91;j] = S&#91;j], S&#91;i]\n        k = S&#91;(S&#91;i] + S&#91;j]) &amp; 0xFF]\n        out.append(byte ^ k)\n\n    return bytes(out)<\/strong>\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">\u56db\u3001RC2\uff08\u57fa\u672c\u4e0d\u8003\u9b54\u6539\uff09<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">RC2\u7684\u52a0\u5bc6\u65b9\u5f0f\u6211\u4eec\u4e0d\u505a\u8be6\u7ec6\u7814\u7a76\uff0c\u53ea\u9700\u8981\u77e5\u9053RC2\u662fDES\u7684\u66ff\u4ee3\u7b97\u6cd5\uff0c\u4eff\u7167\u7684\u8fd8\u662fDES\u7684\u76f8\u5173\u6a21\u5f0f\uff0c\u660e\u6587\u548c\u5bc6\u6587\u90fd\u662f64bit\u6784\u6210\uff0c\u5bc6\u94a5\u957f\u5ea6\u4e3a1bit\u5230128bit\uff0c\u4e0b\u9762\u7ed9\u51fa\u7b80\u5355\u5e93\u51fd\u6570\u8c03\u7528\u65b9\u6cd5<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#ECB\u6a21\u5f0f\nfrom Crypto.Cipher import ARC2\nfrom Crypto.Util.Padding import pad, unpad\n\nkey = b\"12345678\"   # RC2 \u5e38\u89c1 8~16 \u5b57\u8282\nplaintext = b\"hello rc2\"\n\ncipher = ARC2.new(key, ARC2.MODE_ECB)\nciphertext = cipher.encrypt(pad(plaintext, ARC2.block_size))\n\ncipher2 = ARC2.new(key, ARC2.MODE_ECB)\ndecrypted = unpad(cipher2.decrypt(ciphertext), ARC2.block_size)\n\nprint(\"ciphertext:\", ciphertext.hex())\nprint(\"decrypted :\", decrypted)\n\n#CBC\u6a21\u5f0f\nfrom Crypto.Cipher import ARC2\nfrom Crypto.Util.Padding import pad, unpad\n\nkey = b\"12345678\"\niv = b\"12345678\"\nplaintext = b\"hello rc2 cbc\"\n\ncipher = ARC2.new(key, ARC2.MODE_CBC, iv=iv)\nciphertext = cipher.encrypt(pad(plaintext, ARC2.block_size))\n\ncipher2 = ARC2.new(key, ARC2.MODE_CBC, iv=iv)\ndecrypted = unpad(cipher2.decrypt(ciphertext), ARC2.block_size)\n\nprint(\"ciphertext:\", ciphertext.hex())\nprint(\"decrypted :\", decrypted)<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">\u4e94\u3001RC5|RC6<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">RC5 \u6700\u5173\u952e\u7684\u6807\u7b7e\u6709\u4e09\u4e2a\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>w<\/code>\uff1a\u5b57\u957f<\/li>\n\n\n\n<li><code>r<\/code>\uff1a\u8f6e\u6570<\/li>\n\n\n\n<li><code>b<\/code>\uff1a\u5bc6\u94a5\u957f\u5ea6\uff08\u5b57\u8282\uff09<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">\u5982\u679c\u53cd\u7f16\u8bd1\u4ee3\u7801\u4e2d\u5927\u91cf\u51fa\u73b0 ROL\/ROR\uff0c\u800c\u4e14\u79fb\u4f4d\u4f4d\u6570\u4e0d\u662f\u5e38\u6570\uff0c\u800c\u662f\u7531\u53e6\u4e00\u4e2a\u5bc4\u5b58\u5668\u6216\u53d8\u91cf\u51b3\u5b9a\uff0c\u90a3\u4e48\u5f88\u53ef\u80fd\u8981\u5f80 RC5\/RC6 \u65b9\u5411\u60f3\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">RC5 \u9006\u5411\u8bc6\u522b\u70b9<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>\u5e38\u89c1\u9b54\u6570\uff1a\n<ul class=\"wp-block-list\">\n<li><code>0xB7E15163<\/code><\/li>\n\n\n\n<li><code>0x9E3779B9<\/code><\/li>\n<\/ul>\n<\/li>\n\n\n\n<li>\u5206\u7ec4\u901a\u5e38\u662f\u4e24\u4e2a\u5b57\uff08\u5982\u4e24\u4e2a 32 \u4f4d\uff09<\/li>\n\n\n\n<li>\u6bcf\u8f6e\u7ed3\u6784\u5f88\u50cf\uff1a <math xmlns=\"http:\/\/www.w3.org\/1998\/Math\/MathML\" display=\"block\"><semantics><mrow><mi>A<\/mi><mo>=<\/mo><mo stretchy=\"false\">(<\/mo><mo stretchy=\"false\">(<\/mo><mi>A<\/mi><mo>\u2295<\/mo><mi>B<\/mi><mo stretchy=\"false\">)<\/mo><mo>\u22d8<\/mo><mi>B<\/mi><mo stretchy=\"false\">)<\/mo><mo>+<\/mo><mi>S<\/mi><mo stretchy=\"false\">[<\/mo><mn>2<\/mn><mi>i<\/mi><mo stretchy=\"false\">]<\/mo><\/mrow><annotation encoding=\"application\/x-tex\">A = ((A \\oplus B) \\lll B) + S[2i]<\/annotation><\/semantics><\/math><math xmlns=\"http:\/\/www.w3.org\/1998\/Math\/MathML\" display=\"block\"><semantics><mrow><mi>B<\/mi><mo>=<\/mo><mo stretchy=\"false\">(<\/mo><mo stretchy=\"false\">(<\/mo><mi>B<\/mi><mo>\u2295<\/mo><mi>A<\/mi><mo stretchy=\"false\">)<\/mo><mo>\u22d8<\/mo><mi>A<\/mi><mo stretchy=\"false\">)<\/mo><mo>+<\/mo><mi>S<\/mi><mo stretchy=\"false\">[<\/mo><mn>2<\/mn><mi>i<\/mi><mo>+<\/mo><mn>1<\/mn><mo stretchy=\"false\">]<\/mo><\/mrow><annotation encoding=\"application\/x-tex\">B = ((B \\oplus A) \\lll A) + S[2i+1]<\/annotation><\/semantics><\/math><\/li>\n<\/ol>\n\n\n\n<p class=\"wp-block-paragraph\">RC6 \u53ef\u4ee5\u8bf4\u662f\u201cRC5 \u52a0\u5f3a\u7248\u201d\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">RC6\u91c7\u7528128-bit \u5206\u7ec4<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u7528\u56db\u4e2a\u5bc4\u5b58\u5668\uff1a<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><code>A, B, C, D<\/code><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u5927\u91cf\u5faa\u73af\u79fb\u4f4d<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd8\u4f1a\u51fa\u73b0\u4e58\u6cd5\uff1a<math xmlns=\"http:\/\/www.w3.org\/1998\/Math\/MathML\" display=\"block\"><semantics><mrow><mi>t<\/mi><mo>=<\/mo><mo stretchy=\"false\">(<\/mo><mi>B<\/mi><mo>\u00d7<\/mo><mo stretchy=\"false\">(<\/mo><mn>2<\/mn><mi>B<\/mi><mo>+<\/mo><mn>1<\/mn><mo stretchy=\"false\">)<\/mo><mo stretchy=\"false\">)<\/mo><mo>\u22d8<\/mo><msub><mrow><mi>log<\/mi><mo>\u2061<\/mo><\/mrow><mn>2<\/mn><\/msub><mi>w<\/mi><\/mrow><annotation encoding=\"application\/x-tex\">t = (B \\times (2B+1)) \\lll \\log_2 w<\/annotation><\/semantics><\/math><math xmlns=\"http:\/\/www.w3.org\/1998\/Math\/MathML\" display=\"block\"><semantics><mrow><mi>u<\/mi><mo>=<\/mo><mo stretchy=\"false\">(<\/mo><mi>D<\/mi><mo>\u00d7<\/mo><mo stretchy=\"false\">(<\/mo><mn>2<\/mn><mi>D<\/mi><mo>+<\/mo><mn>1<\/mn><mo stretchy=\"false\">)<\/mo><mo stretchy=\"false\">)<\/mo><mo>\u22d8<\/mo><msub><mrow><mi>log<\/mi><mo>\u2061<\/mo><\/mrow><mn>2<\/mn><\/msub><mi>w<\/mi><\/mrow><annotation encoding=\"application\/x-tex\">u = (D \\times (2D+1)) \\lll \\log_2 w<\/annotation><\/semantics><\/math><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#RC5\ndef rol(x, r, w=32):\n    mask = (1 &lt;&lt; w) - 1\n    r &amp;= (w - 1)\n    return ((x &lt;&lt; r) &amp; mask) | (x >> (w - r))\n\n\ndef ror(x, r, w=32):\n    mask = (1 &lt;&lt; w) - 1\n    r &amp;= (w - 1)\n    return (x >> r) | ((x &lt;&lt; (w - r)) &amp; mask)\n\n\ndef rc5_key_schedule(key: bytes, w=32, r=12):\n    # \u8fd9\u91cc\u53ea\u5199\u6700\u5e38\u7528\u7684 w=32\n    if w != 32:\n        raise ValueError(\"This minimal implementation uses w=32 only.\")\n\n    P = 0xB7E15163\n    Q = 0x9E3779B9\n    u = w \/\/ 8\n\n    c = max(1, (len(key) + u - 1) \/\/ u)\n    L = &#91;0] * c\n\n    # \u6309 little-endian \u88c5\u5165 L\n    for i in range(len(key) - 1, -1, -1):\n        L&#91;i \/\/ u] = ((L&#91;i \/\/ u] &lt;&lt; 8) + key&#91;i]) &amp; 0xFFFFFFFF\n\n    t = 2 * (r + 1)\n    S = &#91;0] * t\n    S&#91;0] = P\n    for i in range(1, t):\n        S&#91;i] = (S&#91;i - 1] + Q) &amp; 0xFFFFFFFF\n\n    A = B = i = j = 0\n    for _ in range(3 * max(c, t)):\n        A = S&#91;i] = rol((S&#91;i] + A + B) &amp; 0xFFFFFFFF, 3, w)\n        B = L&#91;j] = rol((L&#91;j] + A + B) &amp; 0xFFFFFFFF, (A + B), w)\n        i = (i + 1) % t\n        j = (j + 1) % c\n\n    return S\n\n\ndef rc5_encrypt_block(block: bytes, S, w=32, r=12):\n    if len(block) != 8:\n        raise ValueError(\"RC5-32 block size must be 8 bytes.\")\n\n    A = int.from_bytes(block&#91;:4], \"little\")\n    B = int.from_bytes(block&#91;4:], \"little\")\n\n    A = (A + S&#91;0]) &amp; 0xFFFFFFFF\n    B = (B + S&#91;1]) &amp; 0xFFFFFFFF\n\n    for i in range(1, r + 1):\n        A = (rol(A ^ B, B, w) + S&#91;2 * i]) &amp; 0xFFFFFFFF\n        B = (rol(B ^ A, A, w) + S&#91;2 * i + 1]) &amp; 0xFFFFFFFF\n\n    return A.to_bytes(4, \"little\") + B.to_bytes(4, \"little\")\n\n\ndef rc5_decrypt_block(block: bytes, S, w=32, r=12):\n    if len(block) != 8:\n        raise ValueError(\"RC5-32 block size must be 8 bytes.\")\n\n    A = int.from_bytes(block&#91;:4], \"little\")\n    B = int.from_bytes(block&#91;4:], \"little\")\n\n    for i in range(r, 0, -1):\n        B = ror((B - S&#91;2 * i + 1]) &amp; 0xFFFFFFFF, A, w) ^ A\n        A = ror((A - S&#91;2 * i]) &amp; 0xFFFFFFFF, B, w) ^ B\n\n    B = (B - S&#91;1]) &amp; 0xFFFFFFFF\n    A = (A - S&#91;0]) &amp; 0xFFFFFFFF\n\n    return A.to_bytes(4, \"little\") + B.to_bytes(4, \"little\")\n\n\nif __name__ == \"__main__\":\n    key = bytes(range(16))      # 16\u5b57\u8282\u5bc6\u94a5\n    pt = b\"12345678\"            # 8\u5b57\u8282\u5206\u7ec4\n\n    S = rc5_key_schedule(key, w=32, r=12)\n    ct = rc5_encrypt_block(pt, S, w=32, r=12)\n    dt = rc5_decrypt_block(ct, S, w=32, r=12)\n\n    print(\"pt:\", pt)\n    print(\"ct:\", ct.hex())\n    print(\"dt:\", dt)\n#RC6\ndef rol(x, r, w=32):\n    mask = (1 &lt;&lt; w) - 1\n    r &amp;= (w - 1)\n    return ((x &lt;&lt; r) &amp; mask) | (x >> (w - r))\n\n\ndef ror(x, r, w=32):\n    mask = (1 &lt;&lt; w) - 1\n    r &amp;= (w - 1)\n    return (x >> r) | ((x &lt;&lt; (w - r)) &amp; mask)\n\n\ndef rc6_key_schedule(key: bytes, w=32, r=20):\n    if w != 32:\n        raise ValueError(\"This minimal implementation uses w=32 only.\")\n\n    P = 0xB7E15163\n    Q = 0x9E3779B9\n    u = w \/\/ 8\n\n    c = max(1, (len(key) + u - 1) \/\/ u)\n    L = &#91;0] * c\n\n    for i in range(len(key) - 1, -1, -1):\n        L&#91;i \/\/ u] = ((L&#91;i \/\/ u] &lt;&lt; 8) + key&#91;i]) &amp; 0xFFFFFFFF\n\n    t = 2 * r + 4\n    S = &#91;0] * t\n    S&#91;0] = P\n    for i in range(1, t):\n        S&#91;i] = (S&#91;i - 1] + Q) &amp; 0xFFFFFFFF\n\n    A = B = i = j = 0\n    for _ in range(3 * max(c, t)):\n        A = S&#91;i] = rol((S&#91;i] + A + B) &amp; 0xFFFFFFFF, 3, w)\n        B = L&#91;j] = rol((L&#91;j] + A + B) &amp; 0xFFFFFFFF, (A + B), w)\n        i = (i + 1) % t\n        j = (j + 1) % c\n\n    return S\n\n\ndef rc6_encrypt_block(block: bytes, S, w=32, r=20):\n    if len(block) != 16:\n        raise ValueError(\"RC6 block size must be 16 bytes.\")\n\n    A = int.from_bytes(block&#91;0:4], \"little\")\n    B = int.from_bytes(block&#91;4:8], \"little\")\n    C = int.from_bytes(block&#91;8:12], \"little\")\n    D = int.from_bytes(block&#91;12:16], \"little\")\n\n    B = (B + S&#91;0]) &amp; 0xFFFFFFFF\n    D = (D + S&#91;1]) &amp; 0xFFFFFFFF\n\n    lgw = 5  # log2(32)\n\n    for i in range(1, r + 1):\n        t = rol((B * ((2 * B + 1) &amp; 0xFFFFFFFF)) &amp; 0xFFFFFFFF, lgw, w)\n        u = rol((D * ((2 * D + 1) &amp; 0xFFFFFFFF)) &amp; 0xFFFFFFFF, lgw, w)\n\n        A = (rol(A ^ t, u, w) + S&#91;2 * i]) &amp; 0xFFFFFFFF\n        C = (rol(C ^ u, t, w) + S&#91;2 * i + 1]) &amp; 0xFFFFFFFF\n\n        A, B, C, D = B, C, D, A\n\n    A = (A + S&#91;2 * r + 2]) &amp; 0xFFFFFFFF\n    C = (C + S&#91;2 * r + 3]) &amp; 0xFFFFFFFF\n\n    return (\n        A.to_bytes(4, \"little\") +\n        B.to_bytes(4, \"little\") +\n        C.to_bytes(4, \"little\") +\n        D.to_bytes(4, \"little\")\n    )\n\n\ndef rc6_decrypt_block(block: bytes, S, w=32, r=20):\n    if len(block) != 16:\n        raise ValueError(\"RC6 block size must be 16 bytes.\")\n\n    A = int.from_bytes(block&#91;0:4], \"little\")\n    B = int.from_bytes(block&#91;4:8], \"little\")\n    C = int.from_bytes(block&#91;8:12], \"little\")\n    D = int.from_bytes(block&#91;12:16], \"little\")\n\n    lgw = 5\n\n    C = (C - S&#91;2 * r + 3]) &amp; 0xFFFFFFFF\n    A = (A - S&#91;2 * r + 2]) &amp; 0xFFFFFFFF\n\n    for i in range(r, 0, -1):\n        A, B, C, D = D, A, B, C\n\n        u = rol((D * ((2 * D + 1) &amp; 0xFFFFFFFF)) &amp; 0xFFFFFFFF, lgw, w)\n        t = rol((B * ((2 * B + 1) &amp; 0xFFFFFFFF)) &amp; 0xFFFFFFFF, lgw, w)\n\n        C = ror((C - S&#91;2 * i + 1]) &amp; 0xFFFFFFFF, t, w) ^ u\n        A = ror((A - S&#91;2 * i]) &amp; 0xFFFFFFFF, u, w) ^ t\n\n    D = (D - S&#91;1]) &amp; 0xFFFFFFFF\n    B = (B - S&#91;0]) &amp; 0xFFFFFFFF\n\n    return (\n        A.to_bytes(4, \"little\") +\n        B.to_bytes(4, \"little\") +\n        C.to_bytes(4, \"little\") +\n        D.to_bytes(4, \"little\")\n    )\n\n\nif __name__ == \"__main__\":\n    key = bytes(range(16))\n    pt = b\"0123456789ABCDEF\"   # 16\u5b57\u8282\u5206\u7ec4\n\n    S = rc6_key_schedule(key, w=32, r=20)\n    ct = rc6_encrypt_block(pt, S, w=32, r=20)\n    dt = rc6_decrypt_block(ct, S, w=32, r=20)\n\n    print(\"pt:\", pt)\n    print(\"ct:\", ct.hex())\n    print(\"dt:\", dt)<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u4e00\u3001\u5f15\u8a00 \u8fd1\u671f\u4e5f\u662f\u5bf9\u52a0\u5bc6\u7b97\u6cd5\u4e5f\u662f\u505a\u4e86\u4e00\u5957\u8be6\u5c3d\u7684\u5b66\u4e60\uff0c\u4e3b\u8981\u662f\u524d\u671f\u5b66\u4e60\u8fc7\u7a0b\u4e2d\u4ec5\u8bb0\u5fc6\u4e86\u76f8\u5173\u7279\u5f81\uff0c\u5e76\u672a\u8fdb\u884c\u6df1\u5ea6\u7814\u7a76\uff0cRC\u5bb6\u65cf\u5c06\u4ee5\u4ee5\u4e0b\u987a\u5e8f\u5c55 &#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"emotion":"","emotion_color":"","title_style":"","license":"","footnotes":""},"categories":[1],"tags":[],"class_list":["post-186","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/www.sherlock666.cn\/index.php\/wp-json\/wp\/v2\/posts\/186","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.sherlock666.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.sherlock666.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.sherlock666.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.sherlock666.cn\/index.php\/wp-json\/wp\/v2\/comments?post=186"}],"version-history":[{"count":2,"href":"https:\/\/www.sherlock666.cn\/index.php\/wp-json\/wp\/v2\/posts\/186\/revisions"}],"predecessor-version":[{"id":188,"href":"https:\/\/www.sherlock666.cn\/index.php\/wp-json\/wp\/v2\/posts\/186\/revisions\/188"}],"wp:attachment":[{"href":"https:\/\/www.sherlock666.cn\/index.php\/wp-json\/wp\/v2\/media?parent=186"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.sherlock666.cn\/index.php\/wp-json\/wp\/v2\/categories?post=186"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.sherlock666.cn\/index.php\/wp-json\/wp\/v2\/tags?post=186"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}