-
Notifications
You must be signed in to change notification settings - Fork 0
/
pa01.c
99 lines (74 loc) · 2.21 KB
/
pa01.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
#include <stdio.h>
#include <ctype.h>
void hillCipher(char *plain, int matrix[][9], int index, int size){
int block[size];
char cipherText[10000];
int cipherIndex = 0;
for(int i = 0; i < index; i += size){
for(int j = 0; j < size; j++){
block[j] = plain[i + j] - 'a';
}
for(int k = 0; k < size; k++){
int sum = 0;
for(int l = 0; l < size; l++){
sum += matrix[k][l] * block[l];
}
cipherText[cipherIndex++] = (sum % 26) + 'a';
}
}
cipherText[cipherIndex] = '\0';
printf("Ciphertext: \n");
for(int i = 0; i < cipherIndex; i++){
printf("%c", cipherText[i]);
// Every 80 letters in a row, it is required to make a new line
if((i + 1) % 80 == 0){
printf("\n");
}
}
printf("\n");
}
int main(int argc, char *argv[]){
int size = 9;
FILE *key;
int matrix[size][size]; // Supports matrix sizes from 2x2 up to 9x9
// Open the key file
key = fopen("k1.txt", "r");
fscanf(key, "%d", &size);
printf("Key matrix:\n");
for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
fscanf(key, "%d", &matrix[i][j]);
// Using 4d as for the integer format required
printf("%4d", matrix[i][j]);
}
printf("\n");
}
fclose(key);
printf("\n");
FILE* plain;
plain = fopen("p1.txt", "r");
char string[10000];
int index = 0;
char character;
while((character = fgetc(plain)) != EOF){
if(isalpha(character)){
string[index++] = tolower(character);
}
}
fclose(plain);
// Padding the last part of the buffer with x
while(index % size != 0){
string[index++] = 'x';
}
// Null terminator indicates the end of the string
string[index] = '\0';
printf("Plaintext: ");
for(int i = 0; i < index; i++){
printf("%c", string[i]);
if((i + 1) % 80 == 0){
printf("\n");
}
}
printf("\n\n");
hillCipher(string, matrix, index, size);
}