Beispiel #1
0
        public void Analyze()
        {
            //Set progress to 50%
            this.pluginProgress(50, 100);

            // 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++)
            {
                Population population = new Population();
                SetUpEnvironment(population, GeneticAttacker.population_size);

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

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

                while ((change > GeneticAttacker.changeBorder) && (curGen < GeneticAttacker.maxGenerations))
                {
                    if (StopFlag)
                    {
                        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);
            }
        }
Beispiel #2
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.grams.CalculateCost(plaintext.ToIntArray());
            String       key_string = CreateAlphabetOutput(key, this.palpha);
            KeyCandidate keyCan     = new KeyCandidate(key, fit, plain, key_string);

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

                // Add key if key does not already exist
                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)
                    {
                        if (!keyCanAlreadyInList.DicAttack)
                        {
                            keyCanAlreadyInList.DicAttack = true;
                            update = true;
                        }
                    }
                    if (keyCan.GenAttack)
                    {
                        if (!keyCanAlreadyInList.GenAttack)
                        {
                            keyCanAlreadyInList.GenAttack = true;
                            update = true;
                        }
                    }
                    if (keyCan.HillAttack)
                    {
                        if (!keyCanAlreadyInList.HillAttack)
                        {
                            keyCanAlreadyInList.HillAttack = true;
                            update = true;
                        }
                    }
                }

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

                    //this.plaintextalphabetoutput = CreateKeyOutput(this.keyCandidates[0].Key, this.ptAlphabet, this.ctAlphabet);
                    //OnPropertyChanged("PlaintextAlphabetOutput");
                    UpdateOutput(this.keyCandidates[0].Key_string, this.keyCandidates[0].Plaintext);

                    ((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 + 1;
                                entry.Text        = keyCandidate.Plaintext;
                                entry.Key         = keyCandidate.Key_string;

                                if (keyCandidate.GenAttack && !keyCandidate.DicAttack)
                                {
                                    entry.Attack = Resources.GenAttackDisplay;
                                }
                                else if (keyCandidate.DicAttack && !keyCandidate.GenAttack)
                                {
                                    entry.Attack = Resources.DicAttackDisplay;
                                }
                                else if (keyCandidate.GenAttack && keyCandidate.DicAttack)
                                {
                                    entry.Attack = Resources.GenAttackDisplay + ", " + Resources.DicAttackDisplay;
                                }
                                else if (keyCandidate.HillAttack)
                                {
                                    entry.Attack = Resources.HillAttackDisplay;
                                }
                                double f    = 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);
            }
        }
Beispiel #4
0
        public void ExecuteOnCPU()
        {
            #region {Variables}

            double globalbestkeycost = double.MinValue;
            int[]  bestkey           = new int[plaintextalphabet.Length];
            inplaceAmountOfSymbols = new int[plaintextalphabet.Length];
            int  alphabetlength = plaintextalphabet.Length; //No need for calculating a few million times. (Performance)
            bool foundbetter;
            bool foundInplace = false;
            totalKeys = 0;
            Random random = new Random();

            #endregion {Variables}

            //Take input and prepare
            //int[] ciphertext = MapTextIntoNumberSpace(RemoveInvalidChars(ciphertextString.ToLower(), ciphertextalphabet), ciphertextalphabet);
            PluginBase.Utils.Alphabet ciphertextAlphabet = new PluginBase.Utils.Alphabet(ciphertextalphabet);
            PluginBase.Utils.Alphabet plaintextAlphabet  = new PluginBase.Utils.Alphabet(plaintextalphabet);
            PluginBase.Utils.Text     cipherText         = new PluginBase.Utils.Text(ciphertextString, ciphertextAlphabet);
            int[] ciphertext = cipherText.ValidLetterArray;

            int   length    = ciphertext.Length;
            int[] plaintext = new int[length];
            inplaceSpots = new int[plaintextalphabet.Length, length];

            for (int restart = 0; restart < restarts; restart++)
            {
                pluginProgress(restart, restarts);

                //Generate random key:
                int[]  runkey      = BuildRandomKey(random);
                double bestkeycost = double.MinValue;

                //Create first plaintext and analyze places of symbols:
                plaintext = UseKeyOnCipher(ciphertext, runkey);
                AnalyzeSymbolPlaces(plaintext, length);

                do
                {
                    foundbetter = false;
                    for (int i = 0; i < alphabetlength; i++)
                    {
                        foundInplace = false;
                        int[] copykey = (int[])runkey.Clone();
                        for (int j = 0; j < alphabetlength; j++)
                        {
                            if (i == j)
                            {
                                continue;
                            }

                            //create child key
                            Swap(copykey, i, j);

                            int sub1 = copykey[i];
                            int sub2 = copykey[j];

                            //Inplace swap in text
                            for (int m = 0; m < inplaceAmountOfSymbols[sub1]; m++)
                            {
                                plaintext[inplaceSpots[sub1, m]] = sub2;
                            }

                            for (int m = 0; m < inplaceAmountOfSymbols[sub2]; m++)
                            {
                                plaintext[inplaceSpots[sub2, m]] = sub1;
                            }

                            //Calculate the costfunction
                            double costvalue = grams.CalculateCost(plaintext);

                            if (bestkeycost < costvalue) //When found a better key adopt it.
                            {
                                bestkeycost  = costvalue;
                                bestkey      = (int[])copykey.Clone();
                                foundbetter  = true;
                                foundInplace = true;
                            }

                            //Revert the CopyKey substitution
                            Swap(copykey, i, j);

                            for (int m = 0; m < inplaceAmountOfSymbols[sub2]; m++)
                            {
                                plaintext[inplaceSpots[sub2, m]] = sub2;
                            }

                            for (int m = 0; m < inplaceAmountOfSymbols[sub1]; m++)
                            {
                                plaintext[inplaceSpots[sub1, m]] = sub1;
                            }

                            totalKeys++; //Count Keys for Performance output
                        }

                        //Fast converge take over new key + therefore new resulting plaintext
                        if (foundInplace)
                        {
                            runkey    = bestkey;
                            plaintext = UseKeyOnCipher(ciphertext, runkey);
                            AnalyzeSymbolPlaces(plaintext, length);
                        }
                    }
                } while (foundbetter);

                if (StopFlag)
                {
                    return;
                }

                if (globalbestkeycost < bestkeycost)
                {
                    globalbestkeycost = bestkeycost;
                    //Add to bestlist-output:
                    string keystring = MapNumbersIntoTextSpace(bestkey, plaintextalphabet);
                    if (keystring.Contains("ABIRD"))
                    {
                        //Console.WriteLine(keystring);
                    }
                    KeyCandidate newKeyCan = new KeyCandidate(bestkey, bestkeycost, MapNumbersIntoTextSpace(UseKeyOnCipher(ciphertext, bestkey), plaintextalphabet), keystring);
                    newKeyCan.HillAttack = true;
                    updateKeyDisplay(newKeyCan);
                }
            }
        }