Ignore:
Timestamp:
Jun 22, 2009, 12:36:54 PM (13 years ago)
Author:
angelov
Message:

New Setting added. It's name is FastAproach. Really Works fine. Fixed some writing errors in the code. More comments coming soon. There is still a bug with shorter texts. The Issue comes from the fact that they do not usually contain all letters from the alphabet. Could be repaired for the next commit.

Location:
trunk/CrypPlugins/MonoalphabeticAnalysis
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/CrypPlugins/MonoalphabeticAnalysis/MonoalphabeticAnalysis.cs

    r332 r365  
    2828    [PluginInfo(false,
    2929    "MonoalphabeticAnalysis",
    30     "Proposes Alphabet for the substitution cypher.",
     30    "Proposes Alphabet for the substitution cipher.",
    3131    "",
    3232    "MonoalphabeticAnalysis/icon.png")]
     
    3939        private string statisticTextFrequencyInput_Monograms="";
    4040        private string statisticTextFrequencyInput_Digrams="";
    41         private string cypherTextFrequencyInput_Monograms="";
    42         private string decypherAttempt_Digrams=""; //After the initial alphabet is proposed and decrypt from substitution plug-in is generated, the result is analysed with Frequencytest concerning digrams and fed back to MonoalphabeticAnalysis as input.
     41        private string cipherTextFrequencyInput_Monograms="";
     42        private string decipherAttempt_Digrams=""; //After the initial alphabet is proposed and decrypt from substitution plug-in is generated, the result is analysed with Frequencytest concerning digrams and fed back to MonoalphabeticAnalysis as input.
    4343        public string GoodAlphabet = null;
    4444        public string NextAlphabet = null;
     
    7474
    7575
    76         [PropertyInfo(Direction.Input, "CypherTextFrequencyInput_Monograms", "CypherTextFrequencyInput_Monograms", "", true, true, DisplayLevel.Beginner, QuickWatchFormat.Text, null)]
    77         public string CypherTextFrequencyInput_Monograms
     76        [PropertyInfo(Direction.Input, "CipherTextFrequencyInput_Monograms", "CipherTextFrequencyInput_Monograms", "", true, true, DisplayLevel.Beginner, QuickWatchFormat.Text, null)]
     77        public string CipherTextFrequencyInput_Monograms
    7878        {
    7979            get
    8080            {
    81                 return cypherTextFrequencyInput_Monograms;
    82             }
    83             set { cypherTextFrequencyInput_Monograms = value; OnPropertyChanged("CypherTextFrequencyInput_Monograms"); }
    84         }
    85 
    86         [PropertyInfo(Direction.Input, "DecypherAttempt_Digrams", "DecypherAttempt_Digrams", "", false, true, DisplayLevel.Beginner, QuickWatchFormat.Text, null)]
    87         public string DecypherAttempt_Digrams
     81                return cipherTextFrequencyInput_Monograms;
     82            }
     83            set { cipherTextFrequencyInput_Monograms = value; OnPropertyChanged("CipherTextFrequencyInput_Monograms"); }
     84        }
     85
     86        [PropertyInfo(Direction.Input, "DecipherAttempt_Digrams", "DecipherAttempt_Digrams", "", false, true, DisplayLevel.Beginner, QuickWatchFormat.Text, null)]
     87        public string DecipherAttempt_Digrams
    8888        {
    8989            get
    9090            {
    91                 return decypherAttempt_Digrams;
    92             }
    93             set { decypherAttempt_Digrams = value; OnPropertyChanged("DecypherAttempt_Digrams"); }
     91                return decipherAttempt_Digrams;
     92            }
     93            set { decipherAttempt_Digrams = value; OnPropertyChanged("DecipherAttempt_Digrams"); }
    9494        }
    9595
     
    171171           
    172172
    173             if (cypherTextFrequencyInput_Monograms != "")
     173            if (cipherTextFrequencyInput_Monograms != "")
    174174            {
    175175                   //if Optional Statistical input not used -> use variables from pre-analysed Text (James Clavel's Shogun) 
     
    177177
    178178
    179                  if (GoodAlphabet == null)
     179                 if (GoodAlphabet == null) //first pass
    180180                 {
    181                     //decypherAttempt_Digrams equals null means initial alphabet should be generated
     181                     //GoodAlphabet equals null means initial alphabet should be generated
    182182                    string alphabet = "";
    183                     ArrayList mapping1 = InitialMapping(cypherTextFrequencyInput_Monograms, statisticTextFrequencyInput_Monograms);
     183                    ArrayList mapping1 = InitialMapping(cipherTextFrequencyInput_Monograms, statisticTextFrequencyInput_Monograms);
    184184
    185185                    alphabet = AlphabetFromMapping(mapping1);
    186186
    187187                    GoodAlphabet = alphabet;
    188                     stringOutput = alphabet;
     188                    stringOutput = MapDecryptKeyAlphabet(alphabet);
    189189                    OnPropertyChanged("StringOutput");
    190190
    191191                 }
    192192
    193 
    194 
    195                  if (GoodAlphabet != null && decypherAttempt_Digrams!="")
     193               
     194
     195                 if (GoodAlphabet != null && decipherAttempt_Digrams!="" && AlphabetcounterB < GoodAlphabet.Length)
    196196                 {
    197197
    198198                     double goodness = ReturnAlphabetGoodness();
    199199                     if (alphabetGoodnes == 0) //secondpass
    200                      {
    201                          alphabetGoodnes = goodness;
     200                     {
     201
     202                         if (settings.FastAproach==1)
     203                         {
     204                             alphabetGoodnes = goodness;
     205                             FastAproach();
     206
     207
     208                         }
     209
     210
     211                         else
     212                         {
     213                             alphabetGoodnes = goodness;
     214                             string alphabet1 = GenerateNextAlphabet();
     215                             NextAlphabet = alphabet1;
     216                             stringOutput = MapDecryptKeyAlphabet(NextAlphabet);
     217                             OnPropertyChanged("StringOutput");
     218                         }
     219                         
     220
     221
     222
    202223                     }
    203                      if (alphabetGoodnes > goodness) //third pass and on if condition applies
     224                     
     225                     if (alphabetGoodnes > goodness && settings.FastAproach==0) //third pass and on
    204226                     {
    205227                         GoodAlphabet = NextAlphabet;
    206228                         alphabetGoodnes = goodness;
     229                         
     230                         string alphabet1 = GenerateNextAlphabet();
     231                         NextAlphabet = alphabet1;
     232                         stringOutput = MapDecryptKeyAlphabet(NextAlphabet);
     233                         OnPropertyChanged("StringOutput");
    207234                     }
    208235
    209                      string alphabet1 = GenerateNextAlphabet();
    210                      NextAlphabet = alphabet1;
    211                      stringOutput = NextAlphabet;
    212                      OnPropertyChanged("StringOutput");
    213 
    214 
    215 
     236                     if (alphabetGoodnes < goodness && settings.FastAproach == 0)
     237                     {
     238                         string alphabet1 = GenerateNextAlphabet();
     239                         NextAlphabet = alphabet1;
     240                         stringOutput = MapDecryptKeyAlphabet(NextAlphabet);
     241                         OnPropertyChanged("StringOutput");
     242                     }
     243
     244                     if (AlphabetcounterB == GoodAlphabet.Length)
     245                     {
     246                         stringOutput = MapDecryptKeyAlphabet(GoodAlphabet);
     247                         OnPropertyChanged("StringOutput");
     248                     }
    216249
    217250                 }
    218251
    219 
     252               
     253               
    220254
    221255            }
     
    320354        {
    321355            if (statisticTextFrequencyInput_Monograms == "") statisticTextFrequencyInput_Monograms = ShogunStatistics.ShogunMonograms;
    322             ArrayList cypherTextFrequencies = ReturnSortedList(cyString, SortElements.SortElemetsBy.byFrequency);
     356            ArrayList cipherTextFrequencies = ReturnSortedList(cyString, SortElements.SortElemetsBy.byFrequency);
    323357            ArrayList statisticFrequencies = ReturnSortedList(statString, SortElements.SortElemetsBy.byFrequency);
    324358
    325359            ArrayList mappings = new ArrayList();
    326360
    327             if (cypherTextFrequencies.Count == statisticFrequencies.Count)
    328             {
    329                 for (int i = 0; i < cypherTextFrequencies.Count ; i++)
     361            if (cipherTextFrequencies.Count == statisticFrequencies.Count)
     362            {
     363                for (int i = 0; i < cipherTextFrequencies.Count ; i++)
    330364                {
    331                     CollectionElement a1 = (CollectionElement)cypherTextFrequencies[i];
     365                    CollectionElement a1 = (CollectionElement)cipherTextFrequencies[i];
    332366                    CollectionElement a2 = (CollectionElement)statisticFrequencies[i];
    333367                    double frequencyDifference = Math.Abs(a1.Frequency - a2.Frequency);
     
    350384        //ALPHABET FROM MAPPING//
    351385
     386
    352387      public string AlphabetFromMapping(ArrayList mapping)
    353388        {
     
    367402
    368403     
    369        //RETURN ALPHABET GOODNESS// Revision Needed!!!
     404       //RETURN ALPHABET GOODNESS//
     405
    370406       
    371407      public double ReturnAlphabetGoodness()
     
    373409          if (statisticTextFrequencyInput_Digrams == "") StatisticTextFrequencyInput_Digrams = ShogunStatistics.ShogunDigrams;//if Optional Statistical input not used -> use variables from pre-analysed Text (James Clavel's Shogun)
    374410
    375           double[,] statisticDigramSquare = GetDigramFrequencySquare(StatisticTextFrequencyInput_Digrams);
    376           double[,] decypherAttemptDigrams = GetDigramFrequencySquare(DecypherAttempt_Digrams);
    377          // double[,] digramFrequencyDifferences = new double[GoodAlphabet.Length, GoodAlphabet.Length];
     411          double[,] statisticDigramSquare = GetDigramFrequencySquare(StatisticTextFrequencyInput_Digrams);   
     412               
     413         
     414          double[,] decipherAttemptDigrams = GetDigramFrequencySquare(DecipherAttempt_Digrams);
     415         
    378416          double goodness=new double();
    379417
     
    383421              for (int n = 0; n < GoodAlphabet.Length; n++)
    384422              {
    385                   goodness +=Math.Abs( statisticDigramSquare[i, n] - decypherAttemptDigrams[i, n]);
     423                  goodness +=Math.Abs( statisticDigramSquare[i, n] - decipherAttemptDigrams[i, n]);
    386424              }
    387425          }
     
    390428          return goodness;
    391429
    392 
    393 
    394 
    395 
    396          /* ArrayList statisticList_Digrams = ReturnSortedList(statisticTextFrequencyInput_Digrams, SortElements.SortElemetsBy.byFrequency);
    397 
    398           ArrayList decypherAttemptList_Digrams = ReturnSortedList(digramsFrequencies, SortElements.SortElemetsBy.byFrequency);
    399 
    400           ArrayList digramFrequencyDifference = statisticList_Digrams;
    401 
    402           for (int i = 0; i < decypherAttemptList_Digrams.Count; i++)
    403           {
    404               CollectionElement a1 = (CollectionElement)decypherAttemptList_Digrams[i];
    405               string name1 = a1.Caption;
    406 
    407               for (int n = 0; n < statisticList_Digrams.Count; n++)
    408               {
    409                   CollectionElement a2 = (CollectionElement)statisticList_Digrams[n];
    410                   string name2 = a2.Caption;
    411                   if (name1 == name2)
    412                   {
    413                       double difference = a2.Frequency - a1.Frequency;
    414                       CollectionElement row = new CollectionElement(name1, difference, "");
    415                       digramFrequencyDifference[n] = (Object)row;
    416                   }
    417                   if (name1[0] != name2[0]) break;
    418               }
    419 
    420 
    421           }
    422           double sumOfDigramFrequencyDifferences = 0;
    423 
    424           for (int m = 0; m < digramFrequencyDifference.Count; m++)
    425           {
    426               CollectionElement z = (CollectionElement)digramFrequencyDifference[m];
    427               sumOfDigramFrequencyDifferences = sumOfDigramFrequencyDifferences + z.Frequency;
    428           }
    429 
    430           return Math.Abs(sumOfDigramFrequencyDifferences); */
    431 
    432          /* decypherAttemptList_Digrams.Reverse();
    433           statisticList_Digrams.Reverse();
    434           int hits=0;
    435 
    436           //CollectionElement[,] darray = new CollectionElement[GoodAlphabet.Length,GoodAlphabet.Length];
    437           for (int i = 0; i < 30; i++)
    438           {
    439               CollectionElement a1 = (CollectionElement)decypherAttemptList_Digrams[i];
    440               CollectionElement a2 = (CollectionElement)statisticList_Digrams[i];
    441 
    442 
    443 
    444               if (a1.Caption == a2.Caption) hits += GoodAlphabet.Length * GoodAlphabet.Length - i * GoodAlphabet.Length;
    445           }
    446 
    447           return hits; */
    448 
    449430      }
    450431
    451432
    452433       //GENERATE NEXT ALPHABET//
     434
    453435
    454436      public string GenerateNextAlphabet ()
     
    456438          //string alphabet = GoodAlphabet;
    457439         
    458           if (AlphabetcounterB != GoodAlphabet.Length)  //TO DO:OTHER WAY TO REALY EXIT
     440          if (AlphabetcounterB != GoodAlphabet.Length) 
    459441          {
    460442              char[] alphabet=GoodAlphabet.ToCharArray();
     
    474456              return alphabet1;
    475457          }
    476           else { return GoodAlphabet; } //TO DO: OTHER WAY TO REALY EXIT
     458          else { return GoodAlphabet; }
    477459
    478460      }
     
    508490
    509491
     492        //FAST APROACH// 
     493
     494
     495
     496      public void FastAproach()
     497      {
     498          if (statisticTextFrequencyInput_Digrams == "") StatisticTextFrequencyInput_Digrams = ShogunStatistics.ShogunDigrams;//if Optional Statistical input not used -> use variables from pre-analysed Text (James Clavel's Shogun)
     499
     500          double[,] statisticDigramSquare = GetDigramFrequencySquare(StatisticTextFrequencyInput_Digrams);
     501          double[,] digramFrequencyAttemptSquare = new double[GoodAlphabet.Length, GoodAlphabet.Length];
     502          digramFrequencyAttemptSquare=GetDigramFrequencySquare(decipherAttempt_Digrams);
     503
     504
     505
     506          double[,] swappingSquare = new double[GoodAlphabet.Length, GoodAlphabet.Length];
     507          swappingSquare.Initialize();
     508         
     509         
     510          while (AlphabetcounterB < GoodAlphabet.Length-1)
     511          {
     512
     513              for (int q = 0; q < GoodAlphabet.Length; q++)
     514              {
     515                  for (int z = 0; z < GoodAlphabet.Length; z++)
     516                  {
     517                      swappingSquare[q, z] = digramFrequencyAttemptSquare[q, z];
     518                  }
     519              }
     520
     521              char[] orderedAlphabet = GoodAlphabet.ToCharArray();
     522              Array.Sort(orderedAlphabet);
     523              int swapIndex1 = Array.IndexOf(orderedAlphabet,GoodAlphabet[AlphabetCounterA-1]);                         //Find Corespondant places in ordered alphabet
     524              int swapIndex2 = Array.IndexOf(orderedAlphabet, GoodAlphabet[AlphabetCounterA + AlphabetcounterB - 1]);
     525             
     526             
     527              //Swap two rows         
     528             
     529              for (int i = 0; i < swappingSquare.GetLength(0); i++)
     530              {
     531                  double holder1 = swappingSquare[swapIndex1, i];
     532                  swappingSquare[swapIndex1 , i] = swappingSquare[swapIndex2, i];
     533                  swappingSquare[swapIndex2, i] = holder1;
     534
     535
     536              }
     537
     538              int breakpoint = 0;
     539
     540              //Swap two columns
     541              for (int n = 0; n < swappingSquare.GetLength(0); n++)
     542              {
     543                  double holder2 = swappingSquare[n, swapIndex1];
     544                  swappingSquare[n,swapIndex1] = swappingSquare[n, swapIndex2];
     545                  swappingSquare[n, swapIndex2] = holder2;
     546             
     547              }
     548
     549                           
     550             
     551              double goodness1 = 0;
     552
     553
     554              for (int m = 0; m < swappingSquare.GetLength(0); m++)
     555              {
     556                  for (int g = 0; g < swappingSquare.GetLength(0); g++)
     557                  {
     558                      goodness1 += Math.Abs(statisticDigramSquare[m, g] - swappingSquare[m, g]);
     559                  }
     560              }
     561
     562              NextAlphabet = GenerateNextAlphabet();
     563
     564              if (goodness1 < alphabetGoodnes)
     565              {
     566                  GoodAlphabet = NextAlphabet;
     567                  alphabetGoodnes = goodness1;
     568
     569                  for (int q = 0; q < GoodAlphabet.Length; q++)
     570                  {
     571                      for (int z = 0; z < GoodAlphabet.Length; z++)
     572                      {
     573                          digramFrequencyAttemptSquare[q, z] = swappingSquare[q, z];
     574                      }
     575                  }
     576              }
     577                           
     578
     579                     
     580
     581
     582          }
     583
     584          stringOutput = MapDecryptKeyAlphabet(GoodAlphabet);
     585          OnPropertyChanged("StringOutput");
     586
     587
     588      }
     589
     590     
     591        //MAPDECRYPTALPHABET//
     592       
     593        public string MapDecryptKeyAlphabet(string alph)
     594        {
     595
     596
     597             string alphabet1 = "";
     598         
     599             char[] AlphabetCharArray = alph.ToCharArray();
     600             char[] orderedAlphabet = alph.ToCharArray();
     601            Array.Sort(orderedAlphabet);
     602         
     603         
     604            for (int i = 0; i < alph.Length; i++)
     605            {
     606                 int mapIndex = Array.IndexOf(AlphabetCharArray, orderedAlphabet[i]);
     607                 alphabet1 += orderedAlphabet[mapIndex];
     608             }
     609
     610             return alphabet1;
     611     
     612        }
     613
     614
     615
    510616        #endregion CUSTOM METHODS
    511617
  • trunk/CrypPlugins/MonoalphabeticAnalysis/MonoalphabeticAnalysisSettings.cs

    r325 r365  
    2020
    2121        #endregion
    22         public int unknownSymbolHandling = 0;
    23         //private int trimInputString=0;
    24         private int caseSensitivity = 0;
    25         private int grammLength = 1;
     22       
     23       
     24        private int fastAproach = 0;
     25       
    2626
    2727        /// <summary>
     
    2929        /// </summary>
    3030        [PropertySaveOrder(1)]
    31         [ContextMenu("Alphabet case sensitivity", "Should upper and lower case be treated differently? (Should a == A)", 7, DisplayLevel.Expert, ContextMenuControlType.ComboBox, null, new string[] { "Case insensitive", "Case sensitive" })]
    32         [TaskPane("Alphabet case sensitivity", "Should upper and lower case be treated differently? (Should a == A)", "", 7, false, DisplayLevel.Expert, ControlType.ComboBox, new string[] { "Case insensitive", "Case sensitive" })]
    33         public int CaseSensitivity
     31        [ContextMenu("Fast Aproach", "Using Fast Aproach dramatically reduce the time needed as only two decryption attempts are made. ", 7, DisplayLevel.Expert, ContextMenuControlType.ComboBox, null, new string[] { "Don't use Fast Aproach", "Use Fast Aproach" })]
     32        [TaskPane("Fast Aproach", "Using Fast Aproach dramatically reduce the time needed as only two decryption attempts are made. ", "", 7, false, DisplayLevel.Expert, ControlType.ComboBox, new string[] { "Don't Use Fast Aproach", "Use Fast Aproach" })]
     33        public int FastAproach
    3434        {
    35             get { return this.caseSensitivity; }
     35            get { return this.fastAproach; }
    3636            set
    3737            {
    38                 if (value != caseSensitivity)
     38                if (value != fastAproach)
    3939                {
    4040                    HasChanges = true;
    41                     caseSensitivity = value;
     41                    fastAproach = value;
    4242                }
    4343
    44                 OnPropertyChanged("CaseSensitivity");
     44                OnPropertyChanged("Fast Aproach");
    4545            }
    4646        }
    47         [PropertySaveOrder(2)]
    48         [TaskPane("Enter the length of the gramms to be investigated.", "Groups of how many characters should be checked?", "", 1, false, DisplayLevel.Expert, ControlType.NumericUpDown, ValidationType.RangeInteger, 0, 100)]
    49         public int GrammLength
    50         {
    51             get { return this.grammLength; }
    52             set
    53             {
    54                 if (value != grammLength)
    55                 {
    56                     HasChanges = true;
    57                     grammLength = value;
    58                 }
    59             }
    60         }
    61         [PropertySaveOrder(3)]
    62         [ContextMenu("Handling of unknown characters", "What should be done with encountered characters at the input which are not in the alphabet?", 4, DisplayLevel.Expert, ContextMenuControlType.ComboBox, null, new string[] { "Don't count", "Count" })]
    63         [TaskPane("Handling of unknown characters", "What should be done with encountered characters at the input which are not in the alphabet?", null, 4, false, DisplayLevel.Expert, ControlType.ComboBox, new string[] { "Don't count", "Count" })]
    64         public int RemoveUnknownSymbols
    65         {
    66             get { return this.unknownSymbolHandling; }
    67             set
    68             {
    69                 if (value != unknownSymbolHandling)
    70                 {
    71                     HasChanges = true;
    72                     unknownSymbolHandling = value;
    73                 }
    7447
    75                 OnPropertyChanged("RemoveUnknownSymbols");
    76             }
    77         }
     48       
     49       
    7850
    7951        #region INotifyPropertyChanged Members
Note: See TracChangeset for help on using the changeset viewer.