summaryrefslogtreecommitdiff
path: root/encrypt.c
blob: 172fbbd1b7933b1c60f8b80e57c2340549afa5b9 (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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <time.h>

int main(int argc, char** argv) {
	char * junk;
	/* We need a file to encrypt */
	if(argc==1) {
		fprintf(stderr, "Arguments fool!");
		return 1;
	}
	/* Now lets open that file */
	FILE* file = fopen(argv[1], "r");
	/* And a temporary file to hold the encrypted data */
	char tpath[255] = "/tmp/";
	char timeS[32];
	sprintf(timeS, "%d", (int) time(NULL));
	strcat(tpath, timeS);
	FILE* temp = fopen(tpath, "w");
	
	char passphrase[255];
	do {
		/* Now we need the passphrase. Get it */
		printf("Enter Passphrase:\n");
		junk = fgets(passphrase, 255, stdin);
	} while (strlen(passphrase) <= 0);
	
	int passlen = strlen(passphrase) - 1;
	int c,j=0;
	/* Now we encrypt! */
	while((c=fgetc(file)) != EOF) {
	if(j==passlen) {
			fputc(c ^ passphrase[0], temp);
			j=1;
		} else {
			fputc(c ^ passphrase[j], temp);
			j++;
		}
	}
	/* Close the files */
	fflush(temp);
	fclose(temp);
	fclose(file);
	/* Move the file back */
	char cmd[255];
	sprintf(cmd, "%s %s %s", "/bin/mv", tpath, argv[1]);
	if (system(cmd) != 0) {
		fprintf(stderr, "Your encrypted file is located in %s.", tpath);
		return EXIT_FAILURE;
	}
	return EXIT_SUCCESS;
}