-
Notifications
You must be signed in to change notification settings - Fork 368
/
ColumnarTranspositionCipher.c
149 lines (126 loc) · 3 KB
/
ColumnarTranspositionCipher.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
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#define MAX_SIZE 100
// Key for Columnar Transposition
const char key[] = "HACK";
int keyMap[MAX_SIZE];
void setPermutationOrder()
{
// Add the permutation order into the map
for (int i = 0; i < strlen(key); i++)
{
keyMap[key[i]] = i;
}
}
// Encryption
void encryptMessage(char msg[], char cipher[])
{
int row, col, j;
char matrix[MAX_SIZE][MAX_SIZE] = {'\0'};
// Calculate the number of columns in the matrix
col = strlen(key);
// Calculate the maximum number of rows in the matrix
row = strlen(msg) / col;
if (strlen(msg) % col != 0)
{
row += 1;
}
int k = 0;
for (int i = 0; i < row; i++)
{
for (j = 0; j < col;)
{
if (msg[k] == '\0')
{
// Adding the padding character '_'
matrix[i][j] = '_';
j++;
}
if (isalpha(msg[k]) || msg[k] == ' ')
{
// Adding only space and alphabets into the matrix
matrix[i][j] = msg[k];
j++;
}
k++;
}
}
int index = 0;
for (int l = 0; key[l] != '\0'; l++)
{
keyMap[key[l]] = index++;
}
k = 0;
for (int l = 0; key[l] != '\0'; l++)
{
j = keyMap[key[l]];
for (int i = 0; i < row; i++)
{
if (isalpha(matrix[i][j]) || matrix[i][j] == ' ' || matrix[i][j] == '_')
{
cipher[k++] = matrix[i][j];
}
}
}
cipher[k] = '\0';
}
// Decryption
void decryptMessage(char cipher[], char decryptedMsg[])
{
int col = strlen(key);
int row = strlen(cipher) / col;
char cipherMat[MAX_SIZE][MAX_SIZE];
int k = 0;
for (int j = 0; j < col; j++)
{
for (int i = 0; i < row; i++)
{
cipherMat[i][j] = cipher[k++];
}
}
int index = 0;
for (int l = 0; key[l] != '\0'; l++)
{
keyMap[key[l]] = index++;
}
char decCipher[MAX_SIZE][MAX_SIZE];
int l = 0, j;
for (; key[l] != '\0';)
{
j = keyMap[key[l++]];
for (int i = 0; i < row; i++)
{
decCipher[i][j] = cipherMat[i][l - 1];
}
}
// Getting message using matrix
k = 0;
for (int i = 0; i < row; i++)
{
for (int j1 = 0; j1 < col; j1++)
{
if (decCipher[i][j1] != '_')
{
decryptedMsg[k++] = decCipher[i][j1];
}
}
}
decryptedMsg[k] = '\0';
}
int main()
{
char msg[MAX_SIZE];
char cipher[MAX_SIZE];
char decryptedMsg[MAX_SIZE];
printf("Enter the Plain Text: ");
scanf("%[^\n]s", msg);
setPermutationOrder();
// Calling encryption function
encryptMessage(msg, cipher);
printf("Encrypted Message: %s\n", cipher);
// Calling Decryption function
decryptMessage(cipher, decryptedMsg);
printf("Decrypted Message: %s\n", decryptedMsg);
return 0;
}