Crypt - Lecture 10 (Stream Ciphers - RC4)
Crypt - Lecture 10 (Stream Ciphers - RC4)
Introduction of Cryptography
(Encryption Basics & Classical Ciphers)
([email protected])
Complete
Hill
Playfair
cipher
Modern Stream Ciphers
RC4
(Rivest Cipher 4)
Stream ciphers
▪ Initialization:
▪ Key Mixing:
Using the key, mix the values in S by swapping
elements, creating a pseudo-random permutation of 0–
255.
How RC4 Works
2- Pseudo-Random Generation Algorithm (PRGA):
Uses the state array to generate a stream of pseudo-
random bytes (key stream) which are XORed with
plaintext bytes for encryption.
▪ Index Generation:
Two pointers, i and j, are used to generate the pseudo-
random stream.
▪ Swapping:
Values in the S array are continually swapped.
▪ Key Stream:
Output bytes are selected by using the S[i] + S[j] values
as indices in the array.
⦿ A variable-length key of from 1 to 256 bytes is used to
initialize a 256-byte state array S, with elements S[0], S[1],
……, S[255].
Ste
p Initialisation of S
1
……
……
S[ 255 ] = 255
Ste Initialisation of S Code
p
//********************************************************
1 S[ 0 ] = 0
S[ 1 ] = 1
for i = 0 to 255 do ……
……
S[ i ] = i ……
//********************************************************
S[ 255 ] = 255
RC4
key k
Given a of length m
bytes
Ste
p
Initial Permutation of S
2 // This involves starting with S[0] and going through S[255], and for each
S[i], swapping S[i] with another byte in S as obtained by some mechanism
m = Size of key
j=0 >0
256 = Size of state
for i = 0 to 255 do array S
Ste
p RC4 Encryption
3 ⦿ Encryption continues shuffling array values
⦿ Sum of shuffled pair selects "stream key" value t
⦿ XOR with next byte of message to en/decrypt
i=j=0
for each message byte Mb
A i = ( i + 1 ) mod 256 Increment in i
Calculation of j
B
j = ( j + S[ i ] ) mod 256
C Final Permutation of S
swap( S[ i ] , S[ j ] )
D t = ( S[ i ] + S[ j ] ) mod 256 Calculation of t
Initial permutation of S
m = 2 = Size of Key
j=0
for i = 0 to 3 do
j = ( j + S[ i ] + k[ i mod 2 ] ) mod
4 swap ( S[ i ], S[ j ] )
4 = Size of state array S
RC4 Example
Initial permutation of S
j=0 10001001 = 137 in
for i = 0 to 3 do Decimal
j = ( j + S[ i ] + k[ i mod 2] ) mod
4 k[0] 10001001 01010001 k[1]
swap ( S[ i ], S[ j ] )
S[0] S[1] S[2] S[3]
00000000 00000001 00000010 00000011
Initial Permutation
of S
for i = 0 to 255 do
S[i]=i
Given a key k of length m bytes
2 Initial permutation of S
j=0
for i = 0 to 255 do
j = ( j + S[ i ] + k[ i mod m ] ) mod 256
swap ( S[ i ], S[ j ] )
3 RC4 Encryption
i=j=0
for each message byte Mi
i = ( i + 1 ) mod 256 , j = ( j + S[ i ] ) mod 256
Final permutation
swap( S[ i ], S[ j ] )
of S
t = ( S[ i ] + S[ j ] ) mod 256
k[0] k[1]
Given a key k of length 2
bytes 10001001 01010001
Initialisation of S
for i = 0 to 3 do S[0] S[1] S[2] S[3]
1
00000000 00000001 00000010 00000011
S[i] = i
2 Initial Permutation of S
i = ( i + 1 ) mod 4 = ( 0 + 1 ) mod 4 = 1
j = ( j + S [ i ] ) mod 4 = ( 0 + S [ 1 ] ) mod 4 = ( 0 + 2 ) mod
4 = 2 swap ( S[ i ], S[ j ] ) = swap ( S[1], S[2] )
S[0] S[1] S[2] S[3]
00000000 00000011 00000010 00000001
i = (i + 1) mod 2 10010001
11100101
j = (j + S[i]) mod 2
swap(S[i], S[j])
t = (S[i] + S[j]) mod 2
Ci = Mi XOR S[t]
RC4 Encryption Example
• Historical Usage: RC4 was widely used in SSL/TLS and WPA due
to its simplicity and speed.