在密码学中,RC4(来自Rivest Cipher 4的缩写)是一种流加密算法,密钥长度可变。它加解密使用相同的密钥,因此也属于对称加密算法。RC4是有线等效加密(WEP)中采用的加密算法,也曾经是TLS可采用的算法之一。该算法是一个可变密钥长度、面向字节操作的流密码。该算法以随机置换作为基础,其密码周期很可能大于10100,且该算法的运行速度很快。(来自百度百科)
RC4密钥生成步骤主要分成两部分初始化算法(KSA)和伪随机子密码生成算法(PRGA)两大部分。先放上图助于理解:
初始化算法:
初始化状态向量S(256个字节,用来作为密钥流生成的种子1):
给向量S256字节一一赋值,从0,1,2,3……255。1
2
3
4
5int s[256] = {0};
for(int i = 0; i < 256; i++)
{
s[i] = i;
}
初始密钥(256字节,不满循环填满):
这个值是用户自定义的,长度任意,不满256字节循环填满,非明文,是已知的1
2
3
4
5
6
7char *key = "aaaaaa";
int len = strlen(key);
unsigned char k[256] = {0};
for(int i = 0;i < 256; i++)
{
k[i] = key[i/len];
}
对状态向量S进行置换操作(打乱S_box):
规则如下:1
2
3
4
5
6
7
8
9int j = 0;
char temp;
for(int i = 0;i < 256; i++)
{
j = (j + s[i] + k[i])%256;
temp = s[i];
s[i] = s[j];
s[j] = temp;
}
伪随机子密码生成算法
加密:明文字节数为:datalength1
2
3
4
5
6
7
8
9
10int i = 0;
int j = 0;
while(datalength--)
{
i = (i + 1)%256;
j = (j + s[i])%256;
swap(s[i] , s[j]);
t = (s[i] + s[j])%256;
data[i] = data[i] ^ s[t];
}
解密:明文异或一次得到密文,再异或一次得到原来的明文
参考百度百科完整代码:1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78//程序开始
typedef unsigned longULONG;
/*初始化函数*/
void rc4_init(unsigned char*s, unsigned char*key, unsigned long Len)
{
int i = 0, j = 0;
char k[256] = { 0 };
unsigned char tmp = 0;
for (i = 0; i<256; i++)
{
s[i] = i;
k[i] = key[i%Len];
}
for (i = 0; i<256; i++)
{
j = (j + s[i] + k[i]) % 256;
tmp = s[i];
s[i] = s[j];//交换s[i]和s[j]
s[j] = tmp;
}
}
/*加解密*/
void rc4_crypt(unsigned char*s, unsigned char*Data, unsigned long Len)
{
int i = 0, j = 0, t = 0;
unsigned long k = 0;
unsigned char tmp;
for (k = 0; k<Len; k++)
{
i = (i + 1) % 256;
j = (j + s[i]) % 256;
tmp = s[i];
s[i] = s[j];//交换s[x]和s[y]
s[j] = tmp;
t = (s[i] + s[j]) % 256;
Data[k] ^= s[t];
}
}
int main()
{
unsigned char s[256] = { 0 }, s2[256] = { 0 };//S-box
char key[256] = { "justfortest" };
char pData[512] = {0};
unsigned long len = 0;
int i;
scanf("%s",&pData);
len = strlen(pData);
printf("pData=%s\n", pData);
printf("key=%s,length=%d\n\n", key, strlen(key));
rc4_init(s, (unsigned char*)key, strlen(key));//已经完成了初始化
printf("完成对S[i]的初始化,如下:\n\n");
for (i = 0; i<256; i++)
{
printf("%02X", s[i]);
if (i && (i + 1) % 16 == 0)putchar('\n');
}
printf("\n\n");
for (i = 0; i<256; i++)//用s2[i]暂时保留经过初始化的s[i],很重要的!!!
{
s2[i] = s[i];
}
printf("已经初始化,现在加密:\n\n");
rc4_crypt(s, (unsigned char*)pData, len);//加密
printf("pData=%s\n\n", pData);
printf("已经加密,现在解密:\n\n");
//rc4_init(s,(unsignedchar*)key,strlen(key));//初始化密钥
rc4_crypt(s2, (unsigned char*)pData, len);//解密
printf("pData=%s\n\n", pData);
return 0;
}
//程序完
参考文章:https://www.cnblogs.com/gambler/p/9075415.html