Exemple #1
0
        public void Execute()
        {
            this.genAttacker = new GeneticAttacker();
            this.dicAttacker = new DictionaryAttacker();

            String  alpha   = "";
            Boolean inputOK = true;

            // Clear presentation
            ((AssignmentPresentation)Presentation).Dispatcher.Invoke(DispatcherPriority.Normal, (SendOrPostCallback) delegate
            {
                ((AssignmentPresentation)Presentation).entries.Clear();
            }, null);

            // Prepare the cryptanalysis of the ciphertext

            // Set alphabet
            alpha           = detAlphabet(settings.Alphabet);
            this.ptAlphabet = new Alphabet(alpha, 1, settings.Alphabet);
            this.ctAlphabet = new Alphabet(alpha, 1, settings.Alphabet);

            // N-gram probabilities
            String helper = IdentifyNGramFile(settings.Alphabet);

            if (helper != null)
            {
                this.langFreq = new Frequencies(this.ptAlphabet);
                this.langFreq.ReadProbabilitiesFromNGramFile(helper);
            }
            else
            {
                GuiLogMessage(Resources.no_ngram_file, NotificationLevel.Error);
            }

            // Dictionary
            if (settings.Alphabet == 0)
            {
                try
                {
                    this.langDic = new Dictionary("en-small.dic", ctAlphabet.Length);
                }
                catch (Exception ex)
                {
                    GuiLogMessage(Resources.error_dictionary + " :" + ex.Message, NotificationLevel.Error);
                }
            }
            else if (settings.Alphabet == 1)
            {
                try
                {
                    this.langDic = new Dictionary("de-small.dic", ctAlphabet.Length);
                }
                catch (Exception ex)
                {
                    GuiLogMessage(Resources.error_dictionary + " :" + ex.Message, NotificationLevel.Error);
                }
            }
            // Add new case for another language
            // elseif (settings.Alphabet == 1)
            // {
            // ......
            // }

            // Set ciphertext
            String helper1 = null;

            try
            {
                //helper1 = returnStreamContent(this.ciphertext);
                if (this.ciphertext.Length != 0)
                {
                    helper1 = this.ciphertext;
                }
            }
            catch
            {
                GuiLogMessage(Resources.error_ciphertext, NotificationLevel.Error);
            }
            if (helper1 != null)
            {
                this.cText = new Text(helper1, this.ctAlphabet, settings.TreatmentInvalidChars);
            }
            else
            {
                this.cText = null;
            }



            // PTAlphabet correct?
            if (this.ptAlphabet == null)
            {
                GuiLogMessage(Resources.no_plaintext_alphabet, NotificationLevel.Error);
                inputOK = false;
            }
            // CTAlphabet correct?
            if (this.ctAlphabet == null)
            {
                GuiLogMessage(Resources.no_ciphertext_alphabet, NotificationLevel.Error);
                inputOK = false;
            }
            // Ciphertext correct?
            if (this.cText == null)
            {
                GuiLogMessage(Resources.no_ciphertext, NotificationLevel.Error);
                inputOK = false;
            }
            // Dictionary correct?
            if (this.langDic == null)
            {
                GuiLogMessage(Resources.no_dictionary, NotificationLevel.Warning);
            }
            // Language frequencies
            if (this.langFreq == null)
            {
                GuiLogMessage(Resources.no_lang_freq, NotificationLevel.Error);
                inputOK = false;
            }
            // Check length of ciphertext and plaintext alphabet
            if (this.ctAlphabet.Length != this.ptAlphabet.Length)
            {
                GuiLogMessage(Resources.error_alphabet_length, NotificationLevel.Error);
                inputOK = false;
            }

            // If input incorrect return otherwise execute analysis
            lock (this.stopFlag)
            {
                if (this.stopFlag.Stop == true)
                {
                    return;
                }
            }


            if (inputOK == false)
            {
                inputOK = true;
                return;
            }
            else
            {
                this.UpdateDisplayStart();
                //this.masPresentation.DisableGUI();
                this.masPresentation.UpdateOutputFromUserChoice = this.UpdateOutput;
                this.keyCandidates = new List <KeyCandidate>();
                if (this.langDic == null)
                {
                    AnalyzeGenetic();
                }
                else
                {
                    AnalyzeDictionary();
                    AnalyzeGenetic();
                }
                //this.masPresentation.EnableGUI();
                this.UpdateDisplayEnd();
            }
            //set final plugin progress to 100%:
            OnPluginProgressChanged(this, new PluginProgressEventArgs(1.0, 1.0));
        }
Exemple #2
0
        private void CreateInitialGeneration(Population pop, Text ciphertext, Alphabet cipher_alpha, Alphabet plain_alpha, Frequencies freq)
        {
            // Create initial population keys
            int[] newkey;
            int   keylength = cipher_alpha.Length;

            // Create the other population keys at random
            for (int i = 0; i < pop.keys.Length; i++)
            {
                newkey = this.CreateInitialKeyRandom(keylength);
                while (this.banlist.Contains(newkey))
                {
                    newkey = this.CreateInitialKeyRandom(keylength);
                }
                this.banlist.Add(newkey);
                pop.keys[i] = newkey;
            }

            // Calculate fitness of population keys
            for (int i = 0; i < pop.keys.Length; i++)
            {
                pop.fitness[i] = this.language_frequencies.CalculateFitnessOfKey(DecryptCiphertext(pop.keys[i], ciphertext, cipher_alpha));
            }

            // Sort keys according to their fitness
            int[]  helper1;
            double helper2;

            for (int i = 0; i < pop.keys.Length; i++)
            {
                for (int j = 0; j < pop.keys.Length; j++)
                {
                    if (pop.fitness[i] > pop.fitness[j])
                    {
                        helper1        = pop.keys[i];
                        pop.keys[i]    = pop.keys[j];
                        pop.keys[j]    = helper1;
                        helper2        = pop.fitness[i];
                        pop.fitness[i] = pop.fitness[j];
                        pop.fitness[j] = helper2;
                    }
                }
            }

            // Calculate change in development
            pop.fit_lastbestkey = pop.fitness[0];
            pop.dev             = Math.Abs(pop.fitness[0]);
        }