-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathencryption.cpp
More file actions
71 lines (67 loc) · 1.27 KB
/
encryption.cpp
File metadata and controls
71 lines (67 loc) · 1.27 KB
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
#include<stdio.h>
#include<string.h>
#include<ctype.h>
void encrypt(int l, int col, char* msg, char* enmsg);
int findRow(int col, int l);
int main(int argc, char *argv[])
{
if (argc < 3)
{
printf("Usage:./(program_name) K \"message\" \n");
return 1;
}
char *c = argv[1];
char *msg = argv[2];
int l = strlen(msg);
int col = *c - '0';
int row = findRow(col, l);
char enmsg[(row*col) + 1];
encrypt(l, col, msg, enmsg);
printf("Encrypted Message : ");
printf("%s\n", enmsg);
return 0;
}
int findRow(int col, int l)
{
int row;
if(l%(col) == 0)
{
row = l/(col);
}
else
{
row = (l/(col)) + 1;
}
return row;
}
void encrypt(int l, int col, char* msg, char* enmsg)
{
int row = findRow(col, l);
int fl = row * col;
char en[fl + 1];
for(int i = 0; i < l; i++)
{
if(isspace(msg[i]))
{
en[i] = '*';
}
else
{
en[i] = msg[i];
}
}
for(int i = l ; i < fl; i++)
{
en[i] = '$';
}
int k = 0;
for(int i = 0; i < col; i++)
{
for(int j = row-1; j >= 0; j--)
{
enmsg[k] = en[i + (j*col)];
k++;
}
}
enmsg[fl] = '\0';
}