|
1 | 1 | using System; |
2 | 2 | using System.Collections.Generic; |
3 | 3 | using System.Linq; |
| 4 | +using System.Security.Cryptography.X509Certificates; |
4 | 5 | using System.Text; |
5 | 6 | using System.Threading.Tasks; |
| 7 | +using MathNet.Numerics.LinearAlgebra; |
| 8 | +using MathNet.Numerics.LinearAlgebra.Double; |
6 | 9 |
|
7 | 10 | namespace SecurityLibrary |
8 | 11 | { |
9 | 12 | /// <summary> |
10 | 13 | /// The List<int> is row based. Which means that the key is given in row based manner. |
11 | 14 | /// </summary> |
12 | | - public class HillCipher : ICryptographicTechnique<string,string>,ICryptographicTechnique<List<int>, List<int>> |
| 15 | + public class HillCipher : ICryptographicTechnique<List<int>, List<int>> |
13 | 16 | { |
14 | | - public List<int>Analyse(List<int>plainText,List<int>cipherText) |
| 17 | + public intdet(Matrix<double>M) |
15 | 18 | { |
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 | + } |
18 | 30 |
|
19 | | - public string Analyse(string plainText, string cipherText) |
| 31 | + return -1; |
| 32 | + |
| 33 | + } |
| 34 | + public Matrix<double> ModMinorCofactor(Matrix<double> M, int A) |
20 | 35 | { |
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; |
22 | 47 | } |
23 | | - |
24 | | - public List<int> Decrypt(List<int> cipherText, List<int> key) |
| 48 | + public List<int> Analyse(List<int> plainText, List<int> cipherText) |
25 | 49 | { |
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; |
27 | 58 | } |
| 59 | + |
28 | 60 |
|
29 | | - public string Decrypt(string cipherText, string key) |
| 61 | + public List<int> Decrypt(List<int> cipherText, List<int> key) |
30 | 62 | { |
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; |
32 | 101 | } |
33 | 102 |
|
| 103 | + |
34 | 104 | public List<int> Encrypt(List<int> plainText, List<int> key) |
35 | 105 | { |
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 | + } |
38 | 121 |
|
39 | | - public string Encrypt(string plainText, string key) |
40 | | - { |
41 | | - throw new NotImplementedException(); |
| 122 | + return finalRes; |
42 | 123 | } |
43 | 124 |
|
44 | | - public List<int> Analyse3By3Key(List<int> plain3, List<int> cipher3) |
45 | | - { |
46 | | - throw new NotImplementedException(); |
47 | | - } |
48 | 125 |
|
49 | | - public string Analyse3By3Key(stringplain3,stringcipher3) |
| 126 | + public List<int> Analyse3By3Key(List<int>plainText,List<int>cipherText) |
50 | 127 | { |
51 | 128 | throw new NotImplementedException(); |
52 | 129 | } |
| 130 | + |
53 | 131 | } |
54 | 132 | } |
0 commit comments