-
Notifications
You must be signed in to change notification settings - Fork 0
/
libex_crypto.c
executable file
·126 lines (98 loc) · 3.96 KB
/
libex_crypto.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
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
/* -------------------------------------------------------------------------- */
/* (c) [email protected] [libex_crypto.c] */
/* -------------------------------------------------------------------------- */
#include "libex_c_precompiled.h"
#include "libex_crypto_.h"
#if defined INCLUDED_LIBEX_CRYPTO_H
#include "libex_call_.h"
#include "libex_debug_.h"
#include "libex_error_.h"
#include "libex_.h"
#include "libex_type_.h"
#if defined __cplusplus_cli
#pragma unmanaged
#endif
#if defined __GNUC__ && !defined __MINGW32__
#pragma GCC diagnostic ignored "-Wmissing-declarations"
#endif
#if defined __clang__
#pragma GCC diagnostic ignored "-Wmissing-prototypes"
#endif
/* -------------------------------------------------------------------------- */
NAMESPACE(c_)
static new_bytes_t _simple_encrypt_or_decrypt( /*F*/
const bytes_t data_, /*-*/
const bytes_t key_, /*-*/
const bool encrypt_ ) { /*-*/
GO (UID(FE900A));
new_bytes_t ret = { 0, NULL };
size_t data_pos = 0;
size_t key_pos = 0;
size_t ret_pos = 0;
if (data_.size < 1) {
WARN(_T("data_ is empty."), UID(E7D2B4));
RETURN(ret);
}
if (key_.size < 1) {
WARN(_T("key_ is empty."), UID(EB45C4));
RETURN(ret);
}
ret.size = data_.size;
ret.data = MALLOC(ret.size);
for (data_pos = 0; data_pos < data_.size; data_pos++) {
int data_byte = data_.data[data_pos];
const int key_byte = key_.data[key_pos];
if (data_byte > 31) { /* leave control characters unchanged */
uint8_t range = 0;
uint8_t shift = 0;
uint8_t min = 0;
uint8_t max = 0;
if (data_byte >= 32 && data_byte <= 126) {
min = 32;
max = 126;
}
if (data_byte >= 127 && data_byte <= 255) {
min = 127;
max = 255;
}
range = max - min + 1;
data_byte -= min;
data_byte++;
shift = key_byte % range;
if (encrypt_) {
data_byte += shift;
if (data_byte > range) {
data_byte -= range;
}
} else {
data_byte -= shift;
if (data_byte < 1) {
data_byte += range;
}
}
data_byte += min;
data_byte--;
}
ret.data[ret_pos++] = CAST(uint8_t, data_byte);
if (++key_pos >= key_.size) {
key_pos = 0;
}
}
RETURN(ret);
} /* _simple_encrypt_or_decrypt */
PUBLIC new_bytes_t simple_encrypt( /*F*/
const bytes_t data_, /*-*/
const bytes_t key_ ) { /*-*/
GO (UID(FBCDBD));
const new_bytes_t ret = _simple_encrypt_or_decrypt(data_, key_, true);
RETURN(ret);
} /* simple_encrypt */
PUBLIC new_bytes_t simple_decrypt( /*F*/
const bytes_t data_, /*-*/
const bytes_t key_ ) { /*-*/
GO (UID(F75D5B));
const new_bytes_t ret = _simple_encrypt_or_decrypt(data_, key_, false);
RETURN(ret);
} /* simple_decrypt */
END_NAMESPACE /*c_*/
#endif /*eof*/