-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcolumnar.c
156 lines (133 loc) · 2.03 KB
/
columnar.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
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<conio.h>
#include<string.h>
int main()
{
char pt[100], ct[100];
char temp[10][10], inter[10][10];
int key[10], ch, i, j, l, lp, row, k, z;
printf("\n1]Sender\n2] Receiver\nEnter the choice:");
scanf("%d", &ch);
if (ch == 1)
{
printf("\nEnter Plain Text:");
scanf("%s", pt);
lp = strlen(pt);
printf("\nEnter length of key:");
scanf("%d", &l);
printf("\nEnter key:");
for (i = 0; i < l; i++)
{
scanf("%d", &key[i]);
}
if (lp%l != 0)
{
while (lp%l != 0)
{
lp++;
pt[lp - 1] = 'x';
}
}
row = lp / l;
k = 0;
for (i = 0; i < row; i++)
{
for (j = 0; j < l; j++)
{
temp[i][j] = pt[k];
printf(" %c ", pt[k]);
k++;
}
printf("\n");
}
for (i = 0; i < lp; i++)
{
for (j = 1; j <= l; j++)
{
k = 0;
while (key[k] != j)
{
k++;
}
for (z = 0; z < row; z++)
{
ct[i] = temp[z][k];
i++;
}
}
}
printf("\nCipher:");
for (i = 0; i < lp; i++)
{
printf("%c ", ct[i]);
}
}
else if (ch == 2)
{
printf("\nEnter cipher text:");
scanf("%s", ct);
lp = strlen(ct);
printf("\nEnter length of key:");
scanf("%d", &l);
printf("\nEnter the key:");
for (i = 0; i < l; i++)
{
scanf("%d", &key[i]);
}
row = lp / l;
k = 0;
for (i = 0; i < lp; i++)
{
for (j = 0; j < l; j++)
{
for (z = 0; z < row; z++)
{
temp[z][j] = ct[i];
i++;
}
}
}
for (i = 0; i < row; i++)
{
for (j = 0; j < l; j++)
{
printf("%c", temp[i][j]);
}
printf("\n");
}
for (k = 0; k < row; k++)
{
for (i = 1; i <= l; i++)
{
for (j = 0; j < l; j++)
{
if (key[j] == i)
{
inter[k][j] = temp[k][i - 1];
}
}
}
}
z = 0;
for (i = 0; i < row; i++)
{
for (j = 0; j < l; j++)
{
pt[z] = inter[i][j];
z++;
}
}
printf("\nPlain text:");
for (i = 0; i < lp; i++)
{
printf("%c", pt[i]);
}
}
else
{
printf("\nInvalid choice......");
}
getchar();
return 0;
}