test_dec.c
2.35 KB
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "rijndael-api-ref.h"
#define BLOCK_SIZE 128
#define FILE_SIZE 512
int main(int argc, char **argv){
FILE *inptr;
FILE *outptr;
char *infilename = NULL;
char *outfilename = NULL;
int num_bytes = 0;
keyInstance key;
cipherInstance cipher;
unsigned char *input_block;
unsigned char *output_block;
unsigned char * whole_data;
int i;
int error=0;
int keyLen, blockLen;
char *keyMaterial;
char *iv;
if ((argc < 4) && (argc > 1)){
fprintf(stderr,"Usage: test_dec inputfile outputfile num_bytes\n");
exit(1);
}
else{
if(argc <2){
infilename = strdup("output.bin");
}
else{
infilename = strdup(argv[1]);
}
if(argc <3){
outfilename = strdup("deciphered.bin");
}
else{
outfilename = strdup(argv[2]);
}
if(argc <4){
num_bytes = FILE_SIZE;
}
else{
num_bytes = atoi(argv[3]);
}
}
printf("arguments= %s %s %d\n", infilename, outfilename, num_bytes);
if((inptr = fopen(infilename, "rb")) == NULL){
fprintf(stderr, "error opening file\n");
}
outptr = fopen(outfilename, "wb");
whole_data = (unsigned char *) malloc ( FILE_SIZE*sizeof(unsigned char));
output_block = (unsigned char *) malloc (FILE_SIZE*sizeof(unsigned char));
//key parameters
keyLen = 128;
keyMaterial = "abcdef0123456789abcdef0123456789";
//iv = "00000000000000000000000000000000";
iv = "000102030405060708090a0b0c0d0e0f";
blockLen = 128;
if(makeKeyEqvtInv(&key, DIR_DECRYPT , keyLen, blockLen, keyMaterial) != TRUE){
fprintf(stderr,"error in computing key\n");
exit(1);
}
if ((error = cipherInit (&cipher, MODE_CBC, iv, blockLen))!= TRUE) {
fprintf(stderr,"cipherInit error %d \n", error);
exit(1);
}
while (fread((void *)whole_data, sizeof(char), FILE_SIZE, inptr) == FILE_SIZE){
fprintf(stderr,"read file\n");
}
if(blockDecryptEqvtInv(&cipher, &key, whole_data, FILE_SIZE*8, output_block)<0){
fprintf(stderr, "cipher decryption error \n");
exit(1);
}
else{
fprintf(stderr,"Computed inverse cipher\n");
if(fwrite(output_block, sizeof(char), FILE_SIZE, outptr) != FILE_SIZE){
fprintf(stderr, "writing error\n");
exit(1);
}
fprintf(stderr,"Wrote file\n");
}
return 1;
}