Exemple #1
0
        // Create a Bigraph Statistic and write in both txt and xml file
        static void CreateBS(string PathTxt, string PathXml)
        {
            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" };

            BigraphStatistic BS = new BigraphStatistic();

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

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

            System.IO.File.WriteAllLines(PathTxt, Tab);

            int[][] BigraphStatDummy = new int[25][];

            for (int i = 0; i < 25; i++)
            {
                BigraphStatDummy[i] = new int[25];
                for (int j = 0; j < 25; 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(PathXml);
            WriteBS.Serialize(XmlWriter, BigraphStatDummy);
            XmlWriter.Close();

            // Compute log-probabilities
            int sum = 0;

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


            double[][] BigraphStatLog = new double[25][];

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

            string[] TabLog = new string[25];
            for (int i = 0; i < 25; i++)
            {
                for (int j = 0; j < 25; j++)
                {
                    TabLog[i] += Convert.ToString(BigraphStatLog[i][j]) + "\t";
                }
            }
            System.IO.File.WriteAllLines(@"H:\BS\BSLog100.txt", TabLog);

            System.Xml.Serialization.XmlSerializer WriteBSLog = new System.Xml.Serialization.XmlSerializer(typeof(Double[][]));
            System.Xml.XmlWriter XmlWriterLog = System.Xml.XmlWriter.Create(@"H:\BS\BSLog100.xml");
            WriteBSLog.Serialize(XmlWriterLog, BigraphStatLog);
            XmlWriterLog.Close();
        }
Exemple #2
0
        public static string Attack(string CipherText, Double[,] BS)
        {
            BigraphStat = BS;
            BigraphStatistic CS = new BigraphStatistic();

            CipherStat = CS.CalcLog(CipherText);
            int KeyHeapLength = 5000;

            int[]  TestKey = new int[25];
            double Score2;

            int[]  WorstKey   = new int[25];
            int[]  BestKey    = new int[25];
            double WorstScore = 0;
            double BestScore;
            int    Count = 0;


            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>();


            Console.Write("Testing all partial keys of length 5 ");

            DateTime time1 = DateTime.Now;

            for (int i = 0; i < 25; i++)
            {
                Console.Write(".");
                TestKey[0] = i;
                for (int j = 0; j < 25; j++)
                {
                    if (j != i)
                    {
                        TestKey[1] = j;
                        for (int k = 0; k < 25; k++)
                        {
                            if (k != i && k != j)
                            {
                                TestKey[2] = k;
                                for (int l = 0; l < 25; l++)
                                {
                                    if (l != i && l != j && l != k)
                                    {
                                        TestKey[3] = l;
                                        for (int m = 0; m < 25; m++)
                                        {
                                            if (m != i && m != j && m != k && m != l)
                                            {
                                                TestKey[4] = m;
                                                Score2     = EvaluateKey2(TestKey);
                                                if (Count > KeyHeapLength - 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})", KeyHeapLength);
                                                        }
                                                    }
                                                }
                                                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})", KeyHeapLength);
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }


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

            Console.Out.WriteLine("\n\ntime required: " + Convert.ToString(diff.TotalSeconds) + " seconds");


            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;
                }
            }

            Console.Out.WriteLine("\nBest Score: {0}", BestScore);
            Console.Out.WriteLine("Best Key:");
            Matrix.WriteOnConsoleInt(BestKey);


            Console.Out.WriteLine("\nWorst Score: {0}", WorstScore);
            Console.Out.WriteLine("Worst Key:");
            Matrix.WriteOnConsoleInt(WorstKey);


            Console.Out.WriteLine("\nAmount of keys in Heap: {0}", KeyHeap[0].Count);

            Console.Out.WriteLine("\nTesting next position of keys in heap...");

            time1 = DateTime.Now;

            for (int pos = 5; pos < 25; pos++)
            {
                KeyHeap[pos % 2].Clear();
                CalcDecodingTab(pos + 1);
                Count = 0;

                foreach (PriorityQueueItem <int[], double> pqi in KeyHeap[(pos + 1) % 2])
                {
                    bool[] letterinkey = new bool[25];
                    for (int i = 0; i < pos; i++)
                    {
                        letterinkey[pqi.Value[i]] = true;
                    }
                    for (int i = 0; i < 25; i++)
                    {
                        if (!letterinkey[i])
                        {
                            pqi.Value[pos] = i;
                            Score2         = EvaluateKey2(pqi.Value);
                            if (Count > KeyHeapLength - 1)
                            {
                                if (Score2 < WorstScore)
                                {
                                    KeyHeap[pos % 2].Dequeue();
                                    KeyHeap[pos % 2].Enqueue((int[])pqi.Value.Clone(), Score2);
                                    WorstScore = KeyHeap[pos % 2].Peek().Priority;
                                }
                            }
                            else
                            {
                                KeyHeap[pos % 2].Enqueue(pqi.Value, Score2);
                                WorstScore = Math.Max(WorstScore, Score2);
                                Count      = KeyHeap[pos % 2].Count;
                            }
                        }
                    }
                }
                Console.Out.WriteLine("Position {0} done.", pos);
            }


            time2 = DateTime.Now;
            diff  = time2 - time1;
            Console.Out.WriteLine("\ntime required: " + Convert.ToString(diff.TotalSeconds) + " seconds");


            BestScore = double.MaxValue;

            foreach (PriorityQueueItem <int[], double> pqi in KeyHeap[24 % 2])
            {
                if (pqi.Priority < BestScore)
                {
                    BestScore = pqi.Priority;
                    BestKey   = (int[])pqi.Value.Clone();
                }
            }

            Console.Out.WriteLine("\nBest Score: {0}", BestScore);
            Console.Out.WriteLine("Best Key: ");
            Matrix.WriteOnConsoleInt(BestKey);

            int[] CorrectKey = new int[25];
            for (int i = 0; i < 25; i++)
            {
                CorrectKey[i] = i;
            }
            Console.Out.WriteLine("\nCorrect Key Score: {0}", EvaluateKey2(CorrectKey));
            Console.Out.WriteLine("Correct Key: ");
            Matrix.WriteOnConsoleInt(CorrectKey);


            Matrix BestKeyMatrix = new Matrix(Matrix.ConvertToChar(BestKey));

            return(Decrypt(BestKeyMatrix, CipherText));
        }