The Wayback Machine - https://web.archive.org/web/20240909125825/https://www.geeksforgeeks.org/rc4-encryption-algorithm/
Open In App

RC4 Encryption Algorithm

Last Updated : 24 Apr, 2023
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

RC4 is a stream cipher and variable-length key algorithm. This algorithm encrypts one byte at a time (or larger units at a time). A key input is a pseudorandom bit generator that produces a stream 8-bit number that is unpredictable without knowledge of input key, The output of the generator is called key-stream, is combined one byte at a time with the plaintext stream cipher using X-OR operation.

Example:

RC4 Encryption 
10011000 ? 01010000 = 11001000    

RC4 Decryption 
11001000 ? 01010000 = 10011000

Key-Generation Algorithm – A variable-length key from 1 to 256 bytes is used to initialize a 256-byte state vector S, with elements S[0] to S[255]. For encryption and decryption, a byte k is generated from S by selecting one of the 255 entries in a systematic fashion, then the entries in S are permuted again.

Key-Scheduling Algorithm: Initialization: The entries of S are set equal to the values from 0 to 255 in ascending order, a temporary vector T, is created. If the length of the key k is 256 bytes, then k is assigned to T. Otherwise, for a key with length(k-len) bytes, the first k-len elements of T as copied from K, and then K is repeated as many times as necessary to fill T. The idea is illustrated as follow: 

Java




for
    i = 0 to 255 do S[i] = i;
T[i] = K[i mod k - len];


we use T to produce the initial permutation of S. Starting with S[0] to S[255], and for each S[i] algorithm swap it with another byte in S according to a scheme dictated by T[i], but S will still contain values from 0 to 255 : 

C++




int j = 0;
for (int i = 0; i <= 255; i++) {
    j = (j + S[i] + T[i]) % 256;
    swap(S[i], S[j]); // Swap S[i] and S[j]
}


Java




j = 0;
for
    i = 0 to 255 do
    {
        j = (j + S[i] + T[i])mod 256;
        Swap(S[i], S[j]);
    }


Python3




j = 0
for i in range(256):
    j = (j + S[i] + T[i]) % 256
    S[i], S[j] = S[j], S[i]  # Swap S[i] and S[j]


C#




for (int i = 0; i < 256; i++) {
j = (j + S[i] + T[i]) % 256;
int temp = S[i];
S[i] = S[j];
S[j] = temp;
}


Javascript




let j = 0;
for (let i = 0; i <= 255; i++) {
    j = (j + S[i] + T[i]) % 256;
    [S[i], S[j]] = [S[j], S[i]]; // Swap S[i] and S[j]
}


Pseudo random generation algorithm (Stream Generation): Once the vector S is initialized, the input key will not be used. In this step, for each S[i] algorithm swap it with another byte in S according to a scheme dictated by the current configuration of S. After reaching S[255] the process continues, starting from S[0] again 

Java




i, j = 0;
while (true)
    i = (i + 1)mod 256;
j = (j + S[i])mod 256;
Swap(S[i], S[j]);
t = (S[i] + S[j])mod 256;
k = S[t];


Encrypt using X-Or(): 

55

News: In September 2015, Microsoft announced the end of using RC4 in Microsoft edge and internet explorer 11. This video gives a clear example of RC4 algorithm.

https://www.youtube.com/watch?v=NKnZARFAhMk

 Features of the RC4 encryption algorithm:

  1. Symmetric key algorithm: RC4 is a symmetric key encryption algorithm, which means that the same key is used for encryption and decryption.
  2. Stream cipher algorithm: RC4 is a stream cipher algorithm, which means that it encrypts and decrypts data one byte at a time. It generates a key stream of pseudorandom bits that are XORed with the plaintext to produce the ciphertext.
  3. Variable key size: RC4 supports variable key sizes, from 40 bits to 2048 bits, making it flexible for different security requirements.
  4. Fast and efficient: RC4 is a fast and efficient encryption algorithm that is suitable for low-power devices and applications that require high-speed data transmission.
  5. Widely used: RC4 has been widely used in various applications, including wireless networks, secure sockets layer (SSL), virtual private networks (VPN), and file encryption.
  6. Vulnerabilities: RC4 has several vulnerabilities, including a bias in the first few bytes of the keystream, which can be exploited to recover the key. As a result, RC4 is no longer recommended for use in new applications.

Advantages:

  1. Fast and efficient: RC4 is a very fast and efficient encryption algorithm, which makes it suitable for use in applications where speed and efficiency are critical.
  2. Simple to implement: RC4 is a relatively simple algorithm to implement, which means that it can be easily implemented in software or hardware.
  3. Variable key size: RC4 supports variable key sizes, which makes it flexible and adaptable for different security requirements.
  4. Widely used: RC4 has been widely used in various applications, including wireless networks, secure sockets layer (SSL), virtual private networks (VPN), and file encryption.

