-
Notifications
You must be signed in to change notification settings - Fork 0
/
DESEncrypt.c
193 lines (149 loc) · 5.41 KB
/
DESEncrypt.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
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
#include "BitPermutationFunctions.h"
#include "DataPrint.h"
#include "DESEncrypt.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void DESEncryption(char *input,char* inputKey);
void desEncryptionPer64(char* message,char* key);
void printCharBinary(char* message);
void printCharHex(char* message);
void printIntBinary(int message);
void printCharBinary(char* message);
void printIntBinary(int message);
void printCharHex(char* message);
void desEncryptionPer64(char* message,char* key);
void desEncryptionPer64_return(char* message,char* key,char* ciphertext);
void desEncryptionPer64_nullReturn(char* message,char* key,char* ciphertext);
void DESEncryption(char* input,char* inputKey){
char* newMessage = NULL;
char(*messageBlocks)[8];
int i,j;
int stringLength = strlen(input) + 1;
int numBlocks = stringLength/ 8;
int rem = stringLength%8;
char temp[8];
if(numBlocks == 1 && rem == 0) {
desEncryptionPer64(input,inputKey);
}
else if(rem != 0){
newMessage = malloc(sizeof(char)*(stringLength + (8-rem)));
if( newMessage == NULL) {
printf("Fail to allocate.\n\n");
return;
}
memcpy(newMessage,input,stringLength);//17
for(i = stringLength;i < (stringLength + (8 - rem));i++) {
newMessage[i] &= 0x00;
}
for(i = 0;i < numBlocks + 1;i++) {
memcpy(temp,newMessage+(8*i),8);
desEncryptionPer64(temp,inputKey);
}
free(newMessage);
} else {
newMessage = malloc(sizeof(char)*(stringLength + (8-rem)));
if( newMessage == NULL) {
printf("Fail to allocate.\n\n");
return;
}
memcpy(newMessage,input,stringLength);//17
for(i = 0;i < numBlocks;i++) {
memcpy(temp,newMessage+(8*i),8);
desEncryptionPer64(temp,inputKey);
}
free(newMessage);
}
}// end
void desEncryptionPer64(char* message,char* key){
char pk[8]; // 56 bit permutation
char ip[8]; // destination 64 bit (permuted message)
char R[4],L[4]; // 32 bit left and right blocks
char c[4],d[4]; // c0 d0
char cShifts[17][4];//c schedule
char dShifts[17][4];// d scheulde
char keyCD[16][8];// concatanted schedule
char keyPC2[16][8];// sub keys
char subR[17][4]; //R transformation (round function)
char subL[17][4]; //L transformation (round function)
char roundOutput[8];
char finalOutput[8];//final Ouput
initialKeyPermutation(key,pk);// initial key permutation
initialPermutation(message,ip);// intial permutation
splitCharByte(ip,L,R);//L,R
splitCharByte(pk,c,d);//c0,d0
keyBlock(c,d,cShifts,dShifts); //key schedule
concatSubBlocks(cShifts,dShifts,keyCD);
PC2Permutation(keyCD,keyPC2); // generate subKeys from key schedule
int i;
for(i = 0;i < 17; i++) { // 16 iteration round function | 0th index initializes
Round(L,R,keyPC2,subR,subL,i);
}
flipSplitBytes(roundOutput,subR[16],subL[16]); // swap R and L
finalPermutation(roundOutput,finalOutput);// final permutation
//printCharHex(finalOutput);
FILE *ptr = fopen("ciphertext.bin","a+");
fwrite(finalOutput,sizeof(char),8,ptr);
fclose(ptr);
}
void desEncryptionPer64_return(char* message,char* key,char* ciphertext){
char pk[8]; // 56 bit permutation
char ip[8]; // destination 64 bit (permuted message)
char R[4],L[4]; // 32 bit left and right blocks
char c[4],d[4]; // c0 d0
char cShifts[17][4];//c schedule
char dShifts[17][4];// d scheulde
char keyCD[16][8];// concatanted schedule
char keyPC2[16][8];// sub keys
char subR[17][4]; //R transformation (round function)
char subL[17][4]; //L transformation (round function)
char roundOutput[8];
char finalOutput[8];//final Ouput
initialKeyPermutation(key,pk);// initial key permutation
initialPermutation(message,ip);// intial permutation
splitCharByte(ip,L,R);//L,R
splitCharByte(pk,c,d);//c0,d0
keyBlock(c,d,cShifts,dShifts); //key schedule
concatSubBlocks(cShifts,dShifts,keyCD);
PC2Permutation(keyCD,keyPC2); // generate subKeys from key schedule
int i;
for(i = 0;i < 17; i++) { // 16 iteration round function | 0th index initializes
Round(L,R,keyPC2,subR,subL,i);
}
flipSplitBytes(roundOutput,subR[16],subL[16]); // swap R and L
finalPermutation(roundOutput,finalOutput);// final permutation
//printCharHex(finalOutput);
FILE *ptr = fopen("ciphertext.bin","a+");
fwrite(finalOutput,sizeof(char),8,ptr);
fclose(ptr);
memcpy(ciphertext,finalOutput,8);
}
void desEncryptionPer64_nullReturn(char* message,char* key,char* ciphertext){
char pk[8]; // 56 bit permutation
char ip[8]; // destination 64 bit (permuted message)
char R[4],L[4]; // 32 bit left and right blocks
char c[4],d[4]; // c0 d0
char cShifts[17][4];//c schedule
char dShifts[17][4];// d scheulde
char keyCD[16][8];// concatanted schedule
char keyPC2[16][8];// sub keys
char subR[17][4]; //R transformation (round function)
char subL[17][4]; //L transformation (round function)
char roundOutput[8];
char finalOutput[8];//final Ouput
initialKeyPermutation(key,pk);// initial key permutation
initialPermutation(message,ip);// intial permutation
splitCharByte(ip,L,R);//L,R
splitCharByte(pk,c,d);//c0,d0
keyBlock(c,d,cShifts,dShifts); //key schedule
concatSubBlocks(cShifts,dShifts,keyCD);
PC2Permutation(keyCD,keyPC2); // generate subKeys from key schedule
int i;
for(i = 0;i < 17; i++) { // 16 iteration round function | 0th index initializes
Round(L,R,keyPC2,subR,subL,i);
}
flipSplitBytes(roundOutput,subR[16],subL[16]); // swap R and L
finalPermutation(roundOutput,finalOutput);// final permutation
//printCharHex(finalOutput);
memcpy(ciphertext,finalOutput,8);
}