密码学实验

First Post:

Last Update:

Word Count:
5.4k

Read Time:
31 min

Page View: loading...

DES

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
#include <bits/stdc++.h>
using namespace std;
string string_to_hex(const string& str) //字符串转化为十六进制
{
string result="";
string tmp;
stringstream ss;
for(int i=0;i<str.size();i++)
{
ss<<hex<<int(str[i])<<endl;
ss>>tmp;
result+=tmp;
}
return result;
}
string hex_to_string(const std::string& str)
{
std::string result;
for (size_t i = 0; i < str.length(); i += 2)//十六进制两个字符为原始字符一个字符
{
std::string byte = str.substr(i, 2);//每次切两个字符
//将十六进制的string转成long再强转成int再转成char
char chr = (char)(int)strtol(byte.c_str(), NULL, 16);
result.push_back(chr);//将处理完的字符压入result中
}
return result;
}

string hex2bin(string s)
{
//十六进制转化为二进制
unordered_map<char, string> mp;
mp['0'] = "0000";
mp['1'] = "0001";
mp['2'] = "0010";
mp['3'] = "0011";
mp['4'] = "0100";
mp['5'] = "0101";
mp['6'] = "0110";
mp['7'] = "0111";
mp['8'] = "1000";
mp['9'] = "1001";
mp['A'] = "1010";
mp['B'] = "1011";
mp['C'] = "1100";
mp['D'] = "1101";
mp['E'] = "1110";
mp['F'] = "1111";
string bin = "";
for (int i = 0; i < s.size(); i++) {
bin += mp[s[i]];
}
return bin;
}
string bin2hex(string s)
{
// 二进制转化为16进制
unordered_map<string, string> mp;
mp["0000"] = "0";
mp["0001"] = "1";
mp["0010"] = "2";
mp["0011"] = "3";
mp["0100"] = "4";
mp["0101"] = "5";
mp["0110"] = "6";
mp["0111"] = "7";
mp["1000"] = "8";
mp["1001"] = "9";
mp["1010"] = "A";
mp["1011"] = "B";
mp["1100"] = "C";
mp["1101"] = "D";
mp["1110"] = "E";
mp["1111"] = "F";
string hex = "";
for (int i = 0; i < s.length(); i += 4) {
string ch = "";
ch += s[i];
ch += s[i + 1];
ch += s[i + 2];
ch += s[i + 3];
hex += mp[ch];
}
return hex;
}

string permute(string k, int* arr, int n)
{
string per = "";
for (int i = 0; i < n; i++) {
per += k[arr[i] - 1];
}
return per;
}

string shift_left(string k, int shifts)
{
string s = "";
for (int i = 0; i < shifts; i++) {
for (int j = 1; j < 28; j++) {
s += k[j];
}
s += k[0];
k = s;
s = "";
}
return k;
}

