-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDayata_Polyalphabeticv2.c
269 lines (236 loc) · 6.38 KB
/
Dayata_Polyalphabeticv2.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
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
/**
* CRYPTOGRAPHY SIMULATION: [3] Polyalphabetic Cipher
* CS 3106 (Information Assurance and Security)
* SUBMITTED BY: Wayne Dayata
* SUBMITTED TO: Mr. Godwin Monserate
* 11/13/2022
*
*******************************************************************************************
* NOTE TO USER: RUN THE .EXE FILE DIRECTLY AND DO NOT CHANGE THE CONETNTS OF THIS FILE. *
*******************************************************************************************
*/
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<ctype.h>
/********** A1. GLOBAL VARIABLES *******/
char fileName[100];
char outputName[100];
FILE* fileIn; //input file (to obtain ciplertext/plaintext)
FILE* fileOut; //output file (to store ciphertext/plaintext)
char* buffer; //area for character manipulation
int length; //character length of file
int mode; //1 = encrypt, 2 = decrypt
char key[100]; //encryption/decryption key
char ch;
/********** A2. FUNCTION PROTOTYPES *******/
void encrypt();
void decrypt();
void getFileName();
void getMode();
void getKey();
void openFile();
void readFromInputFile();
void getOutputFileName();
void writeToOutputFile();
void displayResultMessage();
void resetValues();
/********** B. MAIN FUNCTION *******/
int main(){
do{
//INITIALIZATION
system("cls"); // clear screen
resetValues();
//HEADER
printf("\n---------------------------------------------------------------------");
printf("\n------------- CRYPTOGRAPHY SIMULATION (Vigenere Cipher) -------------");
printf("\n-------------------- DEVELOPED BY: WAYNE DAYATA --------------------");
printf("\n---------------------------- 11/13/2022 -----------------------------");
printf("\n");
//INSTRUCTIONS
printf("\nInstructions for use:");
printf("\n- 1. Enter the file name");
printf("\n- 2. Select the mode (encrypt/decrypt)");
printf("\n- 3. Input your desired key (for encryption) or the key used (for decryption)");
printf("\n- 4. Check the output file generated.");
printf("\n");
//USER INPUTS
getFileName();
getMode();
getKey();
// PROCESS AND RESULT
readFromInputFile();
switch(mode){
case 1: encrypt(); break;
case 2: decrypt(); break;
}
getOutputFileName();
writeToOutputFile();
displayResultMessage();
// ASK FOR MAIN MENU OPTION
printf("\nPress any key to process another file");
printf("\nPress the 0 key to exit program");
ch = getch();
} while (ch != '0');
free(buffer);
}
/********** C. FUNCTIONS FOR ENCRYPTION AND DECRYPTION *******/
void encrypt(){
int x, ndx, keylen, offset, oldch, newch;
ndx=0;
keylen=strlen(key);
for(x=0;x<length;x++){
offset = key[ndx] - 'a';
if(isalpha(buffer[x])){
if(isupper(buffer[x])){
oldch = buffer[x] - 'A';
newch = (oldch+offset)%26;
buffer[x] = newch + 'A';
}else{
oldch = buffer[x] - 'a';
newch = (oldch+offset)%26;
buffer[x] = newch + 'a';
}
ndx = (ndx+1)%keylen;
}
}
}
void decrypt(){
int x, ndx, keylen, offset, oldch, newch;
ndx=0;
keylen=strlen(key);
for(x=0;x<length;x++){
offset = key[ndx] - 'a';
if(isalpha(buffer[x])){
if(isupper(buffer[x])){
oldch = buffer[x] - 'A';
newch = (oldch+26-offset)%26;
buffer[x] = newch + 'A';
}else if (islower(buffer[x])){
oldch = buffer[x] - 'a';
newch = (oldch+26-offset)%26;
buffer[x] = newch + 'a';
}
ndx = (ndx+1)%keylen;
}
}
}
/********** D. UTILITY FUNCTIONS *******/
void getFileName(){
printf("\n=== STEP 1: ===");
printf("\nEnter file name (including the extension).");
printf("\nExample: file1.txt");
printf("\n\nFILE NAME: ");
do{
//obtain file name
scanf("%s",fileName);
//check if file exists
openFile();
if(fileIn == NULL){
printf("\nSpecified file does not exist. Please input file name again.");
printf("\nFILE NAME: ");
}
}while(fileIn == NULL);
}
void getMode(){
printf("\n=== STEP 2: ===");
printf("\nFile obtained. Now enter the mode of operation.");
printf("\n[ 1 ] - Encrypt file");
printf("\n[ 2 ] - Decrypt file");
printf("\n\nMODE: ");
do{
//obtain mode
scanf("%d",&mode);
//check if mode input is correct
if(!(mode == 1 || mode == 2)){
printf("\nIncorrect input. Please try again.");
printf("\nMODE: ");
}
}while(!(mode == 1 || mode == 2));
}
void getKey(){
printf("\n=== STEP 3: ===");
int x;
if(mode==1){
printf("\nEnter an encryption key (Input only alphabetic characters, no spaces/numbers).");
}else{
printf("\nEnter the key for decryption (Input only alphabetic characters, no spaces/numbers).");
}
printf("\n\nINPUT KEY: ");
do{
//obtain key
scanf("%s",key);
//check if key input is correct - alphabet only
for(x=0;key[x]!='\0'&& isalpha(key[x])!=0; x++){}
if(key[x]!='\0' || x==0){
printf("\nIncorrect input. Please try again.");
printf("\nINPUT KEY: ");
}
}while(key[x]!='\0' || x==0);
//convert to lowercase
for(x=0;key[x]!='\0'; x++){
if(isupper(key[x])) key[x]-=32;
}
}
void openFile(){
//open the file
fileIn = fopen(fileName,"rb");
}
void readFromInputFile(){
//get file length
fseek(fileIn, 0, SEEK_END);
length = ftell(fileIn);
//set file pointer to beginning of file
fseek(fileIn, 0, SEEK_SET);
buffer = (char*)malloc((length+1)*sizeof(char));
if(buffer != NULL){
//transfer contents
fread(buffer, 1, length, fileIn);
}
buffer[length] = '\0';
fclose(fileIn);
}
void getOutputFileName(){
strcpy(outputName,fileName);
char *dot = strrchr(outputName, '.');
char fileExt[10];
strcpy(fileExt,dot);
if(mode==1){
strcpy(dot,"_encrypted");
}else{
strcpy(dot,"_decrypted");
}
strcat(outputName,fileExt);
}
void writeToOutputFile(){
//create new file
fileOut = fopen(outputName,"wb");
//write contents to file
length = strlen(buffer);
if(fileOut != NULL){
//transfer contents
fwrite(buffer,1,length,fileOut);
}
fclose(fileOut);
}
void displayResultMessage(){
char modeStr[10];
if(mode==1){
strcpy(modeStr,"encrypted");
}else{
strcpy(modeStr,"decrypted");
}
printf("\n=== STEP 4: ===");
printf("\nYour file has been %s.",modeStr);
printf("\nPlease check the output file named \"%s\"\n",outputName);
}
void resetValues(){
strcpy(fileName,"");
strcpy(outputName,"");
fileIn = NULL;
fileOut = NULL;
free(buffer);
length = 0;
mode = 0;
strcpy(key,"");
}