-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdecrypt.c
More file actions
76 lines (61 loc) · 2.11 KB
/
decrypt.c
File metadata and controls
76 lines (61 loc) · 2.11 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
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <string.h>
#include <openssl/conf.h>
#include <openssl/evp.h>
#include <openssl/err.h>
#define BLOC 16
void cbc(unsigned char* key, unsigned char* iv, unsigned char* data, int size, unsigned char* res){
EVP_CIPHER_CTX *ctx;
int len;
assert(ctx = EVP_CIPHER_CTX_new());
assert(EVP_DecryptInit_ex(ctx, EVP_aes_128_cbc(), NULL, key, iv));
assert(EVP_DecryptUpdate(ctx, res, &len, data, size));
}
void ecb(unsigned char* key, unsigned char* data, unsigned char* res){
EVP_CIPHER_CTX *ctx;
int len;
assert(ctx = EVP_CIPHER_CTX_new());
assert(EVP_DecryptInit_ex(ctx, EVP_aes_128_ecb(), NULL, key, NULL));
assert(EVP_DecryptUpdate(ctx, res, &len, data, BLOC));
}
int main(int argc, char **argv){
int len;
unsigned char key[BLOC];
unsigned char iv[BLOC];
EVP_CIPHER_CTX *ctx;
unsigned char res;
FILE *fi = fopen(argv[1], "rb");
FILE *fo = fopen(argv[2], "wb");
fseek(fi, 0, SEEK_END);
long fsize = ftell(fi);
long dsize = fsize - 2*BLOC;
fseek(fi, 0, SEEK_SET);
fread(key, sizeof(char), BLOC, fi);
fread(iv, sizeof(char), BLOC, fi);
unsigned char data[dsize];
fread(data, sizeof(char), dsize, fi);
if((dsize % BLOC) > 0){
int pad = BLOC - (dsize % BLOC);
unsigned char secondtolast[BLOC];
unsigned char d_secondtolast[BLOC];
unsigned char datapad[dsize+pad];
memcpy(&secondtolast, &data[dsize-2*BLOC+pad], BLOC);
ecb(key, secondtolast, d_secondtolast);
memcpy(&datapad, &data, dsize-2*BLOC+pad);
memcpy(&datapad[dsize-2*BLOC+pad], &data[dsize-BLOC+pad], BLOC-pad);
memcpy(&datapad[dsize-BLOC], &d_secondtolast[BLOC-pad], pad);
memcpy(&datapad[dsize-BLOC+pad], &data[dsize-2*BLOC+pad], BLOC);
unsigned char res[dsize+pad];
cbc(key,iv, datapad, dsize+pad, res);
fwrite(res, sizeof(char), dsize+pad, fo);
}
else{
unsigned char res[dsize];
cbc(key,iv, data, dsize, res);
fwrite(res, sizeof(char), dsize, fo);
}
fclose(fi);
fclose(fo);
}