因为是一开始学逆向时候学的,所以没有什么完整的笔记,就纯纯记录一个脚本:
特点
RC4最大的特点就是将密文顺着跑,就能得到明文,所以一般推荐直接照抄加密。
脚本
未魔改的RC4
cpp:
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
| #include <iostream> #include <vector> using namespace std; void RC4_init(vector<int>& s, vector<int>& t, string key) { for (int i = 0; i < s.size(); i++) { s[i] = i; t[i] = key[i % key.size()]; } }
void RC4_exchange(vector<int>& s, vector<int>& t) { int j = 0; for (int i = 0; i < s.size(); i++) { j = (j + s[i] + t[i]) % s.size(); swap(s[i], s[j]); } }
void RC4_crypt(vector<int>& s, string& m) { int i = 0, j = 0, t = 0; unsigned long k = 0; char temp;
for (k = 0; k < m.size(); k++) { i = (i + 1) % s.size(); j = (j + s[i]) % s.size(); temp = s[i]; s[i] = s[j]; s[j] = temp; t = (s[i] + s[j]) % s.size(); m[k] ^= s[t]; } }
void print(vector<int>& s) { for (int i = 0; i < s.size(); i++) { cout << s[i] << " "; } cout << endl; }
|
python脚本:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| def init_box(key): s_box = list(range(256)) 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] return s_box def rc_4(plain,box): 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((s ^ k)&0xff) return res
|
2023.2.2更新:rc4脚本完善,错别字更正(明文写成明晚可还行)