string xor_(string a, string b)
{
string ans = "";
for (int i = 0; i < a.size(); i++) {
if (a[i] == b[i]) {
ans += "0";
}
else {
ans += "1";
}
}
return ans;
}
string encrypt(string pt, vector<string> rkb,
vector<string> rk)
{
// 16->2
pt = hex2bin(pt);

// 初始置换
int initial_perm[64]
= { 58, 50, 42, 34, 26, 18, 10, 2, 60, 52, 44,
36, 28, 20, 12, 4, 62, 54, 46, 38, 30, 22,
14, 6, 64, 56, 48, 40, 32, 24, 16, 8, 57,
49, 41, 33, 25, 17, 9, 1, 59, 51, 43, 35,
27, 19, 11, 3, 61, 53, 45, 37, 29, 21, 13,
5, 63, 55, 47, 39, 31, 23, 15, 7 };
// 初始置换
pt = permute(pt, initial_perm, 64);
cout << "初始置换后: " << bin2hex(pt)
<< endl;

// 拆分成左右32比特
string left = pt.substr(0, 32);
string right = pt.substr(32, 32);
cout << "拆分后: L0=" << bin2hex(left)
<< " R0=" << bin2hex(right) << endl;

// E盒扩展
int exp_d[48]
= { 32, 1, 2, 3, 4, 5, 4, 5, 6, 7, 8, 9,
8, 9, 10, 11, 12, 13, 12, 13, 14, 15, 16, 17,
16, 17, 18, 19, 20, 21, 20, 21, 22, 23, 24, 25,
24, 25, 26, 27, 28, 29, 28, 29, 30, 31, 32, 1 };

// S盒
int s[8][4][16] = {
{ 14, 4, 13, 1, 2, 15, 11, 8, 3, 10, 6, 12, 5,9, 0, 7, 0, 15, 7, 4, 14, 2, 13, 1, 10, 6,12, 11, 9, 5, 3, 8, 4, 1, 14, 8, 13, 6, 2, 11, 15, 12, 9, 7, 3, 10, 5, 0, 15, 12, 8, 2, 4, 9, 1, 7, 5, 11, 3, 14, 10, 0, 6, 13 },
{ 15, 1, 8, 14, 6, 11, 3, 4, 9, 7, 2, 13, 12, 0, 5, 10, 3, 13, 4, 7, 15, 2, 8, 14, 12, 0, 1, 10, 6, 9, 11, 5, 0, 14, 7, 11, 10, 4, 13, 1, 5, 8, 12, 6, 9, 3, 2, 15, 13, 8, 10, 1, 3, 15, 4, 2, 11, 6, 7, 12, 0, 5, 14, 9 },
{ 10, 0, 9, 14, 6, 3, 15, 5, 1, 13, 12, 7, 11, 4, 2, 8, 13, 7, 0, 9, 3, 4, 6, 10, 2, 8, 5, 14, 12, 11, 15, 1, 13, 6, 4, 9, 8, 15, 3, 0, 11, 1, 2, 12, 5, 10, 14, 7, 1, 10, 13, 0, 6, 9, 8, 7, 4, 15, 14, 3, 11, 5, 2, 12 },
{ 7, 13, 14, 3, 0, 6, 9, 10, 1, 2, 8, 5, 11, 12, 4, 15, 13, 8, 11, 5, 6, 15, 0, 3, 4, 7, 2, 12, 1, 10, 14, 9, 10, 6, 9, 0, 12, 11, 7, 13, 15, 1, 3, 14, 5, 2, 8, 4, 3, 15, 0, 6, 10, 1, 13, 8, 9, 4, 5, 11, 12, 7, 2, 14 },
{ 2, 12, 4, 1, 7, 10, 11, 6, 8, 5, 3, 15, 13, 0, 14, 9, 14, 11, 2, 12, 4, 7, 13, 1, 5, 0, 15, 10, 3, 9, 8, 6, 4, 2, 1, 11, 10, 13, 7, 8, 15, 9, 12, 5, 6, 3, 0, 14, 11, 8, 12, 7, 1, 14, 2, 13, 6, 15, 0, 9, 10, 4, 5, 3 },
{ 12, 1, 10, 15, 9, 2, 6, 8, 0, 13, 3, 4, 14, 7, 5, 11, 10, 15, 4, 2, 7, 12, 9, 5, 6, 1, 13, 14, 0, 11, 3, 8, 9, 14, 15, 5, 2, 8, 12, 3, 7, 0, 4, 10, 1, 13, 11, 6, 4, 3, 2, 12, 9, 5, 15, 10, 11, 14, 1, 7, 6, 0, 8, 13 },
{ 4, 11, 2, 14, 15, 0, 8, 13, 3, 12, 9, 7, 5, 10, 6, 1, 13, 0, 11, 7, 4, 9, 1, 10, 14, 3, 5, 12, 2, 15, 8, 6, 1, 4, 11, 13, 12, 3, 7, 14, 10, 15, 6, 8, 0, 5, 9, 2, 6, 11, 13, 8, 1, 4, 10, 7, 9, 5, 0, 15, 14, 2, 3, 12 },
{ 13, 2, 8, 4, 6, 15, 11, 1, 10, 9, 3, 14, 5, 0, 12, 7, 1, 15, 13, 8, 10, 3, 7, 4, 12, 5, 6, 11, 0, 14, 9, 2, 7, 11, 4, 1, 9, 12, 14, 2, 0, 6, 10, 13, 15, 3, 5, 8, 2, 1, 14, 7, 4, 10, 8, 13, 15, 12, 9, 0, 3, 5, 6, 11 }
};

// P盒置换
int per[32]
= { 16, 7, 20, 21, 29, 12, 28, 17, 1, 15, 23,
26, 5, 18, 31, 10, 2, 8, 24, 14, 32, 27,
3, 9, 19, 13, 30, 6, 22, 11, 4, 25 };

cout << endl;
for (int i = 0; i < 16; i++) {
string right_expanded = permute(right, exp_d, 48);

string x = xor_(rkb[i], right_expanded);

// S
string op = "";
for (int i = 0; i < 8; i++) {
int row = 2 * int(x[i * 6] - '0')
+ int(x[i * 6 + 5] - '0');
int col = 8 * int(x[i * 6 + 1] - '0')
+ 4 * int(x[i * 6 + 2] - '0')
+ 2 * int(x[i * 6 + 3] - '0')
+ int(x[i * 6 + 4] - '0');
int val = s[i][row][col];
op += char(val / 8 + '0');
val = val % 8;
op += char(val / 4 + '0');
val = val % 4;
op += char(val / 2 + '0');
val = val % 2;
op += char(val + '0');
}
op = permute(op, per, 32);

x = xor_(op, left);

left = x;

// 交换左右
if (i != 15) {
swap(left, right);
}
cout << "轮数: " << i + 1 << " " << bin2hex(left)
<< " " << bin2hex(right) << " " << rk[i]
<< endl;
}

// 左右结合
string combine = left + right;

// IP逆置换
int final_perm[64]
= { 40, 8, 48, 16, 56, 24, 64, 32, 39, 7, 47,
15, 55, 23, 63, 31, 38, 6, 46, 14, 54, 22,
62, 30, 37, 5, 45, 13, 53, 21, 61, 29, 36,
4, 44, 12, 52, 20, 60, 28, 35, 3, 43, 11,
51, 19, 59, 27, 34, 2, 42, 10, 50, 18, 58,
26, 33, 1, 41, 9, 49, 17, 57, 25 };

string cipher = bin2hex(permute(combine, final_perm, 64));
return cipher;
}

