-
Notifications
You must be signed in to change notification settings - Fork 0
/
decrypt.c
68 lines (53 loc) · 1.44 KB
/
decrypt.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include "rsa.h"
#define KEY_LEN 64
#define BLOCK_LEN 32
void print_buff(int len, char *buf)
{
int i;
for (i = 0; i < len; i++) {
printf("%02x", (unsigned char) buf[i]);
}
}
size_t sizeoffile(const char *fname)
{
struct stat sbuf;
int rc = stat(fname, &sbuf);
if (rc <0) {
printf("Error -couldnot stat file\n");
exit(-1);
}
return sbuf.st_size;
}
int main(int argc, char **argv)
{
if (argc != 3) {
printf("Error - run as decrypt private_fname encrypted_fname\n");
exit(0);
}
FILE *fp = fopen(argv[2],"r+");
if (fp == NULL) {
perror("could not open encrypted text");
exit(-1);
}
size_t fsize = sizeoffile(argv[2]);
char *decrypted = malloc(fsize*2);
char *encrypted = malloc(fsize*2);
memset(decrypted, 0, fsize*2);
memset(encrypted, 0, fsize*2);
int bytes = fread(encrypted, 1, fsize, fp);
printf("Read %d bytes\n", bytes);
fclose(fp);
rsa_keys_t keys;
rsa_read_private_keys(&keys,argv[1]);
printf("Encrypted: ("); print_buff(bytes, encrypted); printf(")\n");
int dec_len = rsa_decrypt(encrypted, decrypted, bytes, &keys);
printf("Decrypted: ("); print_buff(dec_len, decrypted); printf(")\n");
decrypted[dec_len] = 0x00;
printf("%s\n", decrypted);
}