How does an RC4 encryption algorithm work

The RC4 (Rivest Cipher 4) encryption algorithm is a symmetric key encryption algorithm that encrypts plain text in small chunks producing multiple stream cipherIt is a cryptographic cipher to encrypt text to produce cipher text and vice versa.. It encrypts the system's data and ensures the secure delivery of confidential data over the websites. It is used in numerous applications, such as TLS (transport layer security).

Implementation

RC4 encryption algorithm comprises two components—KSA (Key scheduling algorithm) and PRGA (Pseudo random generation algorithm). These two algorithms together help the rc4 algorithm to produce the stream cipher.

Implementation of rc4 encryption algorithm

Following are the procedures involved in RC4 encryption:

  • The user has to provide plain text as input.

Step 1: Produce keystream from the secret key using KSA and PRGA algorithms:

  • The user also has to provide the secret key as input.
  • Initialize an array of 256 bytes and a temporary array, T, is generated where the first (k - len) elements of the key, K, are copied into it as shown below:
char S[256];
for(int i=0;i<256;i++){
  S[i] = i;
  T[i] = K[i mod (k - len)]
}

The main purpose of creating a temporary array is to provide an initial permutation for the S array.

  • Run the KSA on the altered array. It is used to create different permutations in the defined array S. KSA will use the secret key and the temporary array T to set the values of the S array's entries as follows:
int i, j=0;
while(i<256){
  j= (j + S[i] + T[i])mod 256; // T is the temporary vector created from the secret key.
  int temp = 0;
  temp = S[i]; //swap S[i] and S[j]
  S[i] = S[j];
  S[j] = temp;
  
  i++;
}

In the above code, the values in the S array are swapped with the i and j indexes produced. At this point, the array S is completely initialized to be used in PRGA as input.

  • After passing through KSA, its output acts as the input for PRGA. It outputs a key based on the state of the array S modified by the KSA algorithm. The code for PRGA is as follows:
int i, j =0;
while(1){
  i = ( i + 1 ) mod 256;
  j = ( j + S[i] ) mod 256;
  int temp = 0;
  temp = S[i]; //swap S[i] and S[j]
  S[i] = S[j];
  S[j] = temp;
  t = ( S[i] + S[j] ) mod 256 ; 
  k = S[t]; // k is the byte generated from S by scrambling entries in a calculated way 
}
  • At this point, the keystream has been generated.

Step 2: XOR the keystream produced with the plain text input

  • The output of KSA, which is the key stream is XORed with the plain text provided by the user to create a cipher text.
  • The cipher text is then sent over the network to the receiver.

Conclusion

RC4 encryption algorithms are easy to use, do not require an exuberant amount of memory to operate, and are implemented on large data streams. However, RC4 encryption does not provide authentication, which can compromise security.

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved