summaryrefslogtreecommitdiffstats
path: root/rosicchio.c
blob: ffd3ee88623ec425e1aa8b87818d6c35b4ebd438 (plain)
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
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

int main(int argc, char* argv[]) {

	FILE* mess = fopen(argv[1], "r");
	FILE* pad = fopen(argv[2], "r+");
	FILE* critt = fopen(argv[3],"w");
	fseek(mess, 0L, SEEK_END);
	long mess_size = ftell(mess);
	rewind(mess);
	fseek(pad, 0L, SEEK_END);
	long pad_size = ftell(pad);
	fseek(pad, (pad_size - mess_size), SEEK_SET);
	int i = 1;
	char a, b;

	while(i != EOF) {
		i = fscanf(mess, "%c", &a);
		fscanf(pad, "%c", &b);
		if(i != EOF) {
			fprintf(critt, "%c", a^b);
		}
	}

	/* The truncate function "rosicchia" la parte di chiave utilizzata */

	ftruncate(fileno(pad), (pad_size - mess_size));
	fseek(pad, 0L, SEEK_END);
	long new_pad_size = ftell(pad);

	fclose(mess);
	fclose(pad);
	fclose(critt);

	printf("Message length is %li bytes \n", mess_size);
	printf("Pad length was %li bytes \n", pad_size);
	printf("Pad length is now %li bytes \n", new_pad_size);
	printf("Pad length should now be %li bytes \n", (pad_size - mess_size));

	exit(EXIT_SUCCESS);
}