-
Notifications
You must be signed in to change notification settings - Fork 0
/
CBCDESDecryption.c
94 lines (70 loc) · 1.81 KB
/
CBCDESDecryption.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
#include "BitPermutationFunctions.h"
#include "DataPrint.h"
#include "CBCDESDecryption.h"
#include "DESDecrypt.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void CBC_DESDecryption(char *input,char* inputKey,char* iv,int size);
void CBC_DESDecryption(char *input,char* inputKey,char* iv,int size){
char* newMessage = NULL;
int i,j,k;
int stringLength = size;
int numBlocks = (size/8);
int rem = stringLength%8;
char message[16];
char temp[8];
char prev[8];
char plaintext[8];
if(numBlocks == 1) {
for(k=0; k <8; k++){
input[k] ^= iv[k];
}
keyRead(input,temp); //output right side
desDecryptionPer64(temp,inputKey);
} else {
for(i = 0;i < numBlocks;i++) {
memcpy(message,input+(8*i),8);
memcpy(temp,message,8);
if(i == 0) {
desDecryptionPer64_return(temp,inputKey,plaintext);
for(k=0; k <8; k++){
plaintext[k] ^= iv[k];
}
for(k = 0; k < 8; k++) {
printf("%c",plaintext[k]);
}
memcpy(prev,temp,8);
} else {
desDecryptionPer64_return(temp,inputKey,plaintext);
for(k=0; k <8; k++){
plaintext[k] ^= prev[k];
}
char checkNullTerm;
if(i == numBlocks - 2){
checkNullTerm = plaintext[7];
}
if(i == numBlocks - 1){
int remainder = (strlen(plaintext)+1)%8;
if(remainder != 0){
for(k = 0; k < (int)strlen(plaintext); k++) {
printf("%c",plaintext[k]);
if(plaintext[k] == '\n'){
break;
}
}
} else {
for(k = 0; k < (int)strlen(plaintext); k++) {
printf("%c",plaintext[k]);
}
}
} else {
for(k = 0; k < 8; k++) {
printf("%c",plaintext[k]);
}
}
memcpy(prev,temp,8);
}
}
}
}// end