int main()
{
string pt, key;
cout<<"请输入明文: ";
cin>>pt;
cout<<"请输入密文: ";
cin>>key;
pt = string_to_hex(pt);
cout<<pt;
key = string_to_hex(key);
key = hex2bin(key);
int keyp[56]
= { 57, 49, 41, 33, 25, 17, 9, 1, 58, 50, 42, 34,
26, 18, 10, 2, 59, 51, 43, 35, 27, 19, 11, 3,
60, 52, 44, 36, 63, 55, 47, 39, 31, 23, 15, 7,
62, 54, 46, 38, 30, 22, 14, 6, 61, 53, 45, 37,
29, 21, 13, 5, 28, 20, 12, 4 };

//密钥生成
key = permute(key, keyp, 56);

int shift_table[16] = { 1, 1, 2, 2, 2, 2, 2, 2,
1, 2, 2, 2, 2, 2, 2, 1 };

int key_comp[48] = { 14, 17, 11, 24, 1, 5, 3, 28,
15, 6, 21, 10, 23, 19, 12, 4,
26, 8, 16, 7, 27, 20, 13, 2,
41, 52, 31, 37, 47, 55, 30, 40,
51, 45, 33, 48, 44, 49, 39, 56,
34, 53, 46, 42, 50, 36, 29, 32 };
string left = key.substr(0, 28);
string right = key.substr(28, 28);

vector<string> rkb;
vector<string> rk;
for (int i = 0; i < 16; i++) {
left = shift_left(left, shift_table[i]);
right = shift_left(right, shift_table[i]);

string combine = left + right;

string RoundKey = permute(combine, key_comp, 48);

rkb.push_back(RoundKey);
rk.push_back(bin2hex(RoundKey));
}

cout << "加密:\n";
string cipher = encrypt(pt, rkb, rk);
cout << "密文: " << cipher << endl;

cout << "解密:\n";
reverse(rkb.begin(), rkb.end());
reverse(rk.begin(), rk.end());
string text = encrypt(cipher, rkb, rk);
text = hex_to_string(text);
cout << "明文: " << text << endl;
}

