0% found this document useful (0 votes)
225 views9 pages

RC4 Encryption Algorithm

RC4 is a stream cipher that encrypts data one byte at a time using a variable-length key and a pseudorandom bit generator to produce a keystream. The algorithm includes a Key Scheduling Algorithm (KSA) for initializing the state vector and a Pseudo-Random Generation Algorithm (PRGA) to generate the keystream, which is then XORed with plaintext to produce ciphertext. Despite its speed and flexibility, RC4 has known vulnerabilities and is no longer recommended for new applications.

Uploaded by

Krisha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
225 views9 pages

RC4 Encryption Algorithm

RC4 is a stream cipher that encrypts data one byte at a time using a variable-length key and a pseudorandom bit generator to produce a keystream. The algorithm includes a Key Scheduling Algorithm (KSA) for initializing the state vector and a Pseudo-Random Generation Algorithm (PRGA) to generate the keystream, which is then XORed with plaintext to produce ciphertext. Despite its speed and flexibility, RC4 has known vulnerabilities and is no longer recommended for new applications.

Uploaded by

Krisha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

RC4 Encryption Algorithm

Rivest Cipher 4 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 the input key, The output of the generator is called key-
stream, and 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 8 bytes is used to initialize a 8-byte
state vector S, with elements S[0] to S[7]. For encryption and
decryption, a byte k is generated from S by selecting one of the 7
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 7 in ascending
order, and a temporary vector T, is created. If the length of the key k
is 8 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 follows:
// Initialize S with values from 0 to 8 as given in question
for i = 0 to 7 do
S[i] = i;
T[i] = K[i mod keylen];

We use T to produce the initial permutation of S. Starting with S[0] to


S[7], 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 7 :
int j = 0;
for (int i = 0; i <= 7; i++) {
j = (j + S[i] + T[i]) % 8;
swap(S[i], S[j]); // 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[7] the process continues, starting from S[0] again
flag=0,i=0, j = 0;
while (true) {
i = (i + 1) mod 8;
j = (j + S[i]) mod 8;
swap(S[i], S[j]);
t = (S[i] + S[j]) mod 8;
k = S[t];
cipher = k ⊕ plaintext
flag++
}
Encrypt Using X-Or():

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.
Solve the following using RC4 stream cipher
algorithm. Assume the state vector is 8 bits. Key: [1
0 0 2] Plain Text: [6 1 5 4]

Given
• State size N=8N=8N=8.
• Initial S=[0,1,2,3,4,5,6,7]S =
[0,1,2,3,4,5,6,7]S=[0,1,2,3,4,5,6,7].
• Key (bytes): K=[1,0,0,2]K=[1,0,0,2]K=[1,0,0,2]. Key repeated to
length 8: Kexp=[1,0,0,2,1,0,0,2]K_{exp}=[1,0,0,2,1,0,0,2]Kexp
=[1,0,0,2,1,0,0,2].
• Plaintext (numbers): P=[6,1,5,4]P=[6,1,5,4]P=[6,1,5,4]. (We’ll
XOR numeric bytes.)

1) KSA (Key Scheduling Algorithm)


Start: S = [0,1,2,3,4,5,6,7], j = 0.
We iterate i = 0..7:
For each step: j = (j + S[i] + Kexp[i]) mod 8, then swap S[i] and S[j].
Step-by-step:
1. i=0, K=1: j=(0+S[0]+1)%8=(0+0+1)=1 → swap S[0],S[1]
S → [1,0,2,3,4,5,6,7]
2. i=1, K=0: j=(1+S[1]+0)%8=(1+0)=1 → swap S[1],S[1] (no change)
S → [1,0,2,3,4,5,6,7]
3. i=2, K=0: j=(1+S[2]+0)%8=(1+2)=3 → swap S[2],S[3]
S → [1,0,3,2,4,5,6,7]
4. i=3, K=2: j=(3+S[3]+2)%8=(3+2+2)=7 → swap S[3],S[7]
S → [1,0,3,7,4,5,6,2]
5. i=4, K=1: j=(7+S[4]+1)%8=(7+4+1)=12%8=4 → swap S[4],S[4]
(no change)
S → [1,0,3,7,4,5,6,2]
6. i=5, K=0: j=(4+S[5]+0)%8=(4+5)=9%8=1 → swap S[5],S[1]
S → [1,5,3,7,4,0,6,2]
7. i=6, K=0: j=(1+S[6]+0)%8=(1+6)=7 → swap S[6],S[7]
S → [1,5,3,7,4,0,2,6]
8. i=7, K=2: j=(7+S[7]+2)%8=(7+6+2)=15%8=7 → swap S[7],S[7]
(no change)
Final S after KSA: S = [1,5,3,7,4,0,2,6]

2) PRGA (Pseudo-Rand Gen Algorithm) — produce 4 keystream


bytes
Initialize i = 0, j = 0. For each output byte:
• i = (i+1) mod 8
• j = (j + S[i]) mod 8
• swap S[i], S[j]
• t = (S[i] + S[j]) mod 8
• keystream byte = S[t]
We need 4 bytes:
1. Round 1:
i=1
j = (0 + S[1]) %8 = (0 + 5) = 5
swap S[1],S[5] → S becomes [1,0,3,7,4,5,2,6]
t = (S[1] + S[5]) %8 = (0 + 5) = 5
keystream[1] = S[5] = 5
2. Round 2:
i=2
j = (5 + S[2])%8 = (5 + 3) = 8%8 = 0
swap S[2],S[0] → S becomes [3,0,1,7,4,5,2,6]
t = (S[2] + S[0])%8 = (1 + 3) = 4
keystream[2] = S[4] = 4
3. Round 3:
i=3
j = (0 + S[3])%8 = (0 + 7) = 7
swap S[3],S[7] → S becomes [3,0,1,6,4,5,2,7]
t = (S[3] + S[7])%8 = (6 + 7) = 13%8 = 5
keystream[3] = S[5] = 5
4. Round 4:
i=4
j = (7 + S[4])%8 = (7 + 4) = 11%8 = 3
swap S[4],S[3] → S becomes [3,0,1,4,6,5,2,7]
t = (S[4] + S[3])%8 = (6 + 4) = 10%8 = 2
keystream[4] = S[2] = 1
So the generated keystream sequence (numbers) is: K = [5, 4, 5, 1]
(Notice we got the same keystream in each step: 5,4,5,1.)

3) XOR keystream with plaintext


Plaintext P=[6,1,5,4]P = [6,1,5,4]P=[6,1,5,4]
Keystream K=[5,4,5,1]K = [5,4,5,1]K=[5,4,5,1]
Compute ciphertext bytewise as C[i] = P[i] XOR K[i] (bitwise XOR):
• C0 = 6 XOR 5 = 3
• C1 = 1 XOR 4 = 5
• C2 = 5 XOR 5 = 0
• C3 = 4 XOR 1 = 5
So Ciphertext (numbers) = [3, 5, 0, 5]
If you want them as letters using A=0 → Z=25 mapping:
3 → D, 5 → F, 0 → A, 5 → F
So Ciphertext (letters) = D F A F

Final answer (concise)


• Final state after KSA: S = [1,5,3,7,4,0,2,6]
• Keystream (4 bytes): 5, 4, 5, 1
• Plaintext: 6, 1, 5, 4
• Ciphertext (numbers): 3, 5, 0, 5
• Ciphertext (letters, A=0): D F A F

You might also like