예제 #1
0
 public void should_compare(WordInfo word1, WordInfo word2, int result)
 {
     if (result == 0)
     {
         _comparer.Compare(word1, word2).Should().Be(0);
     }
     else if (result < 0)
     {
         _comparer.Compare(word1, word2).Should().BeLessThan(0);
     }
     else
     {
         _comparer.Compare(word1, word2).Should().BeGreaterThan(0);
     }
 }
예제 #2
0
        public void PrepareAttack()
        {
            // Test input
            if (this.calpha == null || this.ctext == null || this.freq == null || this.lDic == null)
            {
                return;
            }

            List <Byte[]> text_in_words = this.ctext.ToSingleWords(this.calpha);

            // Add only unigue words
            WordComparer word_comparer = new WordComparer();

            for (int i = 0; i < text_in_words.Count; i++)
            {
                Word w         = new Word(i, text_in_words[i]);
                bool vorhanden = false;
                for (int j = 0; j < this.words.Count; j++)
                {
                    if (word_comparer.Compare(w, this.words[j]) == 0)
                    {
                        vorhanden = true;
                    }
                }

                if (vorhanden == false)
                {
                    this.words.Add(w);
                }

                if (this.stopFlag == true)
                {
                    return;
                }
            }

            // Look up words with same pattern in dictionary
            foreach (Word w in words)
            {
                w.Candidates = GetCandidates(w);
                if (w.Candidates == null || w.Candidates.Length == 0)
                {
                    w.Enabled = false;
                }
            }

            // Generate order of ciphertext letters
            int[] letter_frequencies = new int[this.calpha.GetAlphabetQuantity()];

            foreach (Word w in this.words)
            {
                for (int i = 0; i < w.ByteValue.Length; i++)
                {
                    letter_frequencies[w.ByteValue[i]]++;
                }
            }
            this.histogram = letter_frequencies;

            this.historder = new byte[letter_frequencies.Length];
            for (int i = 0; i < this.historder.Length; i++)
            {
                this.historder[i] = (byte)i;
            }

            for (int i = 0; i < letter_frequencies.Length; i++)
            {
                for (int j = i + 1; j < letter_frequencies.Length; j++)
                {
                    if (letter_frequencies[j] > letter_frequencies[i])
                    {
                        int helper = letter_frequencies[i];
                        letter_frequencies[i] = letter_frequencies[j];
                        letter_frequencies[j] = helper;
                        helper            = this.historder[i];
                        this.historder[i] = this.historder[j];
                        this.historder[j] = (byte)helper;
                    }
                }
            }

            // Calculate max progress
            int nr = this.words.Count;
            int v1 = 0;
            int v2 = 0;

            if (nr < 200)
            {
                for (int i = 0; i < nr; i++)
                {
                    if (nr < 70)
                    {
                        for (int j = i + 1; j < nr; j++)
                        {
                            if (nr < 30)
                            {
                                for (int t = j + 1; t < nr; t++)
                                {
                                    v2++;
                                }
                            }
                            v1++;
                        }
                    }
                }
            }
            this.maxProgressIt = (1 + nr + v1 + v2 + 300);
        }