예제 #1
0
        public void FindKey(string chiperText)
        {
            chiperText = chiperText.ToUpper();
            var repetitionPeriods = KasiskiMethod(chiperText);

            if (repetitionPeriods.Count == 0)
            {
                Console.WriteLine("Невозможно найти период. Невозможно взломать ключ");
            }
            else
            {
                var nod = FindNOD(repetitionPeriods.ToArray());
                var k   = 1;
                for (var m = nod; ; m = nod * k)
                {
                    if (m > chiperText.Length / m)
                    {
                        break;
                    }
                    char[,] strings = new char[chiperText.Length / m + 1, m];
                    for (int i = 0; i < chiperText.Length; i++)
                    {
                        int ii = i / m;
                        int ij = i - (ii * m);

                        strings[ii, ij] = chiperText[i];
                    }
                    var paswords = new List <string>();
                    paswords.Add("");
                    for (int i = 0; i < m; i++)
                    {
                        Dictionary <char, int> dict = new Dictionary <char, int>();

                        for (int j = 0; j < (chiperText.Length / m) + 1; j++)
                        {
                            if (dict.ContainsKey(strings[j, i]))
                            {
                                dict[strings[j, i]]++;
                            }
                            else
                            {
                                dict.Add(strings[j, i], 1);
                            }
                        }

                        var sortict = dict.OrderByDescending(x => x.Value)
                                      .ToDictionary(x => x.Key, x => x.Value);
                        var maxValue         = sortict.Values.Max();
                        var positionCandidat = new List <char>();

                        char sy = Languege.z == 33 ? 'О' : 'E';
                        foreach (var kvp in sortict)
                        {
                            if (kvp.Value < maxValue)
                            {
                                break;
                            }
                            else
                            {
                                positionCandidat.Add(kvp.Key);
                            }
                        }
                        if (positionCandidat.Count > 1)
                        {
                            var bufList = new List <string>(paswords);
                            for (var copy = 0; copy < positionCandidat.Count - 1; copy++)
                            {
                                paswords.AddRange(bufList);
                            }
                            var transition      = paswords.Count / positionCandidat.Count;
                            var candidatElement = 0;
                            for (var pos = 0; pos < paswords.Count; pos++)
                            {
                                if (pos != 0 && pos % transition == 0)
                                {
                                    candidatElement++;
                                }
                                int res = Languege.dictionary[positionCandidat[candidatElement]] -
                                          Languege.dictionary[sy];
                                paswords[pos] += FindValue.Findvalue(res + 1);
                            }
                        }
                        else
                        {
                            int res = Languege.dictionary[positionCandidat[0]] -
                                      Languege.dictionary[sy];
                            for (var pos = 0; pos < paswords.Count; pos++)
                            {
                                paswords[pos] += FindValue.Findvalue(res);
                            }
                        }
                    }
                    foreach (var password in paswords)
                    {
                        Add(password);
                    }
                    k += 1;
                }
            }
        }
예제 #2
0
        public void FindKey(string cText, string oText)
        {
            var numberLitera = Converter.ConvertWordToCode(cText);

            for (var del = 2; del <= cText.Length; del++)
            {
                if (cText.Length % del != 0)
                {
                    continue;
                }
                else
                {
                    var possiblePermutations = SetTransposition(new int[del], 0, del);
                    foreach (var perm in possiblePermutations)
                    {
                        var array = new double[cText.Length / del, del];
                        var pos   = 0;
                        for (var x = 0; x < del; x++)
                        {
                            for (var y = 0; y < cText.Length / del; y++)
                            {
                                array[y, x] = numberLitera[pos];
                                pos++;
                            }
                        }
                        var matrixArray = Matrix <double> .Build.DenseOfArray(array);

                        var ansverMatrix = DenseMatrix.Create(cText.Length / del, del, 0);
                        pos = 0;
                        foreach (var p in perm)
                        {
                            var column = matrixArray.Column(p);
                            ansverMatrix.SetColumn(pos, column.ToArray());
                            pos++;
                        }
                        string ans = "";
                        for (var y = 0; y < cText.Length / del; y++)
                        {
                            for (var x = 0; x < del; x++)
                            {
                                ans += Convert.ToString(FindValue.Findvalue(Convert.ToInt32(ansverMatrix[y, x]) % Languege.z));
                            }
                        }
                        if (ans.Contains(oText.ToUpper()))
                        {
                            Key.Add(perm);
                        }
                        #region
                        //var block = 0;
                        //var permIndex = 0;
                        //var decryption = new char[cText.Length];
                        //for (var w = 0; w < cText.Length; w++)
                        //{
                        //    decryption[w] = cText[perm[permIndex] + block];
                        //    permIndex++;
                        //    if ((w + 1) % del == 0 && w != 0)
                        //        block+=del;
                        //    if (permIndex == perm.Length)
                        //        permIndex = 0;
                        //}
                        ////TODO: Сделать из этого что-то нормальное
                        //var stringDecryption = Converter.ConvertArrayToString(decryption);
                        //if (stringDecryption.Contains(oText))
                        //    Key.Add(perm);
                        //if (Languege.dictionary.ContainsKey('А')) //Русский язык
                        //{
                        //    var dictionary = WordList.CreateFromFiles(@"Russian.dic", @"Russian.aff");
                        //    var suggestions = dictionary.Suggest(stringDecryption);
                        //    foreach (var s in suggestions)
                        //        Console.WriteLine(s);
                        //}
                        //else
                        //{

                        //}
                        #endregion
                    }
                }
            }
        }