Encryption principle:
1, through the key scheduling algorithm KSA initialization state vector s (S is a random number generator, called S-box)
2, and then the pseudo-random number generation algorithm Prga to get the key stream keystream
3, the key stream keystream with the plaintext XOR operation obtains ciphertext, decrypts the key stream and the ciphertext XOR
1. KSA
RC4 first uses the key scheduling algorithm (KSA) to initialize and replace byte array s (s-box) of size 256. Use a key when replacing. Its key length generally takes 5~16 bytes, that is, 40~128 bit, can also be longer, usually no more than 256 bits. First initialize the array s with 0~255 and then replace it with the key.
2. Prga
The input key is no longer used after the initialization of the array s is complete. The key stream is generated from s[0]~s[255], for each s[i], according to the current S value, the s[i] with another byte in the S is replaced. When s[255] completes the conversion, the operation continues to execute repeatedly.
The obtained sub-cipher K is used for XOR operation with plaintext, and the decryption process is identical.
RC4 Encryption principle