-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEncryption.h
More file actions
executable file
·318 lines (302 loc) · 6.52 KB
/
Encryption.h
File metadata and controls
executable file
·318 lines (302 loc) · 6.52 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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
#ifndef __ENCRYPTION_H__
#define __ENCRYPTION_H__
#include <vector>
#include <string>
#include "stdio.h"
#define Mo 11
//class Verify{
//
//public:
// bool AddVerify(vector<int> vec)
// {
// int len = (int)vec.size();
// float sum = 0;
// for (int i = 0; i < len; ++i)
// {
// sum += vec[i] * (i+1);
// }
// int r = sum % Mo;
// vec.push_back(r);
// return true;
// }
// bool DecodeVerify(vector<int> v)
// {
// int len = (int)v.size();
// int verify = v[len - 1];
// float testsum = 0;
// for (int i = 0; i < len - 1; ++i)
// {
// testsum += v[i] * (i + 1);
// }
// int r = testsum % Mo;
// if (r == verify)
// {
// return true;
// }
// else
// return false;
// }
//};
#define round(x) ((x)>0?int((x)+0.5f):int((x)-0.5f))
template<class T> inline const T Min(const T& x, const T& y){
return x < y ? x : y;
}
template<class T> inline const T Max(const T& x, const T& y){
return x > y ? x : y;
}
class Encryption{
public:
int ciphertext(char m){
if (m >= '0' && m <= '9'){
return m - '0' + 1;
}
switch(m){
case '.':
return 11;
case '(':
return 12;
case ')':
return 13;
case ' ':
return 14;
case '-':
return 15;
}
return 0;
}
char deciphertext(int e){
if (e <= 10) return char(e -1 + '0');
switch(e){
case 11:
return '.';
case 12:
return '(';
case 13:
return ')';
case 14:
return ' ';
case 15:
return '-';
}
return 0;
}
void encipher(char * buffer, int len){
int tmp, j = 0;
int i;
for(i =0; i < len; i ++){
switch(i % 3){
case 0:
tmp = ciphertext(buffer[i]) * 4;
break;
case 1:
buffer[j ++] = map_into_printablechar(tmp + (ciphertext(buffer[i]) / 4));
tmp = (ciphertext(buffer[i]) % 4) * 16;
break;
case 2:
buffer[j ++] = map_into_printablechar(tmp + ciphertext(buffer[i]));
}
}
if(len % 3 != 0){
buffer[j ++] = map_into_printablechar(tmp);
}
buffer[j] = 0;
}
void decipher(char * buffer, int len){
int* tmp = new int[len], j = 0;
int i;
for(i = 0; i < len; i ++)
tmp[i] = map_from_printablechar(buffer[i]);
for(i =0; i < len; i ++){
switch(i % 2){
case 0:
buffer[j ++] = deciphertext(tmp[i] / 4);
break;
case 1:
buffer[j ++] = deciphertext((tmp[i-1] % 4) * 4 + tmp[i] / 16);
buffer[j ++] = deciphertext(tmp[i] % 16);
break;
}
}
buffer[j] = 0;
delete tmp;
}
char map_into_printablechar(int i){
if (i < 10) return i + '0';
i -= 10;
if (i < 26) return i + 'a';
i -= 26;
if (i < 26) return i + 'A';
i -= 26;
switch(i){
case 0: return '(';
case 1: return ')';
case 2: return '.';
case 3: return '+';
case 4: return '-';
case 5: return '*';
case 6: return '/';
case 7: return '?';
case 8: return '<';
case 9: return '>';
default:return '_';
}
}
int map_from_printablechar(char m){
if (m >= '0' && m <= '9')
return m - '0';
if (m >= 'a' && m <= 'z')
return m - 'a' + 10;
if (m >= 'A' && m <= 'Z')
return m - 'A' + 36;
if (m == '(')
return 62;
if (m == ')')
return 63;
if (m == '.')
return 64;
if (m == '+')
return 65;
if (m == '-')
return 66;
if (m == '*')
return 67;
if (m == '/')
return 68;
if (m == '?')
return 69;
if (m == '<')
return 70;
if (m == '>')
return 71;
return 72;
}
//map a symmetrical section into [0, map_length]
inline unsigned char map(float realvalue, float valid_length, float map_length){
//cout << round(map_length * Max(0.0f, Min((realvalue / valid_length + 0.5f), 1.0f))) << endl;
return (unsigned char)round(map_length * Max(0.0f, Min((realvalue / valid_length + 0.5f), 1.0f)));
}
//antimap [0, map_length] into a symmetrical section
inline float antimap(unsigned char mapvalue, float valid_length, float map_length){
return ((float)mapvalue / map_length - 0.5f) * valid_length;
}
unsigned char f2c(float f, float validlength, float maplength){
unsigned char data = map(f, validlength, maplength);
return data;
}
float c2f(unsigned char c, float valid_length,float maplength)/*int acu, int range_exp = 8, float ref_value,*/
{
//#ifndef _LONG_AUDIO
// unsigned char data = map(ref_value, valid_length, maplength);
// unsigned char tmp, high_bits;
// unsigned char mask = 255;
//
// //retrieve high bits (which is cut off when msg is generated)
// mask = mask << range_exp;
// high_bits = (data & mask) >> range_exp;
//
// /*simulate transferd bits and compare it with received bits
// (the comparing result is used to rectify the high bits)
// if their distance > 1/2 max_distance then high bits should be rectified
// */
// tmp = data << (8-range_exp);
// tmp = tmp >> (8-range_exp);
// tmp = tmp >> acu;
//
// if(abs(tmp - c) > (1 << (bit_len(acu, range_exp)-1))){
// if(tmp > c) high_bits ++;
// else high_bits --;
// }
//
// data = high_bits << range_exp;
// data = data >> acu;
// data |= c;
// data = data << acu;
//
// return antimap(data, valid_length, maplength);
//#else
return antimap(c, valid_length, maplength);
//#endif
}
/*int bit_len(int acu, int range_exp){
#ifdef _LONG_AUDIO
return 8;
#else
return range_exp - acu;
#endif */
/*}*/
int float_int(float num)//简单的强制转换
{
int result;
if (num < 0)
{
result = (int)(num - 0.5);
}
else
{
result = (int)(num + 0.5);
}
return result;
}
struct intNum
{
int head;
int tail;
};
intNum float_int_Robust(float num)//...脑残的方法
{
intNum result;
char temp[20];
sprintf(temp,"%.3f",num);
string tempString = temp;
int pos = tempString.find(".");
int err = (int)tempString.length() - pos;
if (err == 0)
{
result.head = (int)num;
result.tail = 0;
}
else
{
result.head = regard(tempString.substr(0,pos));
result.tail = regard(tempString.substr(pos+1,pos+4));
}
return result;
}
int regard(string str)
{
int n;
n=atoi(str.c_str());
return n;
}
int Combine(float input_float)
{
intNum temp = float_int_Robust(input_float);
int left = temp.head;
int right = temp.tail;
left = left << 3;
right /= 125;
left |= right;
return left;
}
int count(int num)
{
int countValid = 0;
int flag = sizeof(int) * 8;
if (num & 1)
{
countValid = 1;
}
int i = 0;
while (flag)
{
--flag;
++i;
if ((num >> i) & 1)
{
countValid ++;
}
}
return countValid;
}
};
#endif