-
Notifications
You must be signed in to change notification settings - Fork 2
/
enc and decry using columnar transpostion.c
111 lines (106 loc) · 2.42 KB
/
enc and decry using columnar transpostion.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
// Encryption and Decryption using columnar transposition //
#include<stdio.h>
#include<conio.h>
void main()
{
// Declaration of variables and arrays
char pt[60]; // Plain text
char newpt[60] = {"\0"}; // Modified plain text (without spaces)
char et[60] = {"\0"}; // Encrypted text
char newet[60] = {"\0"}; // Modified encrypted text (without spaces)
char dt[60] = {"\0"}; // Decrypted text
char mat[12][5]; // Matrix for columnar transposition
int row, column = 5, len, i, j, newlen, k = 0, m;
// Clearing the console screen
clrscr();
// Input of plain text
printf("\n\nEnter Plain Text:");
gets(pt);
// Remove spaces from plain text
len = strlen(pt);
for(i=0; i<len; i++)
{
if(pt[i] != 32)
30
{
newlen = strlen(newpt);
newpt[newlen] = pt[i];
}
}
// Calculate the number of rows in the matrix
// Getting Size of Row //
newlen - strlen(newpt);
row = newlen/column;
if(newlen % column > 0)
row = row + 1;
// Create matrix from plaintext //
printf("\nColumner Matrix is:");
for(i = 0; i < row; i++)
{
for(j = 0; j < column; j++)
{
if(k < newlen)
{
mat[i][j] = newpt[k];
printf("%2c",newpt[k]);
k++;
}else{
mat[i][j] = 32;
}
}
printf("\n");
}
// Encryption Code //
k = 0;
for(m = 0; m < column; m += 2)
{
for(i = 0; i < row; i++)
{
et[k] = mat[i][m];
k++;
}
}
for(m = 1; m < column; m += 2)
{
for(i = 0; i < row; i++)
{
et[k] = mat[i][m];
k++;
}
31
}
// Remove blank spaces from encrypted text
// Blank space Remove Code //
newlen = strlen(et);
for(i = 0; i < newlen; i++)
{
if(et[i] != 32)
{
len = strlen(newet);
newet[len] = et[i];
}
}
printf("\n\nEncrypted Text Is: %s", newet);
// Decryption Code //
k = 0;
for(i = 0; i < row; i++)
{
m = i;
for(j = 0; j < column; j++)
{
if(j%2 == 0)
{
dt[k] = et[m];
m = m + (row * 3);
k++;
}
else{
dt[k] = et[m];
m = m - (row * 2);
k++;
}
}
}
printf("\n\n Decrypted Text Is: %s", dt);
getch();
}