Beispiel #1
0
 public void Merge(HighscoreList list)
 {
     foreach (var v in list)
     {
         if (!Add(v))
         {
             return;
         }
     }
 }
Beispiel #2
0
        public void Execute()
        {
            //Finished = false;

            Bigrams.InitLanguage(settings.Language);

            if (this.input == null)
            {
                GuiLogMessage("No input!", 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);
            comparer = new ValueKeyComparer(false);
            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 Dictionary Attack", NotificationLevel.Info); DictionaryAttack(); break;

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

            //Finished = true;
            Output = TOPLIST[0].plaintext;
            //OnPropertyChanged("Output");

            ProgressChanged(1, 1);
        }
Beispiel #3
0
        private void HillClimbingAnalysis()
        {
            if (settings.Iterations < 2)
            {
                GuiLogMessage("Check iterations.", NotificationLevel.Error);
                return;
            }

            if (settings.Key1Size < 2)
            {
                GuiLogMessage("The minimum size for key 1 is 2.", NotificationLevel.Error);
                return;
            }

            if (settings.Key2Size < 2)
            {
                GuiLogMessage("The minimum size for key 2 is 2.", NotificationLevel.Error);
                return;
            }

            computeKey1MinMaxColEnding(this.input.Length, settings.Key1Size);
            byte[] mybuffer = new byte[this.input.Length];

            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.Key2Size);
                byte[] oldkey = new byte[settings.Key2Size];

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

                    vk.key = key;
                    decrypt2(vk.key, vk.key.Length, this.input, this.input.Length, mybuffer);
                    vk.plaintext = mybuffer;
                    vk.score     = evalIDPKey2(vk.plaintext, settings.Key1Size);
                    vk.keyphrase = getKeywordFromKey(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);
        }
Beispiel #4
0
        private void DictionaryAttack()
        {
            if (this.Keywords == null || this.Keywords.Length == 0)
            {
                GuiLogMessage("Check dictionary", NotificationLevel.Error);
                return;
            }

            if (settings.Key1Min < 2)
            {
                GuiLogMessage("The minimum size for key 1 is 2.", NotificationLevel.Error);
                return;
            }

            if (settings.Key1Max < settings.Key1Min)
            {
                GuiLogMessage("The maximum size for key 1 must be bigger than the minimum size.", NotificationLevel.Error);
                return;
            }

            if (settings.Key2Min < 2)
            {
                GuiLogMessage("The minimum size for key 2 is 2.", NotificationLevel.Error);
                return;
            }

            if (settings.Key2Max < settings.Key2Min)
            {
                GuiLogMessage("The maximum size for key 2 must be bigger than the minimum size.", 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 = 0;

            foreach (var keyword in this.Keywords)
            {
                if (keyword.Length >= settings.Key2Min && keyword.Length <= settings.Key2Max)
                {
                    totalKeys++;
                }
            }
            totalKeys *= (ulong)(settings.Key1Max - settings.Key1Min + 1);
            ulong doneKeys = 0;

            stop = false;

            byte[] mybuffer = new byte[this.input.Length];

            for (int key1size = settings.Key1Min; key1size <= settings.Key1Max; key1size++)
            {
                computeKey1MinMaxColEnding(this.input.Length, key1size);

                for (int key2size = settings.Key2Min; key2size <= settings.Key2Max; key2size++)
                {
                    byte[] key2 = new byte[key2size];

                    foreach (var keyword in this.Keywords)
                    {
                        if (stop)
                        {
                            break;
                        }

                        if (keyword.Length != key2size)
                        {
                            continue;
                        }
                        getKeyFromKeyword(keyword, key2, key2size);

                        //decrypt(vk, key);
                        vk.key       = key2;
                        vk.keyphrase = keyword;
                        decrypt2(vk.key, vk.key.Length, this.input, this.input.Length, mybuffer);
                        vk.plaintext = mybuffer;
                        vk.score     = evalIDPKey2(vk.plaintext, key1size);

                        //if (TOPLIST.isBetter(vk))
                        //{
                        //    byte[] tmp = new byte[vk.plaintext.Length];
                        //    Array.Copy(vk.plaintext, tmp, vk.plaintext.Length);
                        //    Output = tmp;
                        //}

                        TOPLIST.Add(vk);

                        doneKeys++;

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

                    UpdatePresentationList(totalKeys, doneKeys, startTime);
                }
            }
        }