-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathzeb.c
More file actions
125 lines (92 loc) · 2.35 KB
/
zeb.c
File metadata and controls
125 lines (92 loc) · 2.35 KB
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
#include <stdlib.h>
#include <lua.h>
#include <lualib.h>
#include <lauxlib.h>
#include <stdint.h>
#include <string.h>
#define byte unsigned char
#define offsetKey 18
#define indexKey 121
int encrypt0(byte* buffer, int bufSize, byte* keys, int keySize, byte* out, int offset)
{
//memcpy(out, buffer, bufSize);
// 遍历处理
int i = 0;
for (i = 0; i < bufSize; i++) {
byte b = buffer[i];
byte index = (byte) (indexKey - (i % indexKey));
byte c = (keySize > 0) ? keys[i % keySize] : 0;
c += index;
// 数据偏移
b = (byte) ((b + (offsetKey + keySize + c) + 256) % 256);
// 数据转化
b = (byte) (b ^ c);
// 设置数据
out[i + offset] = b;
};
return bufSize + offset;
//return bufSize;
}
int decrypt0(byte* buffer, int bufSize, byte* keys, int keySize, byte* out, int offset)
{
// 遍历处理
int i = 0;
for (i = 0; i < bufSize; i++) {
byte b = buffer[i];
byte index = (byte) (indexKey - (i % indexKey));
byte c = (keySize > 0) ? keys[i % keySize] : 0;
c += index;
// 数据转化
b = (byte) (b ^ c);
// 数据偏移
b = (byte) ((b - (offsetKey + keySize + c) + 256) % 256);
// 设置数据
out[i + offset] = b;
}
return bufSize + offset;
//return 0;
}
static int encrypt(lua_State *L)
{
//读取第一个参数
size_t bufferSize = 0;
const char* buffer = lua_tolstring(L, 1, &bufferSize);
size_t keySize = 0;
const char* keys = lua_tolstring(L, 2, &keySize);
//加密
byte* out = (byte *)malloc(bufferSize+1);
int outSize = encrypt0((byte*)buffer, (int)bufferSize, (byte*)keys, (int)keySize, out, 0);
out[outSize] = '\0'; //写入结束符
//返回数据
lua_pushfstring(L, (const char*)out);
//释放内存
free(out);
return 1;
}
static int decrypt(lua_State *L)
{
//读取第一个参数
size_t bufferSize = 0;
const char* buffer = lua_tolstring(L, 1, &bufferSize);
size_t keySize = 0;
const char* keys = lua_tolstring(L, 2, &keySize);
//解码
byte* out = (byte *)malloc(bufferSize+1);
int outSize = decrypt0((byte*)buffer, (int)bufferSize, (byte*)keys, (int)keySize, out, 0);
out[outSize] = '\0'; //写入结束符
//返回数据
//lua_pushfstring(L, (const char*)out);
lua_pushlstring(L, (const char*)out, bufferSize);
//释放内存
free(out);
return 1;
}
static const struct luaL_reg zedlib[] = {
{"encrypt", encrypt},
{"decrypt", decrypt},
{NULL, NULL}
};
LUALIB_API int luaopen_zed (lua_State *L) {
luaL_register(L, "ZED", zedlib);
return 1;
}