Disadvantages:

  1. Vulnerabilities: RC4 has several known vulnerabilities that make it unsuitable for new applications. For example, there is a bias in the first few bytes of the keystream, which can be exploited to recover the key.
  2. Security weaknesses: RC4 has some inherent weaknesses in its design, which make it less secure than other encryption algorithms, such as AES or ChaCha20.
  3. Limited key length: The maximum key length for RC4 is 2048 bits, which may not be sufficient for some applications that require stronger encryption.
  4. Not recommended for new applications: Due to its vulnerabilities and weaknesses, RC4 is no longer recommended for use in new applications. Other more secure stream cipher algorithms, such as AES-CTR or ChaCha20, should be used instead.


Previous Article
Next Article

Similar Reads

Implementation of RC4 algorithm
RC4 is a symmetric stream cipher and variable key length algorithm. This symmetric key algorithm is used identically for encryption and decryption such that the data stream is simply XORed with the generated key sequence. The algorithm is serial as it requires successive exchanges of state entries based on the key sequence. The algorithm works in t
15+ min read
Difference between Software Encryption and Hardware Encryption
1. Software Encryption: Software Encryption, as the name suggests, is a process of keeping data safe using the software. In this, the software is generally installed in the host computer that encrypts and decrypts data. It is more cost-effective for smaller companies. In this, the password is the key that one needs to have access to data. It usuall
5 min read
Difference Between RC4 and AES
RC4 is a stream cipher and variable-length key algorithm. The main difference between RC4 and AES is that AES is a block cipher and RC4 is a stream cipher. Symmetric algorithm classes include block encryption and stream encryption. A block cipher encodes plain text in block sizes, whereas a stream cipher encodes bit by bit, simulating the flow of a
4 min read
Twofish Encryption Algorithm
When it comes to data protection, encryption methods act as our buffering agents. One example of an excellent block cipher is the Twofish encryption algorithm. Although it was a competitor of another block cipher in the Advanced Encryption Standard competition and was later succeeded by the latter, it can still be used as a safe approach to protect
6 min read
RC5 Encryption Algorithm
RC5 is a symmetric key block encryption algorithm designed by Ron Rivest in 1994. It is notable for being simple, fast (on account of using only primitive computer operations like XOR, shift, etc.) and consumes less memory. Example: Key : 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00Plain Text : 00000000 00000000Cipher Text : EEDBA521 6D8F4B15RC5
9 min read
Knapsack Encryption Algorithm in Cryptography
Knapsack Encryption Algorithm is the first general public key cryptography algorithm. It was developed by Ralph Merkle and Mertin Hellman in 1978. As it is a Public key cryptography, it needs two different keys. One is the Public key which is used for the Encryption process and the other one is the Private key which is used for the Decryption proce
6 min read
ElGamal Encryption Algorithm
ElGamal Encryption is a public-key cryptosystem. It uses asymmetric key encryption for communicating between two parties and encrypting the message. This cryptosystem is based on the difficulty of finding discrete logarithms in a cyclic group that is even if we know ga and gk, it is extremely difficult to compute gak. In this article, we will get t
6 min read
End to End Encryption (E2EE) in Computer Networks
What is Encryption? Have you ever wondered how our emails, texts, photos, videos are sent over the Internet? Whenever you send anything over the Internet, it passes through a lot of servers, routers, and other devices where anyone can intercept it and get access to your private information. Thus, to tackle such mishap, encryption was introduced. In
2 min read
Strength of Data encryption standard (DES)
Data encryption standard (DES) is a symmetric key block cipher algorithm. The algorithm is based on Feistel network. The algorithm uses a 56-bit key to encrypt data in 64-bit blocks. There are mainly two categories of concerns about the strength of Data encryption standard. They are: Concerns about the particular algorithm used. Concerns about the
2 min read
What is E2EE(End to End Encryption)?
E2EE or End to End Encryption refers to the process in which encryption of data are being done at the end host. It is an implementation of Asymmetric encryption and hence ensures a secure way of data communication. It is the most secure way to communicate privately and securely as data can be read-only by the sender and the receiver. No one else ca
3 min read
Conventional Encryption
Conventional encryption is a cryptographic system that uses the same key used by the sender to encrypt the message and by the receiver to decrypt the message. It was the only type of encryption in use prior to the development of public-key encryption. It is still much preferred of the two types of encryption systems due to its simplicity. It is a r
3 min read
Simplified Data Encryption Standard Key Generation
Simplified Data Encryption Standard (S-DES) is a simple version of the DES Algorithm. It is similar to the DES algorithm but is a smaller algorithm and has fewer parameters than DES. It was made for educational purposes so that understanding DES would become simpler. It is a block cipher that takes a block of plain text and converts it into ciphert
3 min read
Simplified Data Encryption Standard | Set 2
Prerequisite - Simplified Data Encryption Standard | Set 1 Simplified Data Encryption Standard is a simple version of Data Encryption Standard having a 10-bit key and 8-bit plain text. It is much smaller than the DES algorithm as it takes only 8-bit plain text whereas DES takes 64-bit plain text. It was developed for educational purpose so that und
13 min read
Difference Between Data Encryption and Data Compression
Data compression and data encryption are two important methods of protecting and managing data. While both of these processes can help protect your data, they work in different ways. In this article, we’ll discuss the key differences between data encryption and data compression, and explain how each can help you protect your data. What is Data Encr
4 min read
Encryption, Its Algorithms And Its Future
Encryption is very important in today's world, it is the process of transforming plaintext into ciphertext to maintain data security, and it is a crucial tool used in modern cyber security. It secures sensitive data by rendering it unreadable to unauthorized events, making sure of confidentiality, integrity, and authenticity. What is Encryption?Enc
6 min read
What is Data Encryption?
Data Encryption is an important part of preserving data integrity, and confidentiality, its importance cannot be overestimated. Almost the whole thing on the internet has been encrypted at some point. In this article, we will discuss data encryption and its importance. What is Data Encryption?Data Encryption is a method of preserving data confident
9 min read
Why is Encryption Important in Securing Data Transmission Over Networks?
Answer: Encryption is basically important for securing data transmission over Networks, because it makes shows that sensitive information remains private and inaccessible to unauthorized users.Below are the reasons specifying why encryption important in securing data transmission over networks: ConfidentialityEncryption process encodes the data int
2 min read
Encryption vs Hashing vs Salting
Data Integrity and Privacy form the building blocks of cybersecurity. These three core techniques- encryption, hashing, and salting, are considered to be the most important ones when it comes to the security of digital data. This is done to protect data during the process of storage and transmission, but they altogether play different roles and use
5 min read
How Does Wi-Fi Encryption Enhance Network Security?
Answer: Wireless security consists of encryption, authentication, access control, device security, access control, etc. It creates a layer of protection. The process starts by enabling the wireless network encryption method (such as WPA2 or WPA3) to block data transmission. Unauthorized people cannot read the data even if they are intercepted. Acce
2 min read
Difference Between Symmetric and Asymmetric Key Encryption
Symmetric Key Encryption: Encryption is a process to change the form of any message in order to protect it from reading by anyone. In Symmetric-key encryption the message is encrypted by using a key and the same key is used to decrypt the message which makes it easy to use but less secure. It also requires a safe method to transfer the key from one
2 min read
Public Key Encryption
Public key cryptography provides a secure way to exchange information and authenticate users by using pairs of keys. The public key is used for encryption and signature verification, while the private key is used for decryption and signing. When the two parties communicate with each other to transfer the intelligible or sensible message, referred t
8 min read
Encryption vs Digest in Cryptography
Encryption and Digest algorithms are used prominently in cryptography to protect the information which is always in high demand. Both are used as protection for data, however, their roles and capabilities of use are quite varied. Encryption replaces the normal or readable form of information (plaintext) with an unreadable form (ciphertext) to maint
6 min read
Advanced Encryption Standard (AES)
Advanced Encryption Standard (AES) is a specification for the encryption of electronic data established by the U.S. National Institute of Standards and Technology (NIST) in 2001. AES is widely used today as it is much stronger than DES and triple DES despite being harder to implement. In this article, we will cover the AES, the Working of Cipher an
8 min read
What is an Asymmetric Encryption?
Asymmetric encryption, also known as public-key cryptography, is a type of encryption that uses a pair of keys to encrypt and decrypt data. The pair of keys includes a public key, which can be shared with anyone, and a private key, which is kept secret by the owner. What is an Asymmetric Encryption?In asymmetric encryption, the sender uses the reci
8 min read
Difference Between Encryption and Encoding
Encryption and Encoding are terms often interchanged and used incorrectly, but they have distinct differences. Anyone dealing with communication networks or data security needs to be aware of these distinctions. In this article, we will learn the difference between the two terms, encryption and encoding. What is Encryption?It is a process used to c
4 min read
Difference between Encryption and Decryption
Encryption is the process of converting a normal message (plain text) into a meaningless message (ciphertext). Decryption is the process of converting a meaningless message (ciphertext) into its original form (plaintext). The major distinction between secret writing and associated secret writing is the conversion of a message into an unintelligible
4 min read
Difference between Hashing and Encryption
In the field of cybersecurity, encryption, and hashing are the two most basic methods utilized for the protection and integrity of data. Even though all these terms may be utilized to mean the same thing, they tend to work differently and for different purposes. As evidenced in encryption, data cannot be read in its encrypted form, hence making the
5 min read
Difference Between AES and RSA Encryption
In cryptography, this is the process of changing or transforming plain text or information into a cipher text or a text that the receiver, who is the person to whom the information is intended, can only decode. There are several algorithms in data encryption such as AES and RSA. AES represents a symmetric key algorithm whose processes depend on the
6 min read
Difference between Encryption and Cryptography
Encryption and Cryptography are the two terms that are generally used in Cyber Security. Encryption is the process in which the data should be securely locked which means only authorized users access the data while cryptography is the process of securing information or data by using cryptographic algorithms so that any hacker or unauthorized person
4 min read
Difference between Tokenization and Encryption
Tokenization is vital in data security to obscure information that should not be viewed and encryption is a significant strategy in security used to secure delicate data including credit card numbers, PII, and other sensitive information. The two techniques change the form of the information stored in a dataset to ensure that only authorized person
7 min read