2 The RC4 Algorithm
The RC4 algorithm employs a variable-length key, K, ranging from 8 to 2048 bits and a 256-byte state vector, S. The algorithm needs to be initialised as described in algorithm 1 before encryption/decryption can take place.
Algorithm 1 RC4 initialization
{Initialization of S}
for i =0 to 255 do
Si<-i
Ti<-K(i mod 2power8)base
end for
{Initial permutation of S}
j <-0
for i=0 to 255 do
j<-(j+Si+Ti)mod 2power8
Si<-Sj,Sj<-Si {Swap Si and Sj}
end for
Each iteration of the stream generator results in a single byte value, k, which is the used to perform the XOR operation with the cipherstream or plaintext stream for decryption or encryption respectively.
Algorithm 2 RC4 Stream Generation
i,j<-0,0
loop
i<-(i+1)mod 2power8
j<-(j+Si)mod 2power8
Si<-Sj,Sj<-Si {Swap Si and Sj}
t <-(Si+Sj)mod 2power8
k<-St
end loop
Exercises
Exercise 1: Implement the RC4 algorithm in the programming language of your choice.A Sample data
You can use the following information to test your implementation.
Key: abcdefghijklmnopqrst
Plaintext 0x49 0x54 0x53
RC4 stream 0x39 0xE8 0x32