Rc4 Algorithm using Matlab without functions
Version 1.0.1 (1.37 KB) by
Gokula Krishnan
RC4 is a symmetric key stream cipher.
RC4 creates a pseudo-random bit stream (a keystream). These, like any other stream cipher, can be used for encryption by utilizing bit-wise exclusive or to combine it with the plaintext. The same procedure is used for decryption (since exclusive-OR is a symmetric operation).
The cipher uses a secret internal state that is divided into two sections to generate the keystream −
Each of the 256 available bytes is permuted.
Two index pointers (8 bits each).
The key-scheduling algorithm is known to initialize the permutation using a variable-length key, typically between 40 and 256 bits (KSA). A pseudo-random generating technique then generates the stream of bits.
Flow Diagram:
Key-scheduling algorithm (KSA)
The key-scheduling algorithm is used to initialize the permutation in the array "S". "keylength" is defined as the number of bytes in the key and can be in the range 1 ≤ keylength ≤ 256, typically between 5 and 16, corresponding to a key length of 40–128 bits. First, the array "S" is initialized to the identity permutation. S is then processed for 256 iterations in a similar way to the main PRGA, but also mixes in bytes of the key at the same time.
for i from 0 to 255
S[i] := i
end
j := 0
for i from 0 to 255
swap values of S[i] and S[j]
end
Pseudo-random generation algorithm (PRGA)
The lookup stage of RC4. The output byte is selected by looking up the values of S[i] and S[j], adding them together modulo 256, and then using the sum as an index into S; S(S[i] + S[j]) is used as a byte of the key stream K.
For as many iterations as are needed, the PRGA modifies the state and outputs a byte of the keystream. In each iteration, the PRGA:
increments i;
looks up the ith element of S, S[i], and adds that to j;
exchanges the values of S[i] and S[j], then uses the sum S[i] + S[j] (modulo 256) as an index to fetch a third element of S (the keystream value K below);
then bitwise exclusive ORed (XORed) with the next byte of the message to produce the next byte of either ciphertext or plaintext.
Each element of S is swapped with another element at least once every 256 iterations.
i := 0
j := 0
while GeneratingOutput:
i := (i + 1) mod 256
j := (j + S[i]) mod 256
swap values of S[i] and S[j]
K := S[(S[i] + S[j]) mod 256]
output K endwhile
For encryption −
The user enters the Plaintext and a secret key.
For the secret key entered, the encryption engine creates the keystream using the KSA and PRGA algorithms.
Plaintext is XORed with the generated keystream. Because RC4 is a stream cipher, byte-by-byte XORing is used to generate the encrypted text.
This encrypted text is now sent in encrypted form to the intended recipient.
For Decryption −
The same byte-wise X-OR technique is used on the ciphertext to decrypt it.
Cite As
Gokula Krishnan (2026). Rc4 Algorithm using Matlab without functions (https://www.mathworks.com/matlabcentral/fileexchange/127344-rc4-algorithm-using-matlab-without-functions), MATLAB Central File Exchange. Retrieved .
MATLAB Release Compatibility
Created with
R2023a
Compatible with any release
Platform Compatibility
Windows macOS LinuxTags
Discover Live Editor
Create scripts with code, output, and formatted text in a single executable document.
