RC4由Ras Rivest于1987年由RSA Security设计。当它被正式称为“Rivest Cipher 4”时,RC首字母缩略词被理解为代表“Ron’s Code”。
RC4最初是一个商业秘密,但是在1994年9月,它的描述被匿名地张贴在Cypherpunks邮件列表中。它很快被发布在sci.crypt新闻组,并从那里到互联网上的许多站点。泄漏的代码被证实是真实的,因为它的输出被发现与使用许可的RC4的专有软件相匹配。因为算法是已知的,它不再是商业秘密。名称“RC4”是商标,因此RC4通常被称为“ARCFOUR”或“ARC4”(意为被称为RC4),以避免商标问题。 RSA Security从未正式发布算法;然而,Rivest与英文维基百科的文章有关。在RC4的自己的课程笔记中。RC4已经成为一些常用的加密协议和标准的一部分,包括无线网卡和TLS的WEP和WPA。
RC4在如此广泛的应用领域取得成功的主要因素是速度和简单性:软件和硬件的高效实施非常容易开发。
密钥调度算法用于初始化数组S盒中的置换。
“keylength”被定义为密钥中的字节数,并且可以在1≤keylength≤256的范围内,通常在5和16之间,对应于40到128位的密钥长度。
首先,数组“S”被初始化为i。 然后以类似于PRGA的方式对S进行256次迭代处理,同时也以密钥的字节为单位进行混合。
伪代码如下:
for i from 0 to 255
S[i] := i
endfor
j := 0
for i from 0 to 255
j := (j + S[i] + key[i mod keylength]) mod 256
swap values of S[i] and S[j]
endfor
对于需要的迭代次数,PRGA修改状态并输出一个字节的密钥流。
在每次迭代中,PRGA递增i,将i指向的S的值与j相加,交换S [i]和S [j]的值,然后在S [i] + S [j](模256)。
每256个迭代,S的每个元素至少与另一个元素交换一次。
伪代码如下:
i := 0
j := 0
while GeneratingOutput:
i := (i + 1) mod 256
j := (j + S[i]) mod 256
swap values of S[i] and S[j]
K := S[(S[i] + S[j]) mod 256]
output K
endwhile
package Practice;
public class RC4 {
public static void main(String[] args) {
RC4 rc4 = new RC4();
String plaintext = "helloworld";
String key = "key";
String ciphertext = rc4.encrypt(plaintext, key);
String decryptText = rc4.encrypt(ciphertext, key);
System.out.print(
"明文为:" + plaintext + "\n" + "密钥为:" + key + "\n\n" + "密文为:" + ciphertext + "\n" + "解密为:" + decryptText);
}
// 1 加密
public String encrypt(final String plaintext, final String key) {
Integer[] S = new Integer[256]; // S盒
Character[] keySchedul = new Character[plaintext.length()]; // 生成的密钥流
StringBuffer ciphertext = new StringBuffer();
ksa(S, key);
rpga(S, keySchedul, plaintext.length());
for (int i = 0; i < plaintext.length(); ++i) {
ciphertext.append((char) (plaintext.charAt(i) ^ keySchedul[i]));
}
return ciphertext.toString();
}
// 1.1 KSA--密钥调度算法--利用key来对S盒做一个置换,也就是对S盒重新排列
public void ksa(Integer[] s, String key) {
for (int i = 0; i < 256; ++i) {
s[i] = i;
}
int j = 0;
for (int i = 0; i < 256; ++i) {
j = (j + s[i] + key.charAt(i % key.length())) % 256;
swap(s, i, j);
}
}
// 1.2 RPGA--伪随机生成算法--利用上面重新排列的S盒来产生任意长度的密钥流
public void rpga(Integer[] s, Character[] keySchedul, int plaintextLength) {
int i = 0, j = 0;
for (int k = 0; k < plaintextLength; ++k) {
i = (i + 1) % 256;
j = (j + s[i]) % 256;
swap(s, i, j);
keySchedul[k] = (char) (s[(s[i] + s[j]) % 256]).intValue();
}
}
// 1.3 置换
public void swap(Integer[] s, int i, int j) {
Integer mTemp = s[i];
s[i] = s[j];
s[j] = mTemp;
}
}
unsigned char S[256];
unsigned int i, j;
void swap(unsigned char *s, unsigned int i, unsigned int j) {
unsigned char temp = s[i];
s[i] = s[j];
s[j] = temp;
}
/* KSA */
void rc4_init(unsigned char *key, unsigned int key_length) {
for (i = 0; i < 256; i++)
S[i] = i;
for (i = j = 0; i < 256; i++) {
j = (j + key[i % key_length] + S[i]) & 255;
swap(S, i, j);
}
i = j = 0;
}
/* PRGA */
unsigned char rc4_output() {
i = (i + 1) & 255;
j = (j + S[i]) & 255;
swap(S, i, j);
return S[(S[i] + S[j]) & 255];
}
#include
int main() {
int k, output_length;
unsigned char key[] = "Secret"; // key hardcoded to "Secret"
output_length = 10; // number of bytes of output desired
rc4_init(key, 6); // length of key is 6 in this case
k = 0;
while (k < output_length) {
printf("%c", rc4_output());
k++;
}
}
明文为:helloworld
密钥为:key
密文为:c XKø8$/
解密为:helloworld