@@ -52,9 +52,28 @@ public List<int> Analyse(List<int> plainText, List<int> cipherText)
52
52
int m = Convert . ToInt32 ( Math . Sqrt ( ( CD . Count ) ) ) ;
53
53
Matrix < double > CMatrix = DenseMatrix . OfColumnMajor ( m , ( int ) cipherText . Count / m , CD . AsEnumerable ( ) ) ;
54
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 ;
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 ( ) ;
58
77
}
59
78
60
79
@@ -125,7 +144,18 @@ public List<int> Encrypt(List<int> plainText, List<int> key)
125
144
126
145
public List < int > Analyse3By3Key ( List < int > plainText , List < int > cipherText )
127
146
{
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 ;
129
159
}
130
160
131
161
}
0 commit comments