获取中...

-

Just a minute...

在密码学中,RC4(来自Rivest Cipher 4的缩写)是一种流加密算法,密钥长度可变。它加解密使用相同的密钥,因此也属于对称加密算法。RC4是有线等效加密(WEP)中采用的加密算法,是TLS可采用算法之一。

原理

初始化秘钥:根据输入的秘钥key,使用密钥调度算法(KSA)生成一个256字节的sbox。

再通过伪随机数生成算法(PRGA)得到密钥流(keystream)

加密:密钥流与明文进行异或运算得到密文

解密:秘文与密钥流进行异或运算得到明文

Python

加密:

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
import base64
def rc4_main(key = "init_key", message = "init_message"):
# print("RC4加密主函数")
s_box = rc4_init_sbox(key)
crypt = str(rc4_excrypt(message, s_box))
return crypt
def rc4_init_sbox(key):
s_box = list(range(256)) # 我这里没管秘钥小于256的情况,小于256不断重复填充即可
# print("原来的 s 盒:%s" % s_box)
j = 0
for i in range(256):
j = (j + s_box[i] + ord(key[i % len(key)])) % 256
s_box[i], s_box[j] = s_box[j], s_box[i]
# print("混乱后的 s 盒:%s"% s_box)
return s_box
def rc4_excrypt(plain, box):
# print("调用加密程序成功。")
res = []
i = j = 0
for s in plain:
i = (i + 1) % 256
j = (j + box[i]) % 256
box[i], box[j] = box[j], box[i]
t = (box[i] + box[j]) % 256
k = box[t]
res.append(chr(ord(s) ^ k))
# print("res用于加密字符串,加密后是:%res" %res)
cipher = "".join(res)
# print("加密后的字符串是:%s" %cipher)
# print("加密后的输出(经过编码):")
#print(str(base64.b64encode(cipher.encode('utf-8')), 'utf-8'))
return (str(base64.b64encode(cipher.encode('utf-8')), 'utf-8'))
#rc4_main("123456sh","123456sh")

解密:

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
import base64
def rc4_main(key = "init_key", message = "init_message"):
# print("RC4解密主函数调用成功")
s_box = rc4_init_sbox(key)
crypt = rc4_excrypt(message, s_box)
return crypt
def rc4_init_sbox(key):
s_box = list(range(256)) # 我这里没管秘钥小于256的情况,小于256不断重复填充即可
# print("原来的 s 盒:%s" % s_box)
j = 0
for i in range(256):
j = (j + s_box[i] + ord(key[i % len(key)])) % 256
s_box[i], s_box[j] = s_box[j], s_box[i]
# print("混乱后的 s 盒:%s"% s_box)
return s_box
def rc4_excrypt(plain, box):
# print("调用解密程序成功。")
plain = base64.b64decode(plain.encode('utf-8'))
plain = bytes.decode(plain)
res = []
i = j = 0
for s in plain:
i = (i + 1) % 256
j = (j + box[i]) % 256
box[i], box[j] = box[j], box[i]
t = (box[i] + box[j]) % 256
k = box[t]
res.append(chr(ord(s) ^ k))
# print("res用于解密字符串,解密后是:%res" %res)
cipher = "".join(res)
# print("解密后的字符串是:%s" %cipher)
# print("解密后的输出(没经过任何编码):")
return cipher
# rc4_main("123456sh", "ABHCum92PMOXwqI=")

C

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
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

void swap(unsigned char *a, unsigned char *b) {
unsigned char t = *a;
*a = *b;
*b = t;
}

void rc4KSA(unsigned char *s, const unsigned char *key, int len) {
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 + i % len)) % 256;
swap(s + i, s + j);
}
}

void rc4PRGA(unsigned char *s, char *data, size_t len) {
int i = 0;
int j = 0;
for ( size_t idx = 0; idx < len; idx++ ) {
i = (i + 1) % 256;
j = (j + s[i]) % 256;
swap(s + i, s + j);
unsigned char k = s[(s[i] + s[j]) % 256];
data[idx] ^= k; // k是伪随机数,和明文进行XOR进行加密
}
}

int main(int argc, const char * argv[]) {
unsigned char s[256]; // S-box
unsigned char key[128]; // key的长度1到256字节,本例子使用128字节
arc4random_buf((unsigned char *)key, sizeof(key)); // 用伪随机数算法生成key
char data[1000] = "这里是要机密的数据";

// 加密
rc4KSA(s, key, sizeof(key));
rc4PRGA(s, data, strlen(data));

// 解密
rc4KSA(s, key, sizeof(key));
rc4PRGA(s, data, strlen(data));
return 0;
}
相关文章
评论
分享
  • base家族C语言实现

    base家族怕了怕了…. 基本原理https://bbs.pediy.com/thread-251117.htmhttps://bbs.pediy.com/thread-251248.htm base64加密: 1234567891...

    base家族C语言实现
  • 玩转RSA

    RSA是一种既能用于数据加密也能用于数字签名的算法,是一种非对称加密算法,由公钥(n/e),私钥(n/d),明文m和密文c组成 算法原理①选两个素数p和q(为了安全性,两数的长度一样)②计算n=pq(n是模)③计算欧拉函数(Eul...

    玩转RSA
  • 一个有趣的a+b

    凯撒加密🤪 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859...

    一个有趣的a+b
Please check the parameter of comment in config.yml of hexo-theme-Annie!