Skip to content

Commit 395413a

Browse files
committed
hillcipher algorithm added
1 parent 76049e2 commit 395413a

12 files changed

+34
-4
lines changed

β€ŽSecurityLibrary/MainAlgorithms/HillCipher.cs

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,28 @@ public List<int> Analyse(List<int> plainText, List<int> cipherText)
5252
int m = Convert.ToInt32(Math.Sqrt((CD.Count)));
5353
Matrix<double> CMatrix = DenseMatrix.OfColumnMajor(m, (int)cipherText.Count / m, CD.AsEnumerable());
5454
Matrix<double> PMatrix = DenseMatrix.OfColumnMajor(m, (int)plainText.Count / m, PD.AsEnumerable());
55-
Console.WriteLine((CMatrix*PMatrix).ToString());
56-
List<int> f = new List<int>();
57-
return f;
55+
List<int> mayBeKey = new List<int>();
56+
for (int i = 0; i < 26; i++)
57+
{
58+
for (int j = 0; j < 26; j++)
59+
{
60+
for (int k = 0; k < 26; k++)
61+
{
62+
for (int l = 0; l < 26; l++)
63+
{
64+
mayBeKey = new List<int>( new [] {i,j,k,l});
65+
List<int> aa = Encrypt(plainText, mayBeKey);
66+
if (aa.SequenceEqual(cipherText))
67+
{
68+
return mayBeKey;
69+
}
70+
71+
}
72+
}
73+
}
74+
}
75+
76+
throw new InvalidAnlysisException();
5877
}
5978

6079

@@ -125,7 +144,18 @@ public List<int> Encrypt(List<int> plainText, List<int> key)
125144

126145
public List<int> Analyse3By3Key(List<int> plainText, List<int> cipherText)
127146
{
128-
throw new NotImplementedException();
147+
List<double> CD = cipherText.ConvertAll(x => (double)x);
148+
List<double> PD = plainText.ConvertAll(x => (double)x);
149+
int m = Convert.ToInt32(Math.Sqrt((CD.Count)));
150+
Matrix<double> CMatrix = DenseMatrix.OfColumnMajor(m, (int)cipherText.Count / m, CD.AsEnumerable());
151+
Matrix<double> PMatrix = DenseMatrix.OfColumnMajor(m, (int)plainText.Count / m, PD.AsEnumerable());
152+
List<int> mayBeKey = new List<int>();
153+
Matrix<double> KMatrix = DenseMatrix.Create(3, 3, 0);
154+
PMatrix = ModMinorCofactor(PMatrix.Transpose(), det(PMatrix));
155+
KMatrix = (CMatrix * PMatrix);
156+
mayBeKey = KMatrix.Transpose().Enumerate().ToList().Select(i => (int)i%26).ToList();
157+
mayBeKey.ForEach(i=> Console.WriteLine(i.ToString()));
158+
return mayBeKey;
129159
}
130160

131161
}
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.

0 commit comments

Comments
 (0)