-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpolyalphabetic_substitution_ciphers_file.c
155 lines (126 loc) · 3.55 KB
/
polyalphabetic_substitution_ciphers_file.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
/*******
Date: 29/6/2017
This program is written for fun only.
It accepts a text file and a pad as arguments. It outputs a new file with all characters
encrypted or decrypted with the pad provided. Encryption and decryption are done in the
same program, decided by the parameter. The output of the program will be in the format
of <input_file_name>_output(.<file_extension>).
Only encrypt and decrypt English alphabets.
Usage: polyalphabetic_substitution_ciphers_file <file_name> <pad> <e/d>
e -> encryption d -> decryption
*******/
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
char* to_upper_case(char* inp) {
int len = strlen(inp);
char* ret = calloc(len + 1, sizeof(char));
for (int i = 0; i < len; i++)
ret[i] = toupper(inp[i]);
return ret;
}
char* en(char* inp, char* _pad) {
int i = 0, k = 0;
char* ret = malloc(strlen(inp) + 1);
char* plaintext = to_upper_case(inp);
char* pad = to_upper_case(_pad);
for (; plaintext[i]; i++) {
if (!isalpha(plaintext[i])) {
ret[i] = plaintext[i];
continue;
}
int c;
c = (plaintext[i] - 'A') + (pad[k] - 'A') + 1;
c = (c % 26) + 'A'; // 'a' for lowercase output
ret[i] = c;
k = (k + 1) % strlen(pad);
}
free(plaintext);
free(pad);
return ret;
}
char* de(char* inp, char* _pad) {
int i = 0, k = 0;
char* ret = malloc(strlen(inp) + 1);
char* plaintext = to_upper_case(inp);
char* pad = to_upper_case(_pad);
for (; plaintext[i]; i++) {
if (!isalpha(plaintext[i])) {
ret[i] = plaintext[i];
continue;
}
int c;
c = (plaintext[i] - 'A') - (pad[k] - 'A') - 1;
if (c < 0) c += 26; // **CRITICAL**
c = (c % 26) + 'A'; // 'a' for lowercase output
ret[i] = c;
k = (k + 1) % strlen(pad);
}
free(plaintext);
free(pad);
return ret;
}
void display_msg() {
printf("Usage: polyalphabetic_substitution_ciphers_file <filename> <pad> <e/d>\n");
printf("e -> encrypt\td -> decrypt\n");
printf("e.g.\n$ polyalphabetic_substitution_ciphers_file ~/Documents/test myPad e\n");
}
int main(int argc, char* argv[]) {
printf("++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n\n");
if (argc == 1) {
display_msg();
exit(1);
}
if (argc != 4) {
printf("You did not input enough information or you inputted too much.\n");
printf("For more information, run without parameters\n");
exit(1);
}
// Create output file name //
char output_path[256] = "";
char ext[256] = "";
strcpy(output_path, argv[1]);
if (strchr(output_path, '.')) { // If the file has file extension
char* token = strtok(output_path, ".");
sprintf(output_path, "%s", token);
token = strtok(NULL, ".");
strcpy(ext, token);
strcat(output_path, "_output");
strcat(output_path, ".");
strcat(output_path, ext);
free(token);
}
else
sprintf(output_path, "%s_output", argv[1]);
/*************************/
FILE *fp = fopen(argv[1], "r");
FILE *op = fopen(output_path, "w");
if (!fp) {
printf("The file cannot be opened.\n");
exit(1);
}
if (!op) {
printf("The output file cannot be created.\n");
exit(1);
}
char* get = malloc(1024 * sizeof(char));
if (argv[3][0] == 'e') {
while (fgets(get, 1024, fp))
fprintf(op, "%s", en(get, argv[2]));
printf("Encryption success.\n");
printf("Output: %s\n", output_path);
}
else if (argv[3][0] == 'd') {
while (fgets(get, 1024, fp))
fprintf(op, "%s", de(get, argv[2]));
printf("Decryption success.\n");
printf("Output: %s\n", output_path);
}
else
printf("Please enter 'e' for encryption or 'd' for decryption.\n");
free(get);
fclose(fp);
fclose(op);
return 0;
}