AES

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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
#include <iostream>
#include <bits/stdc++.h>
#include <stdint.h>
#include<iomanip>
void AddRoundKey(uint8_t mtx[],uint32_t w[]);
uint32_t SubKeys(uint32_t temp);
void SubBytes(uint8_t mtx[],int len);
void KeyExpansion(uint8_t key[], uint32_t new_key[]);
void Display(uint8_t Matrix[]);
void DisplayKeys(uint8_t Matrix[][16]);
uint32_t left_bit_move(uint32_t val, int n);
using namespace std;
uint32_t T[4][256];//存储4张列混淆表
int mixCol[4][4] = {
{0x02, 0x03, 0x01, 0x01},
{0x01, 0x02, 0x03, 0x01},
{0x01, 0x01, 0x02, 0x03},
{0x03, 0x01, 0x01, 0x02}
};//列混淆矩阵
uint8_t S_Box[16][16] =
{
{0x63, 0x7C, 0x77, 0x7B, 0xF2, 0x6B, 0x6F, 0xC5, 0x30, 0x01, 0x67, 0x2B, 0xFE, 0xD7, 0xAB, 0x76},
{0xCA, 0x82, 0xC9, 0x7D, 0xFA, 0x59, 0x47, 0xF0, 0xAD, 0xD4, 0xA2, 0xAF, 0x9C, 0xA4, 0x72, 0xC0},
{0xB7, 0xFD, 0x93, 0x26, 0x36, 0x3F, 0xF7, 0xCC, 0x34, 0xA5, 0xE5, 0xF1, 0x71, 0xD8, 0x31, 0x15},
{0x04, 0xC7, 0x23, 0xC3, 0x18, 0x96, 0x05, 0x9A, 0x07, 0x12, 0x80, 0xE2, 0xEB, 0x27, 0xB2, 0x75},
{0x09, 0x83, 0x2C, 0x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x89, 0x6F, 0xB7, 0x62, 0x0E, 0xAA, 0x18, 0xBE, 0x1B},
{0xFC, 0x56, 0x3E, 0x4B, 0xC6, 0xD2, 0x79, 0x20, 0x9A, 0xDB, 0xC0, 0xFE, 0x78, 0xCD, 0x5A, 0xF4},
{0x1F, 0xDD, 0xA8, 0x33, 0x88, 0x07, 0xC7, 0x31, 0xB1, 0x12, 0x10, 0x59, 0x27, 0x80, 0xEC, 0x5F},
{0x60, 0x51, 0x7F, 0xA9, 0x19, 0xB5, 0x4A, 0x0D, 0x2D, 0xE5, 0x7A, 0x9F, 0x93, 0xC9, 0x9C, 0xEF},
{0xA0, 0xE0, 0x3B, 0x4D, 0xAE, 0x2A, 0xF5, 0xB0, 0xC8, 0xEB, 0xBB, 0x3C, 0x83, 0x53, 0x99, 0x61},
{0x17, 0x2B, 0x04, 0x7E, 0xBA, 0x77, 0xD6, 0x26, 0xE1, 0x69, 0x14, 0x63, 0x55, 0x21, 0x0C, 0x7D}
};
//轮密钥加
uint32_t LunMiYaoJia[10] = {0x01000000, 0x02000000, 0x04000000, 0x08000000, 0x10000000,
0x20000000, 0x40000000, 0x80000000, 0x1b000000, 0x36000000};
uint32_t left_bit_move(uint32_t val, int n) {
uint32_t size = sizeof(val) * 8;
n = n % size;
return (val >> (size - n) | (val << n));//左移
//return (val << (size - n) | (val >> n));//右移
}
string string_to_hex(const string& str) //字符串转化为十六进制
{
string result="";
string tmp;
stringstream ss;
for(int i=0;i<str.size();i++)
{
ss<<hex<<int(str[i])<<endl;
ss>>tmp;
result+=tmp;
}
return result;
}
string hex_to_string(const std::string& str)
{
std::string result;
for (size_t i = 0; i < str.length(); i += 2)//十六进制两个字符为原始字符一个字符
{
std::string byte = str.substr(i, 2);//每次切两个字符
//将十六进制的string转成long再强转成int再转成char
char chr = (char)(int)strtol(byte.c_str(), NULL, 16);
result.push_back(chr);//将处理完的字符压入result中
}
return result;
}
//制作列混淆表
uint8_t Mul_02(uint8_t x)
{
if((x>>7) == 0)
return x<<1;
else
return ((x<<1)^0x1b)&0xff;
}
uint32_t get_output_col1(uint8_t x)
{
x = (uint32_t)x;
return (Mul_02(x)<<24)|(x<<16)|(x<<8)|(x^ Mul_02(x));
//2113
}

uint32_t get_output_col2(uint8_t x)
{
x = (uint32_t)x;
return ((x^Mul_02(x))<<24)|(Mul_02(x)<<16)|(x<<8)|(x);
//3211
}
uint32_t get_output_col3(uint8_t x)
{
x = (uint32_t)x;
return (x<<24)|((x^Mul_02(x))<<16)|(Mul_02(x)<<8)|(x);
//1321
}
uint32_t get_output_col4(uint8_t x)
{
x = (uint32_t)x;
return (x<<24)|(x<<16)|((x^Mul_02(x))<<8)|(Mul_02(x));
//1132
}
void get_TAB()
{
for(int i=0;i<4;i++) {
for (int j = 0; j < 256; j++) {
T[i][j]=0;
}
}
for(int i=0;i<256;i++) {
T[0][i] = get_output_col1((uint8_t) i);
T[1][i] = get_output_col2((uint8_t) i);
T[2][i] = get_output_col3((uint8_t) i);
T[3][i] = get_output_col4((uint8_t) i);
}
//输出列混淆表
// for(int i=0;i<4;i++) {
// for (int j = 0; j < 256; j++) {
// //printf("%8x ",T[i][j]);
// cout <<setw(8)<<setfill('0')<< hex << T[i][j] << " ";
// }
// cout<<endl;
// }
}

uint8_t GFMul(uint8_t a, uint8_t b) {
uint8_t p = 0;
uint8_t high;
for (int counter = 0; counter < 8; counter++)
{
if ((b & uint8_t(1)) != 0){//判断当前b的最后一位是否为0
p ^= a;}// p与a异或 即如果b当前最后一位不是0,就加上现在的a
high = (uint8_t) (a & uint8_t(0x80));//高4位,看是否会溢出,high=1就是溢出
a <<= 1;//a左移一位,即乘以2
if (high != 0){//发生溢出
a ^= 0x1b; }// x^8 + x^4 + x^3 + x + 1
b >>= 1;//右移,看b的下一位了

}
return p;
}

