-
Notifications
You must be signed in to change notification settings - Fork 0
/
DESDecrypt.c
185 lines (137 loc) · 4.94 KB
/
DESDecrypt.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
#include "BitPermutationFunctions.h"
#include "DESDecrypt.h"
#include "DataPrint.h"
#include "keyReader.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void DESDecryption(char *input,char* inputKey,int size);
void desDecryptionPer64(char* message,char* key);
void desDecryptionPer64_returnChar(char* message,char* key,char* plaintext);
void printCharBinary(char* message);
void printCharHex(char* message);
void printIntBinary(int message);
void printList(char* message);
void DESDecryption(char *input,char* inputKey,int size){
char* newMessage = NULL;
int i,j;
int stringLength = size;
int numBlocks = stringLength/8;
int rem = stringLength%8;
char message[16];
char temp[8];
if(numBlocks == 1) {
memcpy(temp,input,8);
//keyRead(input,temp); //output right side
desDecryptionPer64(temp,inputKey);
printf("\n");
} else {
for(i = 0;i < numBlocks;i++) {
memcpy(message,input+(8*i),8);
memcpy(temp,message,8);
desDecryptionPer64(temp,inputKey);
}
}
}// end
void printList(char* message) {
int i;
for(i = 0; i < 16;i++) {
printf("%c",message[i]);
}
printf("\n");
}
void desDecryptionPer64(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 keyReversal[16][8]; // sub key reveral ****
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
reverseSubKeys(keyPC2,keyReversal);
int i;
for(i = 0;i < 17; i++) { // 16 iteration round function | 0th index initializes
Round(L,R,keyReversal,subR,subL,i);
}
flipSplitBytes(roundOutput,subR[16],subL[16]); // swap R and L
finalPermutation(roundOutput,finalOutput);// final permutation
for(i = 0;i < 8; i++) {
printf("%c",finalOutput[i]);
}
}
void desDecryptionPer64_return(char* message,char* key,char* plaintext){
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 keyReversal[16][8]; // sub key reveral ****
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
reverseSubKeys(keyPC2,keyReversal);
int i;
for(i = 0;i < 17; i++) { // 16 iteration round function | 0th index initializes
Round(L,R,keyReversal,subR,subL,i);
}
flipSplitBytes(roundOutput,subR[16],subL[16]); // swap R and L
finalPermutation(roundOutput,finalOutput);// final permutation
memcpy(plaintext,finalOutput,8);
//printf("%s",finalOutput);
}
void desDecryptionPer64_returnChar(char* message,char* key,char* plaintext){
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 keyReversal[16][8]; // sub key reveral ****
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
reverseSubKeys(keyPC2,keyReversal);
int i;
for(i = 0;i < 17; i++) { // 16 iteration round function | 0th index initializes
Round(L,R,keyReversal,subR,subL,i);
}
flipSplitBytes(roundOutput,subR[16],subL[16]); // swap R and L
finalPermutation(roundOutput,finalOutput);// final permutation
memcpy(plaintext,finalOutput,8);
fwrite(finalOutput,sizeof(char),8,stdout);
//printf("%s",finalOutput);
}