-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHash.cpp
139 lines (108 loc) · 2.6 KB
/
Hash.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
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
#include "Hash.h"
#include <vector>
//The hashing function
using namespace std;
string Hash::EncryptMessage(std::string message)
{
//creating empty matrix
const int messageLenght = message.length(); //Lets say the value is 11
//Creates a vector with two parameters of char like so v[][]
vector<vector<char>> v;
v.resize(key);
for(int i = 0; i < key; i++)
{
v[i].resize(messageLenght);
}
//Fills up the vectors with empty spaces to distinguish them easier
for (int a = 0; a < key; a++)
{
for (int b = 0; b < messageLenght; b++)
{
v[a][b] = '\n';
}
}
//Checking the direction of flow, changing the direction whenever TOP or BOTTOM rows are filled
bool dir_down = false;
int row = 0, col = 0;
for (int i =0; i< messageLenght; i++) //For each Column
{
//If row = 0 or its one less than than the key value
if (row == 0 || row == key - 1)
dir_down = !dir_down;
//Fill the row with characters.
v[row][col++] = message[i];
//find the next raw using next direction flag
dir_down ? row++ : row--; //When dir_down is true increment the row, else decrement the row
}
//Constructing the cipher using the rail matrix
string result;
for(int i = 0; i < key; i++)
{
for (int j = 0; j < messageLenght; j++)
{
if(v[i][j] != '\n')
{
result.push_back(v[i][j]);
}
}
}
return result;
}
string Hash::DecryptMessage(string cipher)
{
//Creation of matrix for the cyphered string
vector<vector<char>> vd;
vd.resize(key);
for (int i = 0; i < key; i++)
{
vd[i].resize(cipher.length());
}
//Fill the matrix with empty spaces
for(int a = 0; a < key; a++)
{
for(int b = 0; b < cipher.length(); b++)
{
vd[a][b] = '\n';
}
}
//Check which direction we will apply
bool dir_down;
int row = 0, col = 0;
for (int i =0; i < cipher.length(); i++)
{
if(row == 0)
dir_down = true;
if (row == key - 1)
dir_down = false;
//starting from row 0 and column 0 replace the space with *
vd[row][col++] = '*';
//If dir_down is true increase the row when its false decrease the row
dir_down ? row++ : row--;
}
int index = 0;
for(int i = 0; i < key; i++)
for (int j = 0; j < cipher.length(); j++)
if (vd[i][j] == '*' && index < cipher.length())
vd[i][j] = cipher[index++];
//Reading the zig-zag to construct the result
string result;
row = 0, col = 0;
for (int i =0; i < cipher.length(); i++)
{
if(row == 0)
{
dir_down = true;
}
if (row == key-1)
{
dir_down = false;
}
if (vd[row][col] != '*')
{
result.push_back(vd[row][col++]);
}
//Find the next row
dir_down ? row++ : row--;
}
return result;
}