1. Introduction | 简介
RC4 is a widely used stream cipher designed by Ron Rivest in 1987. It operates by generating a pseudorandom keystream that is XORed with plaintext to produce ciphertext. The cipher is known for its simplicity and speed, making it popular in applications such as WEP (Wired Equivalent Privacy) and TLS (Transport Layer Security).
RC4是由Ron Rivest于1987年设计的流密码,通过生成伪随机密钥流(keystream),并与明文进行XOR转换,以产生密文。该密码以其简单性和速度而闻名,被广泛应用于WEP(有线等效加密)和TLS(传输层安全协议)等。
2. RC4 Key-Scheduling Algorithm (KSA) | 密钥安排算法
The first step in RC4 is the Key-Scheduling Algorithm (KSA), which initializes the state vector S based on the key K.
RC4算法的第一步是密钥安排算法(KSA),该步骤基于密钥K初始化状态向量S。
KSA Process | KSA过程
Initialize state vector S and temporary vector T.
初始化S状态向量和T临时向量S is initialized such that
S[i] = i
fori = 0
to7
T is initialized as a repetition of the key K = [1 2 3 6]
Example | 例子:
S = [0 1 2 3 4 5 6 7] T = [1 2 3 6 1 2 3 6]
Permute S using T.
用T向量对S进行排列j = 0; for i = 0 to 7 do j = (j + S[i] + T[i]) mod 8 Swap(S[i], S[j]); end
Example Execution | 执行示例:
For i = 0: j = (0 + 0 + 1) mod 8 = 1, Swap(S[0], S[1]); S = [1 0 2 3 4 5 6 7] For i = 1: j = 3, Swap(S[1], S[3]) S = [1 3 2 0 4 5 6 7] For i = 2: j = 0, Swap(S[2], S[0]) S = [2 3 1 0 4 5 6 7] For i = 3: j = 6, Swap(S[3], S[6]) S = [2 3 1 6 4 5 0 7] For i = 4: j = 3, Swap(S[4], S[3]) S = [2 3 1 4 6 5 0 7] For i = 5: j = 2, Swap(S[5], S[2]) S = [2 3 5 4 6 1 0 7] For i = 6: j = 5, Swap(S[6], S[5]) S = [2 3 5 4 6 0 1 7] For i = 7: j = 2, Swap(S[7], S[2]) S = [2 3 7 4 6 0 1 5]
After all iterations, the final permutation of
S
is:S = [2 3 7 4 6 0 1 5]
3. Pseudo-Random Generation Algorithm (PRGA) | 伪随机生成算法
Once S has been initialized, PRGA is used to generate the keystream that will be XORed with plaintext.
一旦S初始化完成,便可使用PRGA生成密钥流,该流将与明文进行XOR运算。
PRGA Process | PRGA过程
Iterate indefinitely, updating S and generating keystream k.
无限迭代,更新S并生成密钥流ki, 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]; }
Example Execution | 执行示例:
i = (0 + 1) mod 8 = 1 j = (0 + S[1]) mod 8 = 3 Swap(S[1], S[3]) S = [2 4 7 3 6 0 1 5] t = (S[1] + S[3]) mod 8 = 7 k = S[7] = 5
Encrypt plaintext using XOR operation.
用XOR完成加密C[i] = P[i] XOR k[i]
Example | 例子:
Given plaintext
P = [1 2 2 2]
First keystream byte:
k = 5
XOR computation:
5 XOR 1 = 101 XOR 001 = 100 = 4
Repeating the process for remaining plaintext bits:
k = 1, 1 XOR 2 = 001 XOR 010 = 011 = 3 k = 0, 0 XOR 2 = 000 XOR 010 = 010 = 2 k = 1, 1 XOR 2 = 001 XOR 010 = 011 = 3
Final ciphertext:
C = [4 3 2 3]
4. Conclusion | 结论
RC4 is a simple yet effective stream cipher that relies on key scheduling and pseudo-random generation to encrypt data. Despite its historical importance, weaknesses in its keystream generation have led to its deprecation in modern secure communications.
RC4是一种简单但高效的流密码,它通过密钥安排和伪随机生成来加密数据。尽管RC4在历史上曾起到重要作用,但由于其密钥流的弱点,它已不再适用于现代安全通信。