Exemple #1
0
        public void Attack()
        {
            BigraphStatistic CS = new BigraphStatistic(matrixSize);

            CipherStat = CS.CalcLogStat(cipherText, alphabet);
            int[]  TestKey = new int[(int)Math.Pow(matrixSize, 2)];
            double Score2;

            int[]  WorstKey   = new int[(int)Math.Pow(matrixSize, 2)];
            int[]  BestKey    = new int[(int)Math.Pow(matrixSize, 2)];
            double WorstScore = 0;
            double BestScore;
            int    Count = 0;


            if (LogMessageByKeySearcher != null)
            {
                LogMessageByKeySearcher("Calculating decoding tab..", NotificationLevel.Info);
            }

            CalcDecodingTab(5);


            //SortedDictionary<double, int[]> KeyHeap = new SortedDictionary<double, int[]>();
            //SortedDictionary<double, int[]>.KeyCollection KeyColl = KeyHeap.Keys;

            PriorityQueue <int[], double>[] keyHeap = new PriorityQueue <int[], double> [2];
            keyHeap[0] = new PriorityQueue <int[], double>();
            keyHeap[1] = new PriorityQueue <int[], double>();


            if (LogMessageByKeySearcher != null)
            {
                LogMessageByKeySearcher("Testing all partial keys of length 5 ", NotificationLevel.Info);
            }


            DateTime time1 = DateTime.Now;

            for (int i = 0; i < (int)Math.Pow(matrixSize, 2); i++)
            {
                if (ProgressChangedByKeySearcher != null)
                {
                    ProgressChangedByKeySearcher(i, (int)Math.Pow(matrixSize, 2) * 2);
                }

                TestKey[0] = i;
                for (int j = 0; j < (int)Math.Pow(matrixSize, 2); j++)
                {
                    if (j != i)
                    {
                        TestKey[1] = j;
                        for (int k = 0; k < (int)Math.Pow(matrixSize, 2); k++)
                        {
                            if (k != i && k != j)
                            {
                                TestKey[2] = k;
                                for (int l = 0; l < (int)Math.Pow(matrixSize, 2); l++)
                                {
                                    if (l != i && l != j && l != k)
                                    {
                                        TestKey[3] = l;
                                        for (int m = 0; m < (int)Math.Pow(matrixSize, 2); m++)
                                        {
                                            if (m != i && m != j && m != k && m != l)
                                            {
                                                TestKey[4] = m;
                                                Score2     = EvaluateKey2(TestKey);
                                                if (Count > keyHeapSize - 1)
                                                {
                                                    if (Score2 < WorstScore)
                                                    {
                                                        try
                                                        {
                                                            //if (!KeyHeap.Contains(Score2))
                                                            {
                                                                keyHeap[0].Dequeue();
                                                                keyHeap[0].Enqueue((int[])TestKey.Clone(), Score2);
                                                                WorstScore = keyHeap[0].Peek().Priority;
                                                            }
                                                        }
                                                        catch (ArgumentException)
                                                        {
                                                            Console.Out.WriteLine("Wert bereits im Heap (> {0})", keyHeapSize);
                                                        }
                                                    }
                                                }
                                                else
                                                {
                                                    try
                                                    {
                                                        keyHeap[0].Enqueue(TestKey, Score2);
                                                        WorstScore = Math.Max(WorstScore, Score2);
                                                        Count      = keyHeap[0].Count;
                                                    }
                                                    catch (ArgumentException)
                                                    {
                                                        Console.Out.WriteLine("Wert bereits im Heap (< {0})", keyHeapSize);
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }


            DateTime time2 = DateTime.Now;
            TimeSpan diff  = time2 - time1;

            if (LogMessageByKeySearcher != null)
            {
                LogMessageByKeySearcher("\n\ntime required: " + Convert.ToString(diff.TotalSeconds) + " seconds", NotificationLevel.Info);
            }


            WorstKey  = keyHeap[0].Peek().Value;
            BestScore = double.MaxValue;
            foreach (PriorityQueueItem <int[], double> pqi in keyHeap[0])
            {
                if (pqi.Priority < BestScore)
                {
                    BestScore = pqi.Priority;
                    BestKey   = pqi.Value;
                }
            }

            if (LogMessageByKeySearcher != null)
            {
                LogMessageByKeySearcher("\nBest Score: " + Convert.ToString(BestScore), NotificationLevel.Info);
                LogMessageByKeySearcher("\nBest Key: ", NotificationLevel.Info);

                LogMessageByKeySearcher("\nWorst Score: " + Convert.ToString(WorstScore), NotificationLevel.Info);
                LogMessageByKeySearcher("\nWorst Key: ", NotificationLevel.Info);

                LogMessageByKeySearcher("\nAmount of keys in Heap: " + Convert.ToString(keyHeap[0].Count), NotificationLevel.Info);
                LogMessageByKeySearcher("\nTesting next position of keys in heap...", NotificationLevel.Info);
            }


            time1 = DateTime.Now;

            for (int pos = 5; pos < (int)Math.Pow(matrixSize, 2); pos++)
            {
                keyHeap[pos % 2].Clear();
                CalcDecodingTab(pos + 1);
                Count = 0;

                if (ProgressChangedByKeySearcher != null)
                {
                    ProgressChangedByKeySearcher((pos - 5) + ((int)Math.Pow(matrixSize, 2) - 5), ((int)Math.Pow(matrixSize, 2) - 5) * 2);
                }

                foreach (PriorityQueueItem <int[], double> pqi in keyHeap[(pos + 1) % 2])
                {
                    bool[] letterinkey = new bool[(int)Math.Pow(matrixSize, 2)];
                    for (int i = 0; i < pos; i++)
                    {
                        letterinkey[pqi.Value[i]] = true;
                    }
                    for (int i = 0; i < (int)Math.Pow(matrixSize, 2); i++)
                    {
                        if (!letterinkey[i])
                        {
                            pqi.Value[pos] = i;
                            Score2         = EvaluateKey2(pqi.Value);
                            if (Count > keyHeapSize - 1)
                            {
                                if (Score2 < WorstScore)
                                {
                                    keyHeap[pos & 1].Dequeue();
                                    keyHeap[pos & 1].Enqueue((int[])pqi.Value.Clone(), Score2);
                                    WorstScore = keyHeap[pos & 1].Peek().Priority;
                                }
                            }
                            else
                            {
                                keyHeap[pos & 1].Enqueue(pqi.Value, Score2);
                                WorstScore = Math.Max(WorstScore, Score2);
                                Count      = keyHeap[pos & 1].Count;
                            }
                        }
                    }
                }
                if (LogMessageByKeySearcher != null)
                {
                    LogMessageByKeySearcher("Position " + Convert.ToString(pos + 1) + " done.", NotificationLevel.Info);
                }
            }


            time2 = DateTime.Now;
            diff  = time2 - time1;

            if (LogMessageByKeySearcher != null)
            {
                LogMessageByKeySearcher("\ntime required: " + Convert.ToString(diff.TotalSeconds) + " seconds", NotificationLevel.Info);
            }


            BestScore = double.MaxValue;

            foreach (PriorityQueueItem <int[], double> pqi in keyHeap[((int)Math.Pow(matrixSize, 2) - 1) % 2])
            {
                if (pqi.Priority < BestScore)
                {
                    BestScore = pqi.Priority;
                    BestKey   = (int[])pqi.Value.Clone();
                }
            }


            if (LogMessageByKeySearcher != null)
            {
                LogMessageByKeySearcher("\nBest Score: " + Convert.ToString(BestScore), NotificationLevel.Info);
                LogMessageByKeySearcher("\nBest Key: ", NotificationLevel.Info);
            }


            int[] CorrectKey = new int[(int)Math.Pow(matrixSize, 2)];
            for (int i = 0; i < (int)Math.Pow(matrixSize, 2); i++)
            {
                CorrectKey[i] = i;
            }

            if (LogMessageByKeySearcher != null)
            {
                LogMessageByKeySearcher("\nCorrect Key Score: " + Convert.ToString(EvaluateKey2(CorrectKey)), NotificationLevel.Info);
                LogMessageByKeySearcher("\nCorrect Key: ", NotificationLevel.Info);
            }


            Key BestKeyMatrix = new Key(Key.ConvertToChar(BestKey, alphabet));

            plainText = Decrypt(BestKeyMatrix, cipherText);
        }
Exemple #2
0
        // Create a Bigraph Statistic and write in both txt and xml file
        internal static void CreateBS(string Path, int matrixSize)
        {
            string[] StatTextPath = { @"H:\Texte\Text1.txt",  @"H:\Texte\Text2.txt",  @"H:\Texte\Text3.txt",  @"H:\Texte\Text4.txt",
                                      @"H:\Texte\Text5.txt",  @"H:\Texte\Text6.txt",  @"H:\Texte\Text7.txt",  @"H:\Texte\Text8.txt",
                                      @"H:\Texte\Text9.txt",  @"H:\Texte\Text10.txt", @"H:\Texte\Text11.txt", @"H:\Texte\Text12.txt",
                                      @"H:\Texte\Text13.txt", @"H:\Texte\Text14.txt", @"H:\Texte\Text15.txt", @"H:\Texte\Text16.txt",
                                      @"H:\Texte\Text17.txt", @"H:\Texte\Text18.txt", @"H:\Texte\Text19.txt", @"H:\Texte\Text20.txt" };


            /*
             * string[] StatTextPath = { @"H:\Texte eng\Text1.txt", @"H:\Texte eng\Text2.txt", @"H:\Texte eng\Text3.txt", @"H:\Texte eng\Text4.txt",
             *                        @"H:\Texte eng\Text5.txt", @"H:\Texte eng\Text6.txt", @"H:\Texte eng\Text7.txt", @"H:\Texte eng\Text8.txt",
             *                        @"H:\Texte eng\Text9.txt", @"H:\Texte eng\Text10.txt", @"H:\Texte eng\Text11.txt", @"H:\Texte eng\Text12.txt"};
             */

            BigraphStatistic BS = new BigraphStatistic(matrixSize);

            int[,] BigraphStat = BS.Generate(StatTextPath);

            string[] Tab = new string[(int)Math.Pow(matrixSize, 2)];
            for (int i = 0; i < (int)Math.Pow(matrixSize, 2); i++)
            {
                for (int j = 0; j < (int)Math.Pow(matrixSize, 2); j++)
                {
                    Tab[i] += Convert.ToString(BigraphStat[i, j]) + " ";
                    Tab[i] += "\t";
                }
            }

            System.IO.File.WriteAllLines(Path + "BSde.txt", Tab);

            int[][] BigraphStatDummy = new int[(int)Math.Pow(matrixSize, 2)][];

            for (int i = 0; i < (int)Math.Pow(matrixSize, 2); i++)
            {
                BigraphStatDummy[i] = new int[(int)Math.Pow(matrixSize, 2)];
                for (int j = 0; j < (int)Math.Pow(matrixSize, 2); j++)
                {
                    BigraphStatDummy[i][j] = BigraphStat[i, j];
                }
            }

            System.Xml.Serialization.XmlSerializer WriteBS = new System.Xml.Serialization.XmlSerializer(typeof(Int32[][]));
            System.Xml.XmlWriter XmlWriter = System.Xml.XmlWriter.Create(Path + "BSde.xml");
            WriteBS.Serialize(XmlWriter, BigraphStatDummy);
            XmlWriter.Close();

            // Compute log-probabilities
            int sum = 0;

            foreach (int value in BigraphStat)
            {
                sum += value;
            }


            double[][] BigraphStatLog = new double[(int)Math.Pow(matrixSize, 2)][];

            for (int i = 0; i < (int)Math.Pow(matrixSize, 2); i++)
            {
                BigraphStatLog[i] = new double[(int)Math.Pow(matrixSize, 2)];
                for (int j = 0; j < (int)Math.Pow(matrixSize, 2); j++)
                {
                    if (BigraphStat[i, j] > 0)
                    {
                        BigraphStatLog[i][j] = Math.Log((double)BigraphStat[i, j] / sum);
                    }
                    else
                    {
                        BigraphStatLog[i][j] = -10;
                    }
                }
            }

            string[] TabLog = new string[(int)Math.Pow(matrixSize, 2)];
            for (int i = 0; i < (int)Math.Pow(matrixSize, 2); i++)
            {
                for (int j = 0; j < (int)Math.Pow(matrixSize, 2); j++)
                {
                    TabLog[i] += Convert.ToString(BigraphStatLog[i][j]) + "\t";
                }
            }

            System.Xml.Serialization.XmlSerializer WriteBSLog = new System.Xml.Serialization.XmlSerializer(typeof(Double[][]));
            System.Xml.XmlWriter XmlWriterLog;

            switch (matrixSize)
            {
            case 5:
                System.IO.File.WriteAllLines(Path + "BSLog10sde.txt", TabLog);
                XmlWriterLog = System.Xml.XmlWriter.Create(Path + "BSLog10sde.xml");
                break;

            case 6:
                System.IO.File.WriteAllLines(Path + "BSLog10lde.txt", TabLog);
                XmlWriterLog = System.Xml.XmlWriter.Create(Path + "BSLog10lde.xml");
                break;

            default:
                XmlWriterLog = System.Xml.XmlWriter.Create(Path + "BSLog10.xml");
                break;
            }

            WriteBSLog.Serialize(XmlWriterLog, BigraphStatLog);
            XmlWriterLog.Close();
        }