-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdes.h
157 lines (142 loc) · 6.13 KB
/
des.h
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
//
// Created by Tayyip Öztürk on 17.01.2023.
//
#ifndef DES_H
#define DES_H
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/time.h>
#include <math.h>
#define BLOCK_SIZE 64
enum permutation_type {
permuted_choice_1 = 0,
permuted_choice_2 = 1,
initial_permutation = 2,
expansion_permutation = 3,
permutation = 4,
final_permutation = 5
};
int binary_to_decimal(char* binary, int len);
char* string_to_hex(char *str);
char* hex_to_binary(char *hex);
char* binary_to_hex(char *binary, int bits);
void shift_left(char* str, int len, int n);
char* xor_strings(char* str1, char* str2, int len);
void permute_key(const char *key, char *permutation_key, enum permutation_type type);
void generate_round_keys(char* key, char** round_keys);
char* generate_random_initialization_vector();
char* padding(char* plain_text, int len);
char** divide_plain_text_to_blocks(char* plain_text, int len);
char* DES(char* plain_text, char** round_keys);
char** CBC(char** blocks, unsigned long block_count, char* iv, char** round_keys, int mode);
char** encrypt(char* plain_text, char* IV, char** round_keys);
void decrypt(char** encrypted_block, int block_count, char* IV, char** round_keys);
const int IP[] = {
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
};
const int FP[] = {
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
};
const int SHIFTS[] = {
1, 1, 2, 2, 2, 2, 2, 2,1, 2, 2, 2, 2, 2, 2, 1};
const int E[] = {
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
};
const int P[] = {
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
};
const int PC1[] = {
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};
const int PC2[] = {
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};
const int S[8][64] = {
{
0xe, 0x4 ,0xd ,0x1 ,0x2 ,0xf ,0xb ,0x8 ,0x3 ,0xa ,0x6 ,0xc ,0x5 ,0x9 ,0x0 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