Encrypt or decrypt any text using the RC4 stream cipher. Symmetric key algorithm for educational purposes.
RC4 (Rivest Cipher 4) is a widely used stream cipher designed by Ron Rivest in 1987. It's known for its simplicity and speed, generating a pseudorandom stream of bits (keystream) which is XORed with the plaintext.
Encryption/Decryption:
ciphertext = plaintext XOR keystream
plaintext = ciphertext XOR keystream
(same operation)
RC4 consists of two main parts: Key Scheduling Algorithm (KSA) and Pseudo-Random Generation Algorithm (PRGA).
for i from 0 to 255
S[i] = i
j = 0
for i from 0 to 255
j = (j + S[i] + key[i mod keylength]) mod 256
swap S[i] and S[j]
i = 0, j = 0
while generating keystream:
i = (i + 1) mod 256
j = (j + S[i]) mod 256
swap S[i] and S[j]
K = S[(S[i] + S[j]) mod 256] // keystream byte
output K
Each keystream byte K is XORed with the corresponding plaintext/ciphertext byte.
Key: "Key" (bytes [75, 101, 121]) Plaintext: "Plain" (bytes [80, 108, 97, 105, 110])
After KSA and PRGA, a keystream (e.g., [171, 235, 12, 89, 210]) is produced. XOR yields ciphertext. The tool above performs this exact process.