summaryrefslogtreecommitdiffstats
path: root/onetimebluh.c
blob: 98d0d3b11065eafeb79c8280c54ebfdbdcd1867a (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
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#include<stdio.h>
#include<stdlib.h>
#include<getopt.h>

void xor(char* mess, char* keyf, char* outp);
void help(char* av[]);

int main(int argc, char* argv[]) {
	int opt = 1;
	int command = 0;
	int comm = 0;
	char* message = NULL;
	char* keyfile = NULL;
	char* output = NULL;
	while (opt) {
		int option_index = 0;
		static struct option options[] = {
			{"encrypt",	required_argument,	0,	'e'},
			{"key-file",	required_argument,	0,	'k'},
			{"key-gen",	no_argument,		0,	'g'},
			{"help",	no_argument,		0,	'h'},
			{"nbytes",	required_argument,	0,	'b'},
			{"output",	required_argument,	0,	'o'},
			{0,		0,			0,	0},
		};

		if ((opt = getopt_long(argc, argv, "b:e:ghk:o:", options, &option_index)) == -1)
			break;

		switch (opt) {
		case 'b':
			printf("Work in progress \n");
			break;
		case 'e':
			message = argv[optind-1];
			command++;
			comm = 'e';
			break;
		case 'g':
			printf("Work in progress \n");
			command++;
			comm = 'g';
			break;
		case 'h':
			help(argv);
			command++;
			break;
		case 'k':
			keyfile = argv[optind-1];
			break;
		case 'o':
			output = argv[optind-1];
			break;
		case '?':
			break;
		default:
			printf("carachter code returned 0%o \n", opt);
		}
	}

	if (command == 0) {
		printf("No command called \n");
		exit(EXIT_FAILURE);
	} else if (command > 1) {
		printf("Multiple commands called (print usage)\n");
		exit(EXIT_FAILURE);
	}

	if (optind < argc) {
		printf("Too many arguments \n");
		exit(EXIT_FAILURE);
	}

	if (comm == 'e') {
		if (keyfile == NULL) {
			printf("No key specified: exit! \n");
			exit(EXIT_FAILURE);
		}
		xor(message, keyfile, output);
	}

	exit(EXIT_SUCCESS);
}

void xor(char* mess, char* keyf, char* outp) {

	char* defoutp = "critt";

	if (outp == NULL) {
		printf("WARNING no output name specified using default value 'critt' \n");
		outp = defoutp;
	}
	FILE* mex = fopen(mess, "r");
	FILE* keyx = fopen(keyf, "r");
	FILE* critt = fopen(outp, "w");
	int i = 1;
	char a, b;

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

	fclose(mex);
	fclose(keyx);
	fclose(critt);

	printf("Message successfully encrypted \n");

	return;
}

void help(char* av[]) {
	printf("ONETIMEBLUH USAGE: \n");
	printf("%s [COMMAND] [OPTIONS] \n \n", av[0]);
	printf("COMMANDS: \n");
	printf("-e, --encrypt=FILE	encrypt message (input) \n");
	printf("-h, --help		print this help \n");
	printf("-g, --key-gen		create key file \n \n");
	printf("OPTIONS \n");
	printf("-k, --key-file=KEY_FILE	use key (input) \n");
	printf("-b, --nbytes=NUM	number of bytes \n");
	printf("-o, --output=FILE	output name \n \n");
	printf("Onetimebluh project repository at http://git.eigenlab.org/Seppia/onetimebluh \n");

	return;
}