-
Notifications
You must be signed in to change notification settings - Fork 2
/
rc4.lua
138 lines (114 loc) · 2.45 KB
/
rc4.lua
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
-- RC4
-- http://en.wikipedia.org/wiki/RC4
function KSA(key)
local key_len = string.len(key)
local S = {}
local key_byte = {}
for i = 0, 255 do
S[i] = i
end
for i = 1, key_len do
key_byte[i-1] = string.byte(key, i, i)
end
local j = 0
for i = 0, 255 do
j = (j + S[i] + key_byte[i % key_len]) % 256
S[i], S[j] = S[j], S[i]
end
return S
end
function PRGA(S, text_len)
local i = 0
local j = 0
local K = {}
for n = 1, text_len do
i = (i + 1) % 256
j = (j + S[i]) % 256
S[i], S[j] = S[j], S[i]
K[n] = S[(S[i] + S[j]) % 256]
end
return K
end
function RC4(key, text)
local text_len = string.len(text)
local S = KSA(key)
local K = PRGA(S, text_len)
return output(K, text)
end
function output(S, text)
local len = string.len(text)
local c = nil
local res = {}
for i = 1, len do
c = string.byte(text, i, i)
res[i] = string.char(bxor(S[i], c))
end
return table.concat(res)
end
-------------------------------
-------------bit wise-----------
-------------------------------
local bit_op = {}
function bit_op.cond_and(r_a, r_b)
return (r_a + r_b == 2) and 1 or 0
end
function bit_op.cond_xor(r_a, r_b)
return (r_a + r_b == 1) and 1 or 0
end
function bit_op.cond_or(r_a, r_b)
return (r_a + r_b > 0) and 1 or 0
end
function bit_op.base(op_cond, a, b)
-- bit operation
if a < b then
a, b = b, a
end
local res = 0
local shift = 1
while a ~= 0 do
r_a = a % 2
r_b = b % 2
res = shift * bit_op[op_cond](r_a, r_b) + res
shift = shift * 2
a = math.modf(a / 2)
b = math.modf(b / 2)
end
return res
end
function bxor(a, b)
return bit_op.base('cond_xor', a, b)
end
function band(a, b)
return bit_op.base('cond_and', a, b)
end
function bor(a, b)
return bit_op.base('cond_or', a, b)
end
--key = "Key"
--text = "Plaintext"
--K = RC4(key, text)
--print (K)
--text = RC4(key, K)
--print (text)
--
--key = "Wiki"
--text = "pedia"
--K = RC4(key, text)
--print (K)
--
--key = "Secret"
--text = "Attack at dawn"
--K = RC4(key, text)
--print (K)
fp_key = io.open("key.txt", "r")
fp_text = io.open("text.txt", "r")
res = io.open("lua.txt", "w")
for i = 1, 10000 do
key = fp_key:read()
text = fp_text:read()
K = RC4(key, text)
res:write(K.."\n")
end
fp_key:close()
fp_text:close()
res:close()