# The RC4 encryption

The RC4 encryption algorithm is a symmetric encryption algorithm.

## Symmetric encryption algorithm

Symmetric encryption (also called private key encryption) refers to an encryption algorithm that uses the same [key](http://baike.baidu.com/view/934.htm) for encryption and decryption . Sometimes called the traditional cryptographic algorithm, the encryption key can be calculated from the decryption key, and the decryption key can also be calculated from the encryption key. In most symmetric algorithms, the encryption key and the decryption key are the same, so this encryption algorithm is also called a secret key algorithm or a single key algorithm. It requires the sender and receiver to agree on a key before communicating securely. The security of the symmetric algorithm depends on the key. Leaking the key means that anyone can decrypt the messages they send or receive, so the confidentiality of the key is very important to the security of communication.

## RC4 encryption algorithm

RC4 algorithm is characterized by simple algorithm, fast running speed, and the key length is variable, the variable range is 1-256 bytes (8-2048 bits), under the premise of technical support today, when the key length is At 128 bits, it is not feasible to search for keys by brute force, so it can be predicted that the key range of RC4 can still resist brute force search key attacks for a long time in the future. In fact, no effective attack method has been found for the 128bit key length RC4 encryption algorithm.

<div id="bkmrk-the-principle-of-rc4">The principle of RC4 algorithm is very simple, including initialization algorithm (KSA) and pseudo-random sub-code generation algorithm (PRGA) two parts. Assume that the length of the S-box is 256 and the key length is Len. </div><div id="bkmrk-"></div><div id="bkmrk-let%27s-take-a-look-at">Let's take a look at the initialization part of the algorithm (in C code):</div><div id="bkmrk--0"></div><div id="bkmrk-among-the%3A">Among the:</div><div id="bkmrk-parameter-1-is-a-256">Parameter 1 is a 256-length char type array, defined as: unsigned char sBox[256];</div><div id="bkmrk-parameter-2-is-the-k">Parameter 2 is the key, and its content can be defined arbitrarily: char key[256];</div><div id="bkmrk-parameter-3-is-the-l">Parameter 3 is the length of the key, Len = strlen(key);</div><div id="bkmrk--1"></div><div id="bkmrk--2"></div>```C
/* Initialization function */ 
void rc4_init(unsigned char *s,unsigned char *key, unsigned long Len)
{
    int i = 0 ,j = 0 ;
     char k[ 256 ]={ 0 };
    unsigned char tmp = 0 ;
     for (i = 0 ;i< 256 ;i++ ) {
        s[i] = i;
        k[i] =key[i% Len];
    }
    for (i= 0 ;i< 256 ;i++ ) {
        j =(j+s[i]+k[i])% 256 ;
        tmp = s[i];
        s[i] = s[j]; // Exchange s[i] and s[j] 
        s[j]= tmp;
    }
}
```

<div id="bkmrk--3"></div><div id="bkmrk-during-the-initializ"><div><span style="vertical-align: inherit;">During the initialization process, the main function of the </span>[<span style="vertical-align: inherit;">key</span>](http://baike.baidu.com/view/934.htm)<span style="vertical-align: inherit;"> is to scramble the S-box, i ensure that each element of the S-box is processed, and j ensures that the scramble of the S-box is random. Different S-boxes can obtain different sub-key sequences after being processed by the </span>[<span style="vertical-align: inherit;">pseudo-random</span>](http://baike.baidu.com/view/3137884.htm)<span style="vertical-align: inherit;"> sub-cipher generation algorithm. Perform an xor operation on the S-box and the plaintext to obtain the ciphertext, and the decryption process is exactly the same.</span></div><div></div><div><span style="vertical-align: inherit;">Let's take a look at the encryption part of the algorithm (in C code):</span></div><div></div><div><span style="vertical-align: inherit;">Among them.</span></div><div><span style="vertical-align: inherit;">Parameter 1 is the disturbed S-box in the rc4\_init function above;</span></div><div><span style="vertical-align: inherit;">Parameter 2 is the data data that needs to be encrypted;</span></div><div><span style="vertical-align: inherit;">Parameter 3 is the length of data.</span></div></div><div id="bkmrk--4"></div>```C
/* Encryption and decryption */ 
void rc4_crypt(unsigned char *s,unsigned char *Data,unsigned long Len)
{
    int i = 0 , j = 0 , t = 0 ;
    unsigned long k = 0 ;
    unsigned char tmp;
     for (k = 0 ;k<Len;k++ )
    {
        i = (i + 1 )% 256 ;
        j =(j+s[i])% 256 ;
        tmp = s[i];
        s[i] = s[j]; // Swap s[x] and s[y] 
        s[j]= tmp;
        t =(s[i]+s[j])% 256 ;
        Data[k] ^= s[t];
    }
}
```

<div id="bkmrk-let%27s-take-a-look-at-0">Let's take a look at the complete program</div><div id="bkmrk--5"></div>```C
// Program start 
#include<stdio.h> 
#include < string .h>
typedef unsigned longULONG;
 
/* Initialization function */ 
void rc4_init(unsigned char *s, unsigned char *key, unsigned long Len)
{
    int i = 0 , j = 0 ;
     char k[ 256 ] = { 0 };
    unsigned char tmp = 0 ;
     for (i = 0 ; i< 256 ; i++ )
    {
        s[i] = i;
        k[i] = key[i% Len];
    }
    for (i = 0 ; i< 256 ; i++ )
    {
        j = (j + s[i] + k[i])% 256 ;
        tmp = s[i];
        s[i] = s[j]; // Swap s[i] and s[j] 
        s[j] = tmp;
    }
}
 
/* Encryption and decryption */ 
void rc4_crypt(unsigned char *s, unsigned char *Data, unsigned long Len)
{
    int i = 0 , j = 0 , t = 0 ;
    unsigned long k = 0 ;
    unsigned char tmp;
     for (k = 0 ; k<Len; k++ )
    {
        i = (i + 1 )% 256 ;
        j = (j + s[i])% 256 ;
        tmp = s[i];
        s[i] = s[j]; // Swap s[x] and s[y] 
        s[j] = tmp;
        t = (s[i] + s[j])% 256 ;
        Data[k] ^= s[t];
    }
}
 
int main()
{
    unsigned char s[ 256 ] = { 0 }, s2[ 256 ] = { 0 }; // S-box 
    char key[ 256 ] = { " justfortest " };
     char pData[ 512 ] = " This is an encryption Data " ;
    unsigned long len = strlen(pData);
     int i;
 
    printf( " pData=%s\n " , pData);
    printf( " key=%s,length=%d\n\n " , key, strlen(key));
    rc4_init(s, (unsigned char *)key, strlen(key)); // Has completed the initialization 
    printf( " Complete the initialization of S[i], as follows: \n\n " );
     for (i = 0 ; i< 256 ; i++ )
    {
        printf( " %02X " , s[i]);
         if (i && (i + 1 )% 16 == 0 )putchar( ' \n ' );
    }
    printf( " \n\n " );
     for (i = 0 ; i< 256 ; i++) // Use s2[i] to temporarily reserve the initialized s[i], it is very important! ! ! 
    {
        s2[i] = s[i];
    }
    printf( " Initialized, now encrypted:\n\n " );
    rc4_crypt(s, (unsigned char *)pData, len); // Encryption 
    printf( " pData=%s\n\n " , pData);
    printf( " Already encrypted, now decrypt:\n\n " );
     // rc4_init(s,(unsignedchar*)key,strlen(key)); // initialize the key 
    rc4_crypt(s2, (unsigned char *)pData, len); // Decrypt 
    printf( " pData=%s\n\n " , pData);
     return  0 ;
}
 
//The program is over
```

<div id="bkmrk--6"></div>#### By Boschko

<div id="bkmrk-my-hack-the-box%3A-htt"><div>- My Hack The Box: [https://www.hackthebox.eu/home/users/profile/37879](https://www.hackthebox.eu/home/users/profile/37879)
- My Website: [https://olivierlaflamme.github.io/](https://olivierlaflamme.github.io/)
- My GitHub: [https://github.com/OlivierLaflamme](https://github.com/OlivierLaflamme)
- My WeChat QR below:

<figure class="graf graf--figure graf--layoutOutsetLeft" id="bkmrk--15">![](https://cdn-images-1.medium.com/max/750/0*Jh0zrZrfyn8WjkDn.png)</figure></div></div>