- Notifications
You must be signed in to change notification settings - Fork 1.6k
/
Copy pathHillEncoder.cs
191 lines (158 loc) · 5.65 KB
/
HillEncoder.cs
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
usingSystem;
usingSystem.Linq;
usingAlgorithms.Numeric;
namespaceAlgorithms.Encoders;
/// <summary>
/// Lester S. Hill's polygraphic substitution cipher,
/// without representing letters using mod26, using
/// corresponding "(char)value" instead.
/// </summary>
publicclassHillEncoder:IEncoder<double[,]>
{
privatereadonlyGaussJordanEliminationlinearEquationSolver;
publicHillEncoder()=>linearEquationSolver=newGaussJordanElimination();// TODO: add DI
publicstringEncode(stringtext,double[,]key)
{
varpreparedText=FillGaps(text);
varchunked=ChunkTextToArray(preparedText);
varsplitted=SplitToCharArray(chunked);
varciphered=newdouble[chunked.Length][];
for(vari=0;i<chunked.Length;i++)
{
varvector=newdouble[3];
Array.Copy(splitted,i*3,vector,0,3);
varproduct=MatrixCipher(vector,key);
ciphered[i]=product;
}
varmerged=MergeArrayList(ciphered);
returnBuildStringFromArray(merged);
}
publicstringDecode(stringtext,double[,]key)
{
varchunked=ChunkTextToArray(text);
varsplit=SplitToCharArray(chunked);
vardeciphered=newdouble[chunked.Length][];
for(vari=0;i<chunked.Length;i++)
{
varvector=newdouble[3];
Array.Copy(split,i*3,vector,0,3);
varproduct=MatrixDeCipher(vector,key);
deciphered[i]=product;
}
varmerged=MergeArrayList(deciphered);
varstr=BuildStringFromArray(merged);
returnUnFillGaps(str);
}
/// <summary>
/// Converts elements from the array to their corresponding Unicode characters.
/// </summary>
/// <param name="arr">array of vectors.</param>
/// <returns>Message.</returns>
privatestaticstringBuildStringFromArray(double[]arr)=>new(arr.Select(c =>(char)c).ToArray());
/// <summary>
/// Multiplies the key for the given scalar.
/// </summary>
/// <param name="vector">list of splitted words as numbers.</param>
/// <param name="key">Cipher selected key.</param>
/// <returns>Ciphered vector.</returns>
privatestaticdouble[]MatrixCipher(double[]vector,double[,]key)
{
varmultiplied=newdouble[vector.Length];
for(vari=0;i<key.GetLength(1);i++)
{
for(varj=0;j<key.GetLength(0);j++)
{
multiplied[i]+=key[i,j]*vector[j];
}
}
returnmultiplied;
}
/// <summary>
/// Given a list of vectors, returns a single array of elements.
/// </summary>
/// <param name="list">List of ciphered arrays.</param>
/// <returns>unidimensional list.</returns>
privatestaticdouble[]MergeArrayList(double[][]list)
{
varmerged=newdouble[list.Length*3];
for(vari=0;i<list.Length;i++)
{
Array.Copy(list[i],0,merged,i*3,list[0].Length);
}
returnmerged;
}
/// <summary>
/// Splits the input text message as chunks of words.
/// </summary>
/// <param name="chunked">chunked words list.</param>
/// <returns>spliiter char array.</returns>
privatestaticchar[]SplitToCharArray(string[]chunked)
{
varsplitted=newchar[chunked.Length*3];
for(vari=0;i<chunked.Length;i++)
{
for(varj=0;j<3;j++)
{
splitted[i*3+j]=chunked[i].ToCharArray()[j];
}
}
returnsplitted;
}
/// <summary>
/// Chunks the input text message.
/// </summary>
/// <param name="text">text message.</param>
/// <returns>array of words.</returns>
privatestaticstring[]ChunkTextToArray(stringtext)
{
// To split the message into chunks
vardiv=text.Length/3;
varchunks=newstring[div];
for(vari=0;i<div;i++)
{
chunks.SetValue(text.Substring(i*3,3),i);
}
returnchunks;
}
/// <summary>
/// Fills a text message with spaces at the end
/// to enable a simple split by 3-length-word.
/// </summary>
/// <param name="text">Text Message.</param>
/// <returns>Modified text Message.</returns>
privatestaticstringFillGaps(stringtext)
{
varremainder=text.Length%3;
returnremainder==0?text:text+newstring(' ',3-remainder);
}
/// <summary>
/// Removes the extra spaces included on the cipher phase.
/// </summary>
/// <param name="text">Text message.</param>
/// <returns>Deciphered Message.</returns>
privatestaticstringUnFillGaps(stringtext)=>text.TrimEnd();
/// <summary>
/// Finds the inverse of the given matrix using a linear equation solver.
/// </summary>
/// <param name="vector">Splitted words vector.</param>
/// <param name="key">Key used for the cipher.</param>
/// <returns>TODO.</returns>
privatedouble[]MatrixDeCipher(double[]vector,double[,]key)
{
// To augment the original key with the given vector.
varaugM=newdouble[3,4];
for(vari=0;i<key.GetLength(0);i++)
{
for(varj=0;j<key.GetLength(1);j++)
{
augM[i,j]=key[i,j];
}
}
for(vark=0;k<vector.Length;k++)
{
augM[k,3]=vector[k];
}
_=linearEquationSolver.Solve(augM);
returnnew[]{augM[0,3],augM[1,3],augM[2,3]};
}
}