Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 76049e2

Browse files
hillcipher added
1 parent 837e380 commit 76049e2

38 files changed

+326298
-23
lines changed
Lines changed: 99 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,132 @@
11
using System;
22
using System.Collections.Generic;
33
using System.Linq;
4+
using System.Security.Cryptography.X509Certificates;
45
using System.Text;
56
using System.Threading.Tasks;
7+
using MathNet.Numerics.LinearAlgebra;
8+
using MathNet.Numerics.LinearAlgebra.Double;
69

710
namespace SecurityLibrary
811
{
912
/// <summary>
1013
/// The List<int> is row based. Which means that the key is given in row based manner.
1114
/// </summary>
12-
public class HillCipher : ICryptographicTechnique<string,string>,ICryptographicTechnique<List<int>, List<int>>
15+
public class HillCipher : ICryptographicTechnique<List<int>, List<int>>
1316
{
14-
public List<int>Analyse(List<int>plainText,List<int>cipherText)
17+
public intdet(Matrix<double>M)
1518
{
16-
throw new NotImplementedException();
17-
}
19+
double A = M[0, 0] * (M[1, 1] * M[2, 2] - M[1, 2] * M[2, 1]) -
20+
M[0, 1] * (M[1, 0] * M[2, 2] - M[1, 2] * M[2, 0]) +
21+
M[0, 2] * (M[1, 0] * M[2, 1] - M[1, 1] * M[2, 0]);
22+
int AI = (int)A % 26 >= 0 ? (int)A % 26 : (int)A % 26 + 26;
23+
for (int i = 0; i < 26; i++)
24+
{
25+
if (AI * i % 26 == 1)
26+
{
27+
return i;
28+
}
29+
}
1830

19-
public string Analyse(string plainText, string cipherText)
31+
return -1;
32+
33+
}
34+
public Matrix<double> ModMinorCofactor(Matrix<double> M, int A)
2035
{
21-
throw new NotImplementedException();
36+
Matrix<double> resMat = DenseMatrix.Create(3, 3, 0.0);
37+
for (int i = 0; i < 3; i++)
38+
{
39+
for (int j = 0; j < 3; j++)
40+
{
41+
int x = i == 0 ? 1 : 0, y = j == 0 ? 1 : 0, x1 = i == 2 ? 1 : 2, y1 = j == 2 ? 1 : 2;
42+
double r = ((M[x, y] * M[x1, y1] - M[x, y1] * M[x1, y]) * Math.Pow(-1, i + j) * A) % 26;
43+
resMat[i, j] = r >= 0 ? r : r + 26;
44+
}
45+
}
46+
return resMat;
2247
}
23-
24-
public List<int> Decrypt(List<int> cipherText, List<int> key)
48+
public List<int> Analyse(List<int> plainText, List<int> cipherText)
2549
{
26-
throw new NotImplementedException();
50+
List<double> CD = cipherText.ConvertAll(x => (double)x);
51+
List<double> PD = plainText.ConvertAll(x => (double)x);
52+
int m = Convert.ToInt32(Math.Sqrt((CD.Count)));
53+
Matrix<double> CMatrix = DenseMatrix.OfColumnMajor(m, (int)cipherText.Count / m, CD.AsEnumerable());
54+
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;
2758
}
59+
2860

29-
public string Decrypt(string cipherText, string key)
61+
public List<int> Decrypt(List<int> cipherText, List<int> key)
3062
{
31-
throw new NotImplementedException();
63+
List<double> keyD = key.ConvertAll(x => (double)x);
64+
List<double> CD = cipherText.ConvertAll(x => (double)x);
65+
int m = Convert.ToInt32(Math.Sqrt((key.Count)));
66+
Matrix<double> keyMatrix = DenseMatrix.OfColumnMajor(m, (int)key.Count / m, keyD.AsEnumerable());
67+
Matrix<double> PMatrix = DenseMatrix.OfColumnMajor(m, (int)cipherText.Count / m, CD.AsEnumerable());
68+
List<int> finalRes = new List<int>();
69+
if (keyMatrix.ColumnCount == 3)
70+
{
71+
keyMatrix = ModMinorCofactor(keyMatrix.Transpose(), det(keyMatrix));
72+
}
73+
else
74+
{
75+
keyMatrix = keyMatrix.Inverse();
76+
Console.WriteLine(keyMatrix.ToString());
77+
Console.WriteLine(((int)keyMatrix[0, 0]).ToString() + ", " + ((int)keyMatrix[0, 0]).ToString());
78+
79+
}
80+
if (Math.Abs((int)keyMatrix[0, 0]).ToString() != Math.Abs((double)keyMatrix[0, 0]).ToString())
81+
{
82+
throw new SystemException();
83+
}
84+
for (int i = 0; i < PMatrix.ColumnCount; i++)
85+
{
86+
List<double> Res = new List<double>();
87+
Res = ((((PMatrix.Column(i)).ToRowMatrix() * keyMatrix) % 26).Enumerate().ToList());
88+
for (int j = 0; j < Res.Count; j++)
89+
{
90+
int x = (int) Res[j] >= 0 ? (int) Res[j] : (int) Res[j] + 26;
91+
finalRes.Add(x);
92+
}
93+
}
94+
95+
for (int i = 0; i < finalRes.Count; i++)
96+
{
97+
Console.WriteLine(finalRes[i].ToString());
98+
}
99+
100+
return finalRes;
32101
}
33102

103+
34104
public List<int> Encrypt(List<int> plainText, List<int> key)
35105
{
36-
throw new NotImplementedException();
37-
}
106+
List<double> keyD = key.ConvertAll(x => (double)x);
107+
List<double> PD = plainText.ConvertAll(x => (double)x);
108+
int m = Convert.ToInt32(Math.Sqrt((key.Count)));
109+
Matrix<double> keyMatrix = DenseMatrix.OfColumnMajor(m,(int) key.Count/m, keyD.AsEnumerable());
110+
Matrix<double> PMatrix = DenseMatrix.OfColumnMajor(m, (int) plainText.Count / m, PD.AsEnumerable());
111+
List<int> finalRes = new List<int>();
112+
for (int i = 0; i < PMatrix.ColumnCount; i++)
113+
{
114+
List<double> Res = new List<double>();
115+
Res = ((((PMatrix.Column(i)).ToRowMatrix()*keyMatrix) % 26).Enumerate().ToList());
116+
for (int j = 0; j < Res.Count; j++)
117+
{
118+
finalRes.Add((int)Res[j]);
119+
}
120+
}
38121

39-
public string Encrypt(string plainText, string key)
40-
{
41-
throw new NotImplementedException();
122+
return finalRes;
42123
}
43124

44-
public List<int> Analyse3By3Key(List<int> plain3, List<int> cipher3)
45-
{
46-
throw new NotImplementedException();
47-
}
48125

49-
public string Analyse3By3Key(stringplain3,stringcipher3)
126+
public List<int> Analyse3By3Key(List<int>plainText,List<int>cipherText)
50127
{
51128
throw new NotImplementedException();
52129
}
130+
53131
}
54132
}

‎SecurityLibrary/SecurityLibrary.csproj‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,14 @@
3131
<WarningLevel>4</WarningLevel>
3232
</PropertyGroup>
3333
<ItemGroup>
34+
<Reference Include="MathNet.Numerics, Version=4.4.0.0, Culture=neutral, processorArchitecture=MSIL">
35+
<HintPath>..\packages\MathNet.Numerics.4.4.0\lib\net40\MathNet.Numerics.dll</HintPath>
36+
<Private>True</Private>
37+
</Reference>
3438
<Reference Include="System" />
3539
<Reference Include="System.Core" />
40+
<Reference Include="System.Numerics" />
41+
<Reference Include="System.Runtime.Serialization" />
3642
<Reference Include="System.Xml.Linq" />
3743
<Reference Include="System.Data.DataSetExtensions" />
3844
<Reference Include="Microsoft.CSharp" />
@@ -62,7 +68,9 @@
6268
<Compile Include="ICryptographicTechnique.cs" />
6369
<Compile Include="RSA\RSA.cs" />
6470
</ItemGroup>
65-
<ItemGroup />
71+
<ItemGroup>
72+
<None Include="packages.config" />
73+
</ItemGroup>
6674
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
6775
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
6876
Other similar extension points exist, see Microsoft.Common.targets.
1.47 MB
Binary file not shown.

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /