Exemple #1
0
        private void AddKeyToResult(Mapping map, bool fullSolution)
        {
            if (fullSolution)
            {
                this.completeKey = true;
            }
            else
            {
                this.partialKey = true;
            }

            int[]        key        = this.MakeKeyComplete(map.DeriveKey());
            Text         plaintext  = this.DecryptCiphertext(key, this.ctext, this.calpha);
            string       plain      = plaintext.ToString(this.palpha);
            double       fit        = this.freq.CalculateFitnessOfKey(plaintext);
            String       key_string = CreateAlphabetOutput(key, this.palpha);
            KeyCandidate keyCan     = new KeyCandidate(key, fit, plain, key_string);

            keyCan.DicAttack = true;
            this.updateKeyDisplay(keyCan);
        }
Exemple #2
0
        private void UpdateKeyDisplay(KeyCandidate keyCan)
        {
            try
            {
                bool update = false;

                // Add key if key does not already exists
                if (!this.keyCandidates.Contains(keyCan))
                {
                    this.keyCandidates.Add(keyCan);
                    this.keyCandidates.Sort(new KeyCandidateComparer());

                    if (this.keyCandidates.Count > 20)
                    {
                        this.keyCandidates.RemoveAt(this.keyCandidates.Count - 1);
                    }
                    update = true;
                }
                else
                {
                    int          index = this.keyCandidates.IndexOf(keyCan);
                    KeyCandidate keyCanAlreadyInList = this.keyCandidates[index];
                    if (keyCan.DicAttack == true)
                    {
                        if (keyCanAlreadyInList.DicAttack == false)
                        {
                            keyCanAlreadyInList.DicAttack = true;
                            update = true;
                        }
                    }
                    if (keyCan.GenAttack == true)
                    {
                        if (keyCanAlreadyInList.GenAttack == false)
                        {
                            keyCanAlreadyInList.GenAttack = true;
                            update = true;
                        }
                    }
                }

                // Display output
                if (update)
                {
                    this.plaintext = this.keyCandidates[0].Plaintext;
                    OnPropertyChanged("Plaintext");

                    this.plaintext_alphabet_output = CreateAlphabetOutput(this.keyCandidates[0].Key, this.ctAlphabet);
                    OnPropertyChanged("Plaintext_Alphabet_Output");

                    ((AssignmentPresentation)Presentation).Dispatcher.Invoke(DispatcherPriority.Normal,
                                                                             (SendOrPostCallback) delegate
                    {
                        try
                        {
                            ((AssignmentPresentation)Presentation).entries.Clear();

                            for (int i = 0; i < this.keyCandidates.Count; i++)
                            {
                                KeyCandidate keyCandidate = this.keyCandidates[i];

                                ResultEntry entry = new ResultEntry();
                                entry.Ranking     = i.ToString();
                                entry.Text        = keyCandidate.Plaintext;
                                entry.Key         = keyCandidate.Key_string;

                                if (keyCandidate.GenAttack == true && keyCandidate.DicAttack == false)
                                {
                                    entry.Attack = Resources.GenAttackDisplay;
                                }
                                else if (keyCandidate.DicAttack == true && keyCandidate.GenAttack == false)
                                {
                                    entry.Attack = Resources.DicAttackDisplay;
                                }
                                else if (keyCandidate.GenAttack == true && keyCandidate.DicAttack == true)
                                {
                                    entry.Attack = Resources.GenAttackDisplay + ", " + Resources.DicAttackDisplay;
                                }

                                double f    = Math.Log10(Math.Abs(keyCandidate.Fitness));
                                entry.Value = string.Format("{0:0.00000} ", f);
                                ((AssignmentPresentation)Presentation).entries.Add(entry);
                            }
                        }
                        catch (Exception ex)
                        {
                            GuiLogMessage("Exception during UpdateKeyDisplay Presentation.Dispatcher: " + ex.Message, NotificationLevel.Error);
                        }
                    }, null);
                }
            }
            catch (Exception ex)
            {
                GuiLogMessage("Exception during UpdateKeyDisplay: " + ex.Message, NotificationLevel.Error);
            }
        }
Exemple #3
0
        public void Analyze()
        {
            //Set progress to 50%
            this.pluginProgress(50.0, 100.0);

            // Adjust analyzer parameters to ciphertext length
            AdjustAnalyzerParameters(this.ciphertext.Length);
            this.currun_keys_tested = 0;

            // Initialization of repetition data structures
            int[][]  bestkeys     = new int[this.repetitions][];
            double[] bestkeys_fit = new double[this.repetitions];

            // Execute analysis
            for (int curRep = 0; curRep < this.repetitions; curRep++)
            {
                if (this.stopFlag == true)
                {
                    return;
                }

                Population population = new Population();
                SetUpEnvironment(population, GeneticAttacker.population_size);

                CreateInitialGeneration(population, this.ciphertext, this.ciphertext_alphabet, this.plaintext_alphabet, null);

                double     change  = population.dev;
                int        curGen  = 1;
                Population nextGen = population;

                while ((change > GeneticAttacker.changeBorder) && (curGen < GeneticAttacker.maxGenerations))
                {
                    if (this.stopFlag == true)
                    {
                        return;
                    }

                    nextGen = CreateNextGeneration(nextGen, this.ciphertext, this.ciphertext_alphabet, false);
                    change  = nextGen.dev;
                    curGen++;

                    this.pluginProgress(50.0 + (((double)(curRep + 1) * curGen) / (this.repetitions * GeneticAttacker.maxGenerations)) / 2 * 100, 100.0);
                }

                nextGen = CreateNextGeneration(nextGen, this.ciphertext, this.ciphertext_alphabet, true);

                this.plaintext = DecryptCiphertext(nextGen.keys[0], this.ciphertext, this.ciphertext_alphabet);

                bestkeys[curRep]     = nextGen.keys[0];
                bestkeys_fit[curRep] = nextGen.fitness[0];

                Text   plainTxt   = DecryptCiphertext(bestkeys[curRep], this.ciphertext, this.ciphertext_alphabet);
                String plain      = plainTxt.ToString(this.plaintext_alphabet);
                String key_string = CreateAlphabetOutput(bestkeys[curRep], this.plaintext_alphabet);

                // Report keyCan
                KeyCandidate newKeyCan = new KeyCandidate(bestkeys[curRep], bestkeys_fit[curRep], plain, key_string);
                newKeyCan.GenAttack = true;
                this.updateKeyDisplay(newKeyCan);
            }
        }