-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcolumnar_transposition_cipher.c
264 lines (210 loc) · 6.24 KB
/
columnar_transposition_cipher.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
char *get_key_letters(const char *key, int *count)
{
int letter_count = 0;
int i, j;
// count the number of alphanumeric characters in the key
for (i = 0; i < strlen(key); i++)
{
if (isalnum(key[i]))
{
letter_count++;
}
}
// allocate memory to store all the alphanumeric characters in the key
char *key_letters = malloc((letter_count + 1) * sizeof(*key_letters));
if (key_letters == NULL)
{
fprintf(stderr, "Error allocating memory for key\n");
return NULL;
}
// add each alphanumeric character form the key to key_letters
j = 0;
for (i = 0; i < strlen(key); i++)
{
if (isalnum(key[i]))
{
key_letters[j++] = key[i];
}
}
key_letters[j] = '\0';
*count = letter_count;
return key_letters;
}
char *get_file_letters(const char *filename, int *count)
{
FILE *file;
if ((file = fopen(filename, "r")) == NULL)
{
perror("Error opening file");
return NULL;
}
int letter_count = 0;
char c;
// count the number of alphanumeric characters in the file
while ((c = fgetc(file)) != EOF)
{
if (isalnum(c))
{
letter_count++;
}
}
// allocate memory to store all the alphanumeric characters in the file
char *file_letters = malloc((letter_count + 1) * sizeof(*file_letters));
if (file_letters == NULL)
{
fprintf(stderr, "Error allocating memory for file characters\n");
fclose(file);
return NULL;
}
rewind(file); // go back to the beginning of the file
// add all alphanumeric characters from the file to the string
int i = 0;
while ((c = fgetc(file)) != EOF)
{
if (isalnum(c))
{
file_letters[i++] = c;
}
}
file_letters[i] = '\0';
fclose(file);
*count = letter_count;
return file_letters;
}
void pad_string(char **file_letters, int *letter_count, int pad_size)
{
char file_letters_no_padding[*letter_count];
strcpy(file_letters_no_padding, *file_letters);
*letter_count += pad_size; // update the number of letters to account for the padding
*file_letters = realloc(*file_letters, (*letter_count + 1) * sizeof(**file_letters));
int i;
for (i = *letter_count - pad_size; i < *letter_count; i++)
{
(*file_letters)[i] = 'X';
}
(*file_letters)[i] = '\0';
}
void encrypt(const char *filename, const char *key, char **result)
{
int letter_count_key; // number of alphanumeric characters in the key
int letter_count_file; // number of alphanumeric characters in the message
char *key_letters = get_key_letters(key, &letter_count_key);
if (key_letters == NULL)
{
*result = NULL;
return;
}
char *file_letters = get_file_letters(filename, &letter_count_file);
if (key_letters == NULL)
{
*result = NULL;
free(key_letters);
key_letters = NULL;
return;
}
// calculate the number of rows and columns required to store the message and the key in a 2D array
int rows = (int)((letter_count_file / strlen(key_letters)) + 1); // add one for the key
int columns = (int)strlen(key_letters);
// calculate the number of X characters that will be needed as padding
int X_number = columns - (letter_count_file % columns);
if (letter_count_file % columns != 0)
{
rows++; // if the message does not fit perfectly in the grid, increment the number of rows
pad_string(&file_letters, &letter_count_file, X_number);
}
else
{
X_number = 0; // if the message does fit, then there will be no X characters needed for padding
}
char table[rows][columns];
int i, j, k;
int swap_made = 0;
// add the key to the top row of the grid
for (i = 0; i < columns; i++)
{
table[0][i] = key_letters[i];
}
// add all other letters to the grid
k = 0;
i = 1;
while (k < letter_count_file)
{
for (j = 0; j < columns; j++)
{
table[i][j] = file_letters[k];
k++;
}
i++;
}
// sort the key using a bubble sort and rearrange the columns
for (k = 0; k < columns - 1; k++)
{
swap_made = 0;
for (j = 0; j < columns - k - 1; j++)
{
if (table[0][j] >= table[0][j + 1])
{
for (i = 0; i < rows; i++)
{
// swap the characters of the key and all the characters in each column
// of the characters being swapped
char temp = table[i][j];
table[i][j] = table[i][j + 1];
table[i][j + 1] = temp;
swap_made = 1;
}
}
}
// exit the bubble sort if no swaps are made
if (swap_made == 0)
{
break;
}
}
// re-populate the file_letters array with the new encrypted message
k = 0;
for (i = 1; i < rows; i++)
{
for (j = 0; j < columns; j++)
{
file_letters[k++] = table[i][j];
}
}
file_letters[k] = '\0'; // put the null terminator on the end of the string
// allocate memory for the result
// if the allocation is successful, copy the encrypted message to result
*result = malloc((letter_count_file + 1) * sizeof(**result));
if (*result != NULL)
{
strcpy(*result, file_letters);
}
free(key_letters);
key_letters = NULL;
free(file_letters);
file_letters = NULL;
}
int main(int argc, char *argv[])
{
if (argc != 3)
{
fprintf(stderr, "\nInvalid arguments passed\n");
fprintf(stderr, "\nUsage:\n");
fprintf(stderr, "\tcolumnar_transposition_cipher <text_file_name> <key_for_cipher>\n\n");
exit(EXIT_FAILURE);
}
const char *filename = argv[1];
const char *key = argv[2];
char *encrypted_text = NULL;
encrypt(filename, key, &encrypted_text);
if (encrypted_text != NULL)
{
printf("%s\n", encrypted_text);
}
free(encrypted_text);
encrypted_text = NULL;
return EXIT_SUCCESS;
}