forked from rathoresrikant/HacktoberFestContribute
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Rail-fence.cpp
97 lines (88 loc) · 2.34 KB
/
Rail-fence.cpp
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
#include <iostream>
#include <cstring>
#include <string>
// Rails fence cipher - Cryptography
using namespace std;
string Encryptor(string msg, int key)
{
int msgLen = msg.size();
bool direction = true;
char enc[key][msgLen];
for (int i = 0; i < key; i++)
for (int j = 0; j < msgLen; j++)
enc[i][j] = '\n';
for (int i = 0, row = 0, col = 0; i < msgLen; i++)
{
if (row == 0 || row == key - 1)
direction = !direction;
enc[row][col++] = msg[i];
direction ? row-- : row++;
}
string cipher;
for (int i = 0; i < key; i++)
for (int j = 0; j < msgLen; j++)
if (enc[i][j] != '\n')
cipher.push_back(enc[i][j]);
return cipher;
}
string Decryptor(string cipher, int key)
{
int cipLen = cipher.size();
bool direction;
char dec[key][cipLen];
for (int i = 0; i < key; i++)
for (int j = 0; j < cipLen; j++)
dec[i][j] = '\n';
for (int i = 0, row = 0, col = 0; i < cipLen; i++)
{
if (row == 0)
direction = false;
else if (row == key - 1)
direction = true;
dec[row][col++] = '*';
direction ? row-- : row++;
}
int index = 0;
for (int i = 0; i < key; i++)
for (int j = 0; j < cipLen; j++)
if (dec[i][j] == '*' && index < cipLen)
dec[i][j] = cipher[index++];
string msg;
for (int i = 0, row = 0, col = 0; i < cipLen; i++)
{
if (row == 0)
direction = false;
else if (row == key - 1)
direction = true;
if (dec[row][col] != '*')
msg.push_back(dec[row][col++]);
direction ? row-- : row++;
}
return msg;
}
int main()
{
int key, choice;
std::string cip, msg;
cout << "1. Encrypt \n2. Decrypt \n>>";
cin >> choice;
if (choice == 1)
{
cout << "Enter Message\n>>";
cin.ignore();
std::getline(std::cin, msg);
cout << "Enter Key\n>>";
cin >> key;
cout << "Encrypted Message\n>>" << Encryptor(msg, key);
}
else if (choice == 2)
{
cout << "Enter Encrypted Message\n>>";
cin.ignore();
std::getline(std::cin, cip);
cout << "Enter Key\n>>";
cin >> key;
cout << "Original Message\n>>" << Decryptor(cip, key);
}
return 0;
}