-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patha2encrypt.c
More file actions
196 lines (149 loc) · 5.45 KB
/
a2encrypt.c
File metadata and controls
196 lines (149 loc) · 5.45 KB
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
//Cis3090 A1
//Student Numbers: 1271673 & 1278122
//Names: Betty Vuong & Justin Ivanovski
#include <stdbool.h>
#include <limits.h>
#include <float.h>
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <unistd.h>
#include <sys/time.h>
#include <string.h>
#include <strings.h>
#include <math.h>
#include <semaphore.h>
#include <ctype.h>
char * input_dictionary;
char * encrypt_dictionary;
//Could also just not pass input_dictionary in and leave it global not sure
int input_check(char char_to_insert);
int insert_to_dictionary(char char_to_insert);
int randomize_dictionary();
int encrypt_string(char * str);
int main(int argc, char *argv[]){
//gets the string to encrypt from command line
if(argv==NULL){
printf("Command line argument issue");
return 1;
}
//checks if it can be encrypted, can not encrypt a single character
char * str = argv[1];
if(strlen(str)<=1){
return 1;
}
//makes the string lowercase and populates input dictionary string
for(int i = 0;i<strlen(str);i++){
str[i] = tolower(str[i]);
int check = input_check(str[i]);
if(check==1){
insert_to_dictionary(str[i]);
}
}
//shuffles the input dictionary
randomize_dictionary();
//encrypts the string based on the encrypt dictionary
encrypt_string(str);
return 0;
}
//check if the input dictionary exists and iterate through it to make sure a character isn't in it
//before adding new unique character
int input_check(char char_to_insert){
int charFound = 1;
if(input_dictionary == NULL){
return 1;
}
for(int i = 0;i<strlen(input_dictionary);i++){
if(char_to_insert==input_dictionary[i]){//if the character is already in the input dict
charFound = 0;
}
}
return charFound;
}
//populates the dictionary
int insert_to_dictionary(char char_to_insert){
//ensures its a valid character
if(!isalpha(char_to_insert) && char_to_insert !='\''){
return -1;
}else if(isspace(char_to_insert)){
//Do not insert spaces, but not necessarily a fail
return 0;
}
char_to_insert = tolower(char_to_insert);
//reallocate space for input dictionary to allow for the new character, insert it, then null terminate
char * temp;
if(input_dictionary==NULL){//if the dictionary does not exist yet
input_dictionary = malloc(sizeof(char)*(2));
// strcat(input_dictionary,char_to_insert);
input_dictionary[0] = char_to_insert;
input_dictionary[1] = '\0';
return 1;
}else{//otherwise add more sapce
temp = malloc (sizeof(char)*(strlen(input_dictionary)+2));
}
//Make input dictionary into the updated temp dictionary
int len = strlen(input_dictionary);
temp = strcpy(temp,input_dictionary);
temp[len] = char_to_insert;
temp[len + 1] = '\0';
free(input_dictionary);
input_dictionary = temp;
// temp = strcat(temp,char_to_insert);
return 1;
}
//Shuffles the characters in the input dictionary to create the encrypt dictionary
int randomize_dictionary(){
srand(time(NULL));//to generate random num
int len = strlen(input_dictionary);
if(encrypt_dictionary==NULL){//malloc encrypt dictionary if it does not exist
encrypt_dictionary = malloc(sizeof(char) * (len+1));
}
//set encrypt dict to input dict
strcpy(encrypt_dictionary,input_dictionary);
//iterate through the encrypt dict
for(int i = 0; i < len;i++){
int j = rand() % (i + 1);//generate random index
//swap the current char with the char at the random index
char temp = encrypt_dictionary[i];
encrypt_dictionary[i] = encrypt_dictionary[j];
encrypt_dictionary[j] = temp;
}
return 1;
}
//encrypts the string based on the encrypt dictionary
int encrypt_string(char * str){
//creates temp str to encrypt
int len = strlen(str);
char * temp = malloc(sizeof(char) * (len+1));
temp[len] = '\0';
do {
randomize_dictionary();//randomizes the input dictionary to create the encrypt dictionary
//iterates through the string
for(int i = 0; i < len; i++){
if(isspace(str[i])){//leaves spaces where they are
temp[i] = ' ';
} else {
//if its not a space, it attempts to match the current char with one from the input dictionary at index j
//then swaps the character in the string with the character at j in the encrypt dictionary
for(int j = 0; j < strlen(input_dictionary); j++){
if(str[i] == input_dictionary[j]){
temp[i] = encrypt_dictionary[j];
break;
}
}
}
}
} while(strcasecmp(temp, str) == 0); //if the encrypt dictionary ends up the same as the input dictionary and creates the same string, repeat encryption process
//write the output to the ciphertext file
FILE *fp = fopen("ciphertext.txt", "w");
if (fp == NULL) {
perror("Failed to open file");
return 1;
}
// write to the file
fprintf(fp, "%s",temp);
// close the file
fclose(fp);
printf("%s\n",temp);
return 1;
}