예제 #1
0
 public void Merge(HighscoreList list)
 {
     foreach (var v in list)
     {
         if (!Add(v))
         {
             return;
         }
     }
 }
예제 #2
0
        public void Execute()
        {
            if (this.input == null)
            {
                GuiLogMessage("No input!", NotificationLevel.Error);
                return;
            }

            if (this.ControlMaster == null)
            {
                GuiLogMessage("You have to connect the Transposition component to the Transpostion Analyzer control!", NotificationLevel.Error);
                return;
            }

            if (this.costMaster == null)
            {
                GuiLogMessage("You have to connect the Cost Function component to the Transpostion Analyzer control!", NotificationLevel.Error);
                return;
            }

            comparer = new ValueKeyComparer(costMaster.GetRelationOperator() != RelationOperator.LargerThen);
            TOPLIST  = new HighscoreList(comparer, 10);

            myPresentation.Dispatcher.BeginInvoke(DispatcherPriority.Normal, (SendOrPostCallback) delegate { myPresentation.entries.Clear(); }, null);

            switch (this.settings.Analysis_method)
            {
            case 0: GuiLogMessage("Starting Brute-Force Analysis", NotificationLevel.Info); BruteforceAnalysis(); break;

            case 1: GuiLogMessage("Starting Crib Analysis", NotificationLevel.Info); CribAnalysis(crib, input); break;

            case 2: GuiLogMessage("Starting Genetic Analysis", NotificationLevel.Info); GeneticAnalysis(); break;

            case 3: GuiLogMessage("Starting Hill Climbing Analysis", NotificationLevel.Info); HillClimbingAnalysis(); break;
            }

            ProgressChanged(1, 1);
        }
예제 #3
0
        private void HillClimbingAnalysis()
        {
            if (settings.Iterations < 2 || settings.KeySize < 2)
            {
                GuiLogMessage("Check keylength and iterations", NotificationLevel.Error);
                return;
            }

            DateTime startTime  = DateTime.Now;
            DateTime nextUpdate = DateTime.Now.AddMilliseconds(100);

            HighscoreList ROUNDLIST = new HighscoreList(comparer, 10);

            ValueKey vk = new ValueKey();

            ulong totalKeys = (ulong)settings.Repeatings * (ulong)settings.Iterations;
            ulong doneKeys  = 0;

            stop = false;

            for (int repeating = 0; repeating < settings.Repeatings; repeating++)
            {
                if (stop)
                {
                    break;
                }

                ROUNDLIST.Clear();

                byte[] key    = randomArray(settings.KeySize);
                byte[] oldkey = new byte[settings.KeySize];

                for (int iteration = 0; iteration < settings.Iterations; iteration++)
                {
                    if (stop)
                    {
                        break;
                    }

                    Array.Copy(key, oldkey, key.Length);

                    int r = rd.Next(100);
                    if (r < 50)
                    {
                        for (int i = 0; i < rd.Next(10); i++)
                        {
                            swap(key, rd.Next(key.Length), rd.Next(key.Length));
                        }
                    }
                    else if (r < 70)
                    {
                        for (int i = 0; i < rd.Next(3); i++)
                        {
                            int l = rd.Next(key.Length - 1) + 1;
                            int f = rd.Next(key.Length);
                            int t = (f + l + rd.Next(key.Length - l)) % key.Length;
                            blockswap(key, f, t, l);
                        }
                    }
                    else if (r < 90)
                    {
                        int l = 1 + rd.Next(key.Length - 1);
                        int f = rd.Next(key.Length);
                        int t = (f + 1 + rd.Next(key.Length - 1)) % key.Length;
                        blockshift(key, f, t, l);
                    }
                    else
                    {
                        pivot(key, rd.Next(key.Length - 1) + 1);
                    }

                    decrypt(vk, key);

                    if (ROUNDLIST.Add(vk))
                    {
                        if (TOPLIST.isBetter(vk))
                        {
                            TOPLIST.Add(vk);
                            Output = vk.plaintext;
                        }
                    }
                    else
                    {
                        Array.Copy(oldkey, key, key.Length);
                    }

                    doneKeys++;

                    if (DateTime.Now >= nextUpdate)
                    {
                        TOPLIST.Merge(ROUNDLIST);
                        UpdatePresentationList(totalKeys, doneKeys, startTime);
                        nextUpdate = DateTime.Now.AddMilliseconds(1000);
                    }
                }
            }

            TOPLIST.Merge(ROUNDLIST);
            UpdatePresentationList(totalKeys, doneKeys, startTime);
        }
예제 #4
0
        private void GeneticAnalysis()
        {
            if (settings.Iterations < 2 || settings.KeySize < 2 || settings.Repeatings < 1)
            {
                GuiLogMessage("Check keylength and iterations", NotificationLevel.Error);
                return;
            }

            ValueKey vk = new ValueKey();

            DateTime startTime  = DateTime.Now;
            DateTime nextUpdate = DateTime.Now.AddMilliseconds(100);

            HighscoreList ROUNDLIST = new HighscoreList(comparer, 12);

            ulong totalKeys = (ulong)settings.Repeatings * (ulong)settings.Iterations * 6;
            ulong doneKeys  = 0;

            stop = false;

            for (int repeating = 0; repeating < settings.Repeatings; repeating++)
            {
                if (stop)
                {
                    break;
                }

                ROUNDLIST.Clear();

                for (int i = 0; i < ROUNDLIST.Capacity; i++)
                {
                    ROUNDLIST.Add(createKey(randomArray(settings.KeySize)));
                }

                for (int iteration = 0; iteration < settings.Iterations; iteration++)
                {
                    if (stop)
                    {
                        break;
                    }

                    // Kinder der besten Keys erstellen
                    int rndInt = 0;

                    for (int a = 0; a < 6; a++)
                    {
                        if (a % 2 == 0)
                        {
                            rndInt = rd.Next(settings.KeySize - 1) + 1;
                        }

                        // combine DNA of two parents
                        ValueKey parent1 = ROUNDLIST[a];
                        ValueKey parent2 = ROUNDLIST[(a % 2 == 0) ? a + 1 : a - 1];

                        byte[] child = new byte[parent1.key.Length];
                        Array.Copy(parent1.key, child, rndInt);

                        int pos = rndInt;
                        for (int b = 0; b < parent2.key.Length; b++)
                        {
                            for (int c = rndInt; c < parent1.key.Length; c++)
                            {
                                if (parent1.key[c] == parent2.key[b])
                                {
                                    child[pos] = parent1.key[c];
                                    pos++;
                                    break;
                                }
                            }
                        }

                        // add a single mutation
                        int apos = rd.Next(settings.KeySize);
                        int bpos = (apos + rd.Next(1, settings.KeySize)) % settings.KeySize;
                        swap(child, apos, bpos);

                        decrypt(vk, child);

                        ROUNDLIST.Add(vk);

                        if (TOPLIST.isBetter(vk))
                        {
                            TOPLIST.Add(vk);
                            Output = vk.plaintext;
                        }

                        doneKeys++;
                    }

                    if (DateTime.Now >= nextUpdate)
                    {
                        TOPLIST.Merge(ROUNDLIST);
                        UpdatePresentationList(totalKeys, doneKeys, startTime);
                        nextUpdate = DateTime.Now.AddMilliseconds(1000);
                    }
                }
            }

            TOPLIST.Merge(ROUNDLIST);
            UpdatePresentationList(totalKeys, doneKeys, startTime);
        }