From aca1721a4186ec0c173253b1386a5bb691529177 Mon Sep 17 00:00:00 2001 From: Manuel Bosisio <128630998+choppadebug@users.noreply.github.com> Date: Sun, 19 Apr 2026 17:20:48 +0200 Subject: [PATCH 1/2] Add README documentation for RC4 cipher --- Uncategorized/RC4/README.md | 104 ++++++++++++++++++++++++++++++++++++ 1 file changed, 104 insertions(+) create mode 100644 Uncategorized/RC4/README.md diff --git a/Uncategorized/RC4/README.md b/Uncategorized/RC4/README.md new file mode 100644 index 00000000..e9aef696 --- /dev/null +++ b/Uncategorized/RC4/README.md @@ -0,0 +1,104 @@ +# RC4 (Keystream + Stream Cipher) + +📌 Description +-------------- + +**RC4** (Rivest Cipher 4) is a symmetric stream cipher designed by Ron Rivest in 1987. It is known for its simplicity and speed in software, generating a **pseudorandom keystream** that is combined with plaintext to produce ciphertext. + +RC4 has historically been used in protocols such as SSL/TLS and WEP, but it is now considered insecure due to multiple discovered vulnerabilities. + +* * * * * + +⚙️ Keystream Generation (Pseudo-Random Generation) +-------------------------------------------------- + +RC4 operates using an internal state consisting of: + +- A permutation array `S` of 256 bytes +- Two indices `i` and `j` + +### 1\. Key-Scheduling Algorithm (KSA) + +Initializes the permutation `S` using the secret key: + +- Start with: + + S[i] = i for i = 0..255\ + j = 0 + +- For each `i`: + + j = (j + S[i] + key[i mod keylength]) mod 256\ + swap(S[i], S[j]) + +This step mixes the key into the internal state. + +* * * * * + +### 2\. Pseudo-Random Generation Algorithm (PRGA) + +Generates the keystream byte-by-byte: + +- For each output byte: + + i = (i + 1) mod 256\ + j = (j + S[i]) mod 256\ + swap(S[i], S[j])\ + t = (S[i] + S[j]) mod 256\ + output S[t] + +The resulting sequence is a **pseudorandom keystream** used for encryption. + +* * * * * + +🔐 Encryption & Decryption (XOR) +-------------------------------- + +RC4 is a stream cipher, meaning encryption is performed by combining the keystream with plaintext using **bitwise XOR**: + +ciphertext = plaintext ⊕ keystream + +Decryption uses the same operation: + +plaintext = ciphertext ⊕ keystream + +This works because XOR is its own inverse. + +* * * * * + +⚠️ Weaknesses +------------- + +RC4 is no longer considered secure due to several well-known issues: + +- **Biased keystream output**\ + Early bytes of the keystream are statistically biased, leaking information about the key. +- **Weak key scheduling (KSA)**\ + Certain key patterns lead to predictable states, enabling attacks such as the Fluhrer--Mantin--Shamir attack. +- **Key reuse / related-key vulnerabilities**\ + Reusing keys or combining keys improperly can expose secret information. +- **No built-in authentication**\ + RC4 is vulnerable to bit-flipping attacks if not combined with a message authentication mechanism. +- **Deprecated in modern protocols**\ + Due to these weaknesses, RC4 has been removed or prohibited in modern standards (e.g., TLS). + +* * * * * + +📊 Example Outputs +------------------ + +The following examples are adapted from RC4 Wikipedia article.\ +The **first row has been verified against this implementation**. + +| Key | Keystream | Plaintext | Ciphertext | +| --- | --- | --- | --- | +| Key | EB9F7781B734CA72A719... | Plaintext | BBF316E8D940AF0AD3 | +| Wiki | 6044DB6D41B7... | pedia | 1021BF0420 | +| Secret | 04D46B053CA87B59... | Attack at dawn | 45A01F645FC35B383552544B9BF5 | + +* * * * * + +📚 References +------------- + +- [Wikipedia](https://en.wikipedia.org/wiki/RC4) From 44b98f3035cb47b8040669e516d9b5809080b02a Mon Sep 17 00:00:00 2001 From: Manuel Bosisio <128630998+choppadebug@users.noreply.github.com> Date: Sun, 19 Apr 2026 17:22:05 +0200 Subject: [PATCH 2/2] Added js code for RC4 algorithm visualization --- Uncategorized/RC4/code.js | 153 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 153 insertions(+) create mode 100644 Uncategorized/RC4/code.js diff --git a/Uncategorized/RC4/code.js b/Uncategorized/RC4/code.js new file mode 100644 index 00000000..a37353a7 --- /dev/null +++ b/Uncategorized/RC4/code.js @@ -0,0 +1,153 @@ +// Run on https://algorithm-visualizer.org/ + +// import visualization libraries { +const { Tracer, Array1DTracer, ChartTracer, LogTracer, Randomize, Layout, VerticalLayout } = require('algorithm-visualizer'); +// } + +// Define tracer variables { +const chart = new ChartTracer(); +const chart_T = new ChartTracer(); +const tracer = new Array1DTracer(); +const tracer_T = new Array1DTracer(); +const logger = new LogTracer(); +const stream_logger = new LogTracer(); +Layout.setRoot(new VerticalLayout([chart, tracer_T, stream_logger])); +// Layout.setRoot(new VerticalLayout([chart, tracer_T, logger, stream_logger])); // *uncomment this line to see indexes actual values at runtime* +// } + +// Define working variables +let N = 256; // <--- set here desired S size (default 256) +const S = Array(N); +const T = Array(N); +const secret = []; +const key = "Key"; // <-- set here secret key (any keysize <=N) +for(let i=0; i