{"id":502,"date":"2026-11-13T13:39:50","date_gmt":"2026-11-13T05:39:50","guid":{"rendered":"https:\/\/hoho.live\/luda\/index.php\/2026\/11\/13\/how-rc4-generates-a-keystream-from-a-permutation\/"},"modified":"2026-11-13T21:59:16","modified_gmt":"2026-11-13T13:59:16","slug":"how-rc4-generates-a-keystream-from-a-permutation","status":"publish","type":"post","link":"https:\/\/hoho.live\/luda\/index.php\/2026\/11\/13\/how-rc4-generates-a-keystream-from-a-permutation\/","title":{"rendered":"How RC4 generates a keystream from a permutation"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\"><strong>Short answer:<\/strong> <strong>RC4<\/strong> builds a 256-byte permutation from the key, then walks that permutation to emit a keystream byte. The key schedule adds the next key byte into an index and swaps. The generator increments <code>i<\/code>, adds <code>S[i]<\/code> into <code>j<\/code>, swaps, and emits <code>S[(S[i] + S[j]) mod 256]<\/code>. The key <code>Key<\/code> and the plaintext <code>Plaintext<\/code> produce <code>bbf316e8d940af0ad3<\/code>.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>What the problem actually asks<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\">A stream cipher can be a walk on a permutation instead of a block function. RC4 is that walk. The plaintext is xored with the emitted bytes, and decryption is the same walk. The permutation is the whole secret state after the key schedule. The keystream is biased. Those biases distinguish the pad from a random string, which is why the cipher is obsolete. The fragment shows the walk. It is not a procedure for recovering a key.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>What the algorithm guarantees<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\">The same key reproduces the same keystream. Decryption is the same function. The first keystream bytes are not uniformly distributed, and a keystream from this construction is not a substitute for a random pad. A new design does not use RC4.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>The working steps<\/strong><\/h4>\n\n<ol class=\"wp-block-list\">\n<li>Start with the identity permutation of 0 through 255.<\/li>\n<li>For each index, add the permutation entry and the next key byte into <code>j<\/code>, and swap the two entries.<\/li>\n<li>For each plaintext byte, increment <code>i<\/code>, add <code>S[i]<\/code> into <code>j<\/code>, swap, and xor the plaintext byte with <code>S[(S[i] + S[j]) mod 256]<\/code>.<\/li>\n<\/ol>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>Approaches that look simpler and fail<\/strong><\/h4>\n\n<figure class=\"wp-block-table\">\n<table>\n<thead>\n<tr>\n<th>Approach<\/th>\n<th>Why it fails as a current stream cipher<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>Assume the emitted bytes are an unbiased pad<\/td>\n<td>The keystream distribution is biased. A biased pad is distinguishable from a random pad.<\/td>\n<\/tr>\n<tr>\n<td>Drop the first bytes and keep the same construction for new work<\/td>\n<td>Discarding a prefix was an attempted patch. The cipher remains retired.<\/td>\n<\/tr>\n<tr>\n<td>Reuse one keystream for two messages<\/td>\n<td>The xor of the ciphertexts cancels the pad, as it does for any stream cipher.<\/td>\n<\/tr>\n<tr>\n<td>Use the permutation walk as a hash<\/td>\n<td>The walk is a keystream. It is not a collision-resistant digest.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/figure>\n\n\n<p class=\"wp-block-paragraph\">ChaCha20 is a current keystream with a nonce and a counter. RC4 is the historical permutation walk.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>A working example<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\">This Python fragment walks the RC4 permutation for the key <code>Key<\/code> and the plaintext <code>Plaintext<\/code>. It prints <code>bbf316e8d940af0ad3<\/code>.<\/p>\n\n<pre><code class=\"language-python\">def rc4(key: bytes, data: bytes) -&gt; bytes:\n    s = list(range(256))\n    j = 0\n    for i in range(256):\n        j = (j + s[i] + key[i % len(key)]) % 256\n        s[i], s[j] = s[j], s[i]\n    i = j = 0\n    out = bytearray()\n    for byte in data:\n        i = (i + 1) % 256\n        j = (j + s[i]) % 256\n        s[i], s[j] = s[j], s[i]\n        out.append(byte ^ s[(s[i] + s[j]) % 256])\n    return bytes(out)\n\ncipher = rc4(b&quot;Key&quot;, b&quot;Plaintext&quot;)\nassert cipher.hex() == &quot;bbf316e8d940af0ad3&quot;\nprint(cipher.hex())\n<\/code><\/pre>\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>Where it is used<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\">RC4 was the stream cipher of <strong>WEP<\/strong> and of old TLS cipher suites. Both were withdrawn because the keystream is biased. ChaCha20 is the stream cipher those protocols moved to.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>When you can drop this algorithm<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\">Drop RC4. The 256-byte permutation is the historical stream cipher. A current keystream is ChaCha20, with a nonce that is not repeated under the same key.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>Why this stays published<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\">We publish this so a search for RC4 returns the permutation walk and the reason the keystream is retired. <strong>RC4 swaps its way through a 256-byte permutation to emit a keystream, and that keystream is biased enough that the cipher is obsolete.<\/strong><\/p>\n\n\n<p class=\"wp-block-paragraph\">\u672c\u6587\u7531 HoHo \u8207 AI \u5354\u4f5c\u6574\u7406\uff0c\u6700\u5f8c\u66f4\u65b0\u65bc 2026 \u5e74 11 \u6708 13 \u65e5\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Short answer: RC4 builds a 256-byte permutation from the key, then walks that permutation to emit a keystream byte. The key schedule adds the next key byte into an index and swaps. The generator increments i, adds S[i] into j, swaps, and emits S[(S[i] + S[j]) mod 256]. The&hellip;<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[30,49],"tags":[],"class_list":["post-502","post","type-post","status-publish","format-standard","hentry","category-algorithms","category-cryptography"],"_links":{"self":[{"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/posts\/502","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/comments?post=502"}],"version-history":[{"count":1,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/posts\/502\/revisions"}],"predecessor-version":[{"id":714,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/posts\/502\/revisions\/714"}],"wp:attachment":[{"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/media?parent=502"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/categories?post=502"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/tags?post=502"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}