Exemplo n.º 1
0
        private void buttonCrypto_Click(object sender, EventArgs e)
        {
            string cipher,substring,tempstring="";
            int indexfirst,indexlast,i,j,k,a,b;
            List<int> distList = new List<int>();
            List<Nod> NodList = new List<Nod>();
            Nod currentnod=new Nod();
            bool check;

            if (textBox_Cipher.Lines.Count() == 0)
            {
                MessageBox.Show("Не введен шифротекст!");
                return;
            }
            
            textBox_Source.Clear();
            cipher = textBox_Cipher.Text.ToString();
            for (indexfirst = 0; indexfirst < cipher.Length; indexfirst++)
            {
                if (Array.IndexOf(Alphabet, cipher[indexfirst]) == -1)
                {
                    cipher = cipher.Remove(indexfirst, 1);
                    indexfirst--;
                }
            }
            for (int length = 10; length >= 3; length--)
            {
                for (indexfirst = 0; indexfirst < cipher.Length-length; indexfirst++)
                {
                    substring = cipher.Substring(indexfirst,length);
                    if ((textBox_Source.Text.IndexOf(substring) == -1) && (textBox_Source.Text.IndexOf(substring.Substring(0,3)) == -1))
                    {
                        tempstring = "";
                        indexlast = indexfirst + length - 1;
                        while (indexlast > -1)
                        {
                            indexlast = cipher.IndexOf(substring, indexlast + 1);
                            if (indexlast > -1)
                            {
                                tempstring = tempstring + "pos=" + indexlast + " dist=" + (indexlast - indexfirst) + "; ";
                                distList.Add(indexlast - indexfirst);
                            }
                        }
                        if (tempstring != "")
                            textBox_Source.AppendText("\"" + substring + "\" " + "starting pos=" + indexfirst + " " + tempstring + "\r\n");
                    }
                }
            }
            distList.Sort();
            for (i = 1; i < distList.Count; i++)
            {
                if (distList[i] == distList[i - 1])
                {
                    distList.RemoveAt(i);
                    i--;
                }
            }
            for (i = 0; i < distList.Count; i++)
            {
                for (j = i+1; j < distList.Count; j++)
                {
                    check = true;
                    a = distList[i];
                    b = distList[j];
                    while ((a!=0) && (b!=0))
                    {
                        if (a > b)
                            a = a % b;
                        else
                            b = b % a;
                    }
                    currentnod.value=a+b;
                    currentnod.chance=1;
                    if (currentnod.value == 1)
                        continue;
                    for (k = 0; k < NodList.Count; k++)
                    {
                        if (NodList[k].value == currentnod.value)
                        {
                            check = false;
                            currentnod = NodList[k];
                            currentnod.chance++;
                            NodList[k] = currentnod;
                            break;
                        }
                    }
                    if (check)
                        NodList.Add(currentnod);
                }
            }
            indexfirst = 0;
            currentnod = NodList[0];
            for (i = 0; i < NodList.Count; i++)
            {
                indexfirst+=NodList[i].chance;
                if (NodList[i].chance>currentnod.chance)
                    currentnod = NodList[i];
            }
            textBox_Source.AppendText("Криптоанализ завершен!\r\n");
            textBox_Source.AppendText("Вероятная длинна ключа: "+currentnod.value+". Вероятность: "+(currentnod.chance*100/indexfirst)+"%\r\n");
            
        }