void KeyExpansion(uint8_t key[], uint32_t w[])
{
int j=0;
uint32_t temp=0;
// w[]的前4个就是输入的key
//初始化w[0]->w[3]
for(int i=0; i<4;i++){
w[i] = (key[4*i]<<24)|(key[4*i+1]<<16)|(key[4*i+2]<<8)|(key[4*i+3]);
//printf("%d:%x\n",i,w[i]);
}
//创建w[4]->w[43]
for(int i=4; i<44;i++){
if(i%4==0){
temp = left_bit_move(w[i-1],8);//循环左移8位
temp = SubKeys(temp);
w[i] = temp ^ w[i-4] ^ LunMiYaoJia[j++];
//printf("%d:%x\n",i,w[i]);
}
else {
w[i] = w[i - 1] ^ w[i - 4];
//printf("%d:%x\n",i,w[i]);
}
}
}

void AddRoundKey(uint8_t mtx[],uint32_t w[])
{

for(int i=0; i<4; i++){
uint8_t w1 = w[i] >> 24 & 0xff;
uint8_t w2 = (w[i] >> 16) & 0xff;
uint8_t w3 = (w[i] >> 8) & 0xff;
uint8_t w4 = (w[i]) & 0xff;
mtx[i] ^= w1; //按列异或
mtx[i+4] ^= w2;
mtx[i+8] ^= w3;
mtx[i+12] ^= w4;
}
}

// S盒密钥变换 - 前4位为行号,后4位为列号
uint32_t SubKeys(uint32_t temp)
{
uint8_t w[4] = {0};
int j=0;
//循环4次
for(int i=3; i>=0; i--){
w[j++] = (temp>>(i*8))&0xff;
}
SubBytes(w,4);
return (w[0]<<24|w[1]<<16|w[2]<<8|w[3]);
}

// S盒变换 - 前4位为行号,后4位为列号
void SubBytes(uint8_t mtx[],int len)
{
for(int i=0; i<len; ++i)
{
int row = ((mtx[i]>>7)&0x1)*8 + ((mtx[i]>>6)&0x1)*4 + ((mtx[i]>>5)&0x1)*2 + ((mtx[i]>>4)&0x1)*1; //第4 5 6 7位确定行
int col = ((mtx[i]>>3)&0x1)*8 + ((mtx[i]>>2)&0x1)*4 + ((mtx[i]>>1)&0x1)*2 + (mtx[i]&0x1)*1; //第0 1 2 3位确定列
mtx[i] = S_Box[row][col];
}
}
void InvSubBytes(uint8_t mtx[],int len)
{
for(int i=0; i<len; ++i)
{
int row = ((mtx[i]>>7)&0x1)*8 + ((mtx[i]>>6)&0x1)*4 + ((mtx[i]>>5)&0x1)*2 + ((mtx[i]>>4)&0x1)*1; //第4 5 6 7位确定行
int col = ((mtx[i]>>3)&0x1)*8 + ((mtx[i]>>2)&0x1)*4 + ((mtx[i]>>1)&0x1)*2 + (mtx[i]&0x1)*1; //第0 1 2 3位确定列
mtx[i] = Inv_S_Box[row][col];
}
}
//行移位
void ShiftRows(uint8_t mtx[])
{
uint8_t temp=mtx[4];
//第二行左移一位
for(int i=0; i<3; i++){
mtx[i+4] = mtx[i+5];
}
mtx[7] = temp;
//第三行左移二位
for(int i=0; i<2; i++){
temp = mtx[i+8];
mtx[i+8] = mtx[i+10];
mtx[i+10] = temp;
}
//第四行左移三位
temp = mtx[15];
for(int i=3; i>0; i--) {
mtx[i + 12] = mtx[i + 11];
}
mtx[12] = temp;
}

void InvShiftRows(uint8_t mtx[])
{
// 第二行循环右移一位
uint8_t temp = mtx[7];
for(int i=3; i>0; --i)
mtx[i+4] = mtx[i+3];
mtx[4] = temp;
// 第三行循环右移两位
for(int i=0; i<2; ++i)
{
temp = mtx[i+8];
mtx[i+8] = mtx[i+10];
mtx[i+10] = temp;
}
// 第四行循环右移三位
temp = mtx[12];
for(int i=0; i<3; ++i)
mtx[i+12] = mtx[i+13];
mtx[15] = temp;
}

void MixColumns(uint8_t c[])
{
uint32_t total[4]={0}; //存储每一列取出的4个32比特字节异或的结果,即列混淆后的第i列
uint32_t temp[16]={0}; //存储16个 每个字节从T表得到的32比特的4字节一列
for(int i=0;i<16;i++){
temp[i] = T[i/4][c[i]];
}

for(int i=0; i<4;i++) {
total[i] = temp[i] ^ temp[i+4] ^ temp[i+8] ^ temp[i+12];
c[i] = (total[i]>>24)&0xff;
c[i+4]=(total[i]>>16)&0xff;
c[i+8] = (total[i]>>8)&0xff;
c[i+12] = (total[i])&0xff;
}
}
void InvMixColumns(uint8_t mtx[])
{
uint8_t arr[4];
for(int i=0; i<4; ++i) {
for (int j = 0; j < 4; ++j)
arr[j] = mtx[i + j * 4];

mtx[i] = GFMul(0x0e, arr[0]) ^ GFMul(0x0b, arr[1]) ^ GFMul(0x0d, arr[2]) ^ GFMul(0x09, arr[3]);
mtx[i + 4] = GFMul(0x09, arr[0]) ^ GFMul(0x0e, arr[1]) ^ GFMul(0x0b, arr[2]) ^ GFMul(0x0d, arr[3]);
mtx[i + 8] = GFMul(0x0d, arr[0]) ^ GFMul(0x09, arr[1]) ^ GFMul(0x0e, arr[2]) ^ GFMul(0x0b, arr[3]);
mtx[i + 12] = GFMul(0x0b, arr[0]) ^ GFMul(0x0d, arr[1]) ^ GFMul(0x09, arr[2]) ^ GFMul(0x0e, arr[3]);
}
}

void Encrypt(uint8_t m[],uint32_t key[],uint8_t c[])
{
//首先进行开始的轮密钥加
int round=0;
uint32_t w[4]={0};
for(int k=0; k<4; k++)
w[k] = key[4*round+k];
for(int i=0; i<16; i++)
c[i] = m[i];
AddRoundKey(c,w);
//接下来是9轮一摸一样的函数
for(int i = 1;i<=9;i++)
{
SubBytes(c,16);//字节替代
ShiftRows(c);//行移位
MixColumns(c);//列混淆
round++;
//cout<<"轮数"<<round<<endl;
for(int k=0; k<4; k++) {
w[k] = key[4 * round + k];
}
AddRoundKey(c,w);//轮密钥加
}
//最后一轮
round++;
//cout<<"轮数"<<round<<endl;
for(int k=0; k<4; k++) {
w[k] = key[4 * round + k];
//("%x ",w[k]);
}
SubBytes(c,16);//字节替代
ShiftRows(c);//行移位
AddRoundKey(c,w);//轮密钥加
}
void Decrypt(uint8_t c[],uint32_t key[],uint8_t m[])
{
//首先进行开始的轮密钥加
//注意密钥反着来
int round=10;
uint32_t w[4]={0};
for(int k=0; k<4; k++)
w[k] = key[4*round+k]; //密钥赋值
for(int i=0; i<16; i++)
m[i] = c[i];
AddRoundKey(m,w);
//接下来是9轮一摸一样的函数
for(int i = 1;i<=9;i++)
{
InvShiftRows(m);//行移位
InvSubBytes(m,16);//字节替代
round--;
//cout<<"轮数"<<round<<endl;
for(int k=0; k<4; k++) {
w[k] = key[4 * round + k];
}
AddRoundKey(m,w);//轮密钥加
InvMixColumns(m);//列混淆
}
//最后一轮
round--;
//cout<<"轮数"<<round<<endl;
for(int k=0; k<4; k++) {
w[k] = key[4 * round + k];
//("%x ",w[k]);
}
InvShiftRows(m);//行移位
InvSubBytes(m,16);//字节替代
AddRoundKey(m,w);//轮密钥加
}
void Display(uint8_t Matrix[])
{
for(int i=0; i<16; i++) {
printf("%x ", Matrix[i]);
if(i%4==3)
cout<<endl;
}
}
void DisplayKeys(uint32_t Matrix[])
{
for(int i=0; i<44; i++) {
printf("第%d列:%x\n",i,Matrix[i]);
}
}
int main()
{
string mingwen;
string miwen;
uint8_t key[16]={0};
uint8_t m[16]={0};
cout<<"请输入明文(128bits):";
cin>>mingwen;
cout<<"请输入密文(128bits):";
cin>>miwen;
for(int i=0;i<16;i++)
{
m[i] = (uint8_t)(mingwen[i]);
key[i] = (uint8_t)(miwen[i]);
}
uint8_t c[16]={0};
uint8_t x[16]={0};
uint32_t new_key[44]={0}; //存储44列密钥
cout<<"明文:"<<endl;
Display(m);
cout<<"密钥:"<<endl;
Display(key);
get_TAB();//生成列混淆表
KeyExpansion(key,new_key);
cout<<"生成密钥"<<endl;
//DisplayKeys(new_key);
Encrypt(m,new_key,c);
cout<<"加密结果"<<endl;
Display(c);
Decrypt(c,new_key,x);
cout<<"解密结果(hex)"<<endl;
Display(x);
cout<<"解密结果(string)"<<endl;
for(int i=0;i<16;i++)
cout<<(char)x[i];
return 0;
}

SM4

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
#include "string.h"
#include<iostream>
#include <stdint.h>
#include<string>
using namespace std;
uint32_t shift_l(uint32_t s,int n)//循环左移n位
{
int size=sizeof (s)*8; //字节数乘以8代表一共多少位
return s<<(n)|(s>>size-n);
}
uint32_t Sb(uint32_t s) {

uint8_t SboxTable[16][16] =
{
{0xd6, 0x90, 0xe9, 0xfe, 0xcc, 0xe1, 0x3d, 0xb7, 0x16, 0xb6, 0x14, 0xc2, 0x28, 0xfb, 0x2c, 0x05},
{0x2b, 0x67, 0x9a, 0x76, 0x2a, 0xbe, 0x04, 0xc3, 0xaa, 0x44, 0x13, 0x26, 0x49, 0x86, 0x06, 0x99},
{0x9c, 0x42, 0x50, 0xf4, 0x91, 0xef, 0x98, 0x7a, 0x33, 0x54, 0x0b, 0x43, 0xed, 0xcf, 0xac, 0x62},
{0xe4, 0xb3, 0x1c, 0xa9, 0xc9, 0x08, 0xe8, 0x95, 0x80, 0xdf, 0x94, 0xfa, 0x75, 0x8f, 0x3f, 0xa6},
{0x47, 0x07, 0xa7, 0xfc, 0xf3, 0x73, 0x17, 0xba, 0x83, 0x59, 0x3c, 0x19, 0xe6, 0x85, 0x4f, 0xa8},
{0x68, 0x6b, 0x81, 0xb2, 0x71, 0x64, 0xda, 0x8b, 0xf8, 0xeb, 0x0f, 0x4b, 0x70, 0x56, 0x9d, 0x35},
{0x1e, 0x24, 0x0e, 0x5e, 0x63, 0x58, 0xd1, 0xa2, 0x25, 0x22, 0x7c, 0x3b, 0x01, 0x21, 0x78, 0x87},
{0xd4, 0x00, 0x46, 0x57, 0x9f, 0xd3, 0x27, 0x52, 0x4c, 0x36, 0x02, 0xe7, 0xa0, 0xc4, 0xc8, 0x9e},
{0xea, 0xbf, 0x8a, 0xd2, 0x40, 0xc7, 0x38, 0xb5, 0xa3, 0xf7, 0xf2, 0xce, 0xf9, 0x61, 0x15, 0xa1},
{0xe0, 0xae, 0x5d, 0xa4, 0x9b, 0x34, 0x1a, 0x55, 0xad, 0x93, 0x32, 0x30, 0xf5, 0x8c, 0xb1, 0xe3},
{0x1d, 0xf6, 0xe2, 0x2e, 0x82, 0x66, 0xca, 0x60, 0xc0, 0x29, 0x23, 0xab, 0x0d, 0x53, 0x4e, 0x6f},
{0xd5, 0xdb, 0x37, 0x45, 0xde, 0xfd, 0x8e, 0x2f, 0x03, 0xff, 0x6a, 0x72, 0x6d, 0x6c, 0x5b, 0x51},
{0x8d, 0x1b, 0xaf, 0x92, 0xbb, 0xdd, 0xbc, 0x7f, 0x11, 0xd9, 0x5c, 0x41, 0x1f, 0x10, 0x5a, 0xd8},
{0x0a, 0xc1, 0x31, 0x88, 0xa5, 0xcd, 0x7b, 0xbd, 0x2d, 0x74, 0xd0, 0x12, 0xb8, 0xe5, 0xb4, 0xb0},
{0x89, 0x69, 0x97, 0x4a, 0x0c, 0x96, 0x77, 0x7e, 0x65, 0xb9, 0xf1, 0x09, 0xc5, 0x6e, 0xc6, 0x84},
{0x18, 0xf0, 0x7d, 0xec, 0x3a, 0xdc, 0x4d, 0x20, 0x79, 0xee, 0x5f, 0x3e, 0xd7, 0xcb, 0x39, 0x48}};
uint8_t a[4];
a[0]=(s>>24)&0xff; //最高8位
a[1]=(s>>16)&0xff;
a[2]=(s>>8)&0xff;
a[3]=s&0xff;
uint8_t b[4];
for(int i=0;i<4;i++) {
int row = ((a[i] >> 7) & 1) * 8 + ((a[i] >> 6) & 1) * 4 + ((a[i] >> 5) & 1) * 2 + ((a[i] >> 4) & 1) * 1;
int col = ((a[i] >> 3) & 1) * 8 + ((a[i] >> 2) & 1) * 4 + ((a[i] >> 1) & 1) * 2 + ((a[i]) & 1) * 1;
b[i] = SboxTable[row][col];
}
uint32_t result;
result=(b[0]<<24)|(b[1]<<16)|(b[2]<<8)|b[3];
return result;
}

uint32_t L_change(uint32_t s)
{
return s^ shift_l(s,13)^ shift_l(s,23);
}

uint32_t T_change(uint32_t s)
{
return L_change(Sb(s));
}

void keyexpand(uint32_t MK[4],uint32_t rk[32])
{
uint32_t FK[4]={0xA3B1BAC6, 0x56AA3350,0x677D9197, 0xB27022DC};
uint32_t CK[32] = { 0x00070E15, 0x1C232A31, 0x383F464D, 0x545B6269,
0x70777E85, 0x8C939AA1, 0xA8AFB6BD, 0xC4CBD2D9,
0xE0E7EEF5, 0xFC030A11, 0x181F262D, 0x343B4249,
0x50575E65, 0x6C737A81, 0x888F969D, 0xA4ABB2B9,
0xC0C7CED5, 0xDCE3EAF1, 0xF8FF060D, 0x141B2229,
0x30373E45, 0x4C535A61, 0x686F767D, 0x848B9299,
0xA0A7AEB5, 0xBCC3CAD1, 0xD8DFE6ED, 0xF4FB0209,
0x10171E25, 0x2C333A41, 0x484F565D, 0x646B7279 };
uint32_t k[4]={MK[0]^FK[0],MK[1]^FK[1],MK[2]^FK[2],MK[3]^FK[3]};
uint32_t K[36];
for(int i=0;i<=3;i++)
K[i]=k[i];
for(int i=0;i<32;i++)
{
K[i+4]=K[i]^ T_change(K[i+1]^K[i+2]^K[i+3]^CK[i]);
}
for(int i=0;i<32;i++)
rk[i]=K[i+4];

}

uint32_t Lchange(uint32_t s)
{
return s^shift_l(s,2)^shift_l(s,10)^shift_l(s,18)^shift_l(s,24);
}

uint32_t Tchange(uint32_t s)
{
return Lchange(Sb(s));
}

uint32_t fround(uint32_t X[4],uint32_t rk)
{
uint32_t B=X[1]^X[2]^X[3]^rk;
return X[0]^ L_change(Sb(B));
}

static uint32_t out1[4];

void SM4(uint32_t X[4],uint32_t rk[32])
{
uint32_t Y[36];
Y[0]=X[0];
Y[1]=X[1];
Y[2]=X[2];
Y[3]=X[3];
for(int i=0;i<=31;i++) {
Y[i + 4] = Y[i] ^ Tchange(Y[i + 1] ^ Y[i + 2] ^ Y[i + 3] ^ rk[i]);
}
// for(int i=0;i<36;i++) {
// printf("第%d轮:%x \n",i,Y[i]);
// }
out1[0]=Y[35];
out1[1]=Y[34];
out1[2]=Y[33];
out1[3]=Y[32];
}

static uint32_t out2[4];

void sm4(uint32_t Y[4],uint32_t rk[32])
{
uint32_t X[36];
X[35]=Y[0];
X[34]=Y[1];
X[33]=Y[2];
X[32]=Y[3];
for(int i=31;i>=0;i--)
{
X[i]=X[i+4]^ Tchange(X[i+3]^X[i+2]^X[i+1]^rk[i]);
}
out2[0]=X[0];
out2[1]=X[1];
out2[2]=X[2];
out2[3]=X[3];
}
int main() {
uint32_t ming[4]={0x01234567,0x89ABCDEF,0xFEDCBA98,0x76543210};
cout<<"明文:"<<endl;
for(int i=0;i<4;i++)
cout<<hex<<ming[i]<<" ";
cout<<endl;
uint32_t MK[4]={0x01234567,0x89ABCDEF,0xFEDCBA98,0x76543210};
cout<<"密钥:"<<endl;
for(int i=0;i<4;i++)
cout<<hex<<MK[i]<<" ";
cout<<endl;
uint32_t rk[32];
keyexpand(MK,rk);
// cout<<"扩展密钥:"<<endl;
// for(int i=0;i<32;i++)
// cout<<i<<"轮扩展密钥"<<rk[i]<<endl;
cout<<"加密结果:"<<endl;
SM4(ming,rk);
for(int i=0;i<4;i++)
cout<<out1[i]<<" ";
cout<<endl;
cout<<"解密结果:"<<endl;
sm4(out1,rk);
for(int i=0;i<4;i++)
cout<<out2[i]<<" ";
cout<<endl;
return 0;
}