Changeset 1633


Ignore:
Timestamp:
Jun 14, 2010, 12:58:52 PM (11 years ago)
Author:
nolte
Message:

Modified the code of both Plugins (Vigenere Autokey Analyser & AutokorrelationFunction )...
know they can be read more comfortable.

Location:
trunk/CrypPlugins
Files:
4 edited

Legend:

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

    r1623 r1633  
    4040        private double probablekorr = -999999.999999;                   //initialized probable korrelation of the length
    4141        private String alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";         //used alphabet
    42         private double a;                                               //Found same letter counter
     42        private double same;                                            //Found same letter counter
    4343        private double[] ak;                                            // Autokorrelation Values
    4444
     
    4747        #region Data Properties
    4848
     49        /// <summary>
     50        /// The input for the ciphertext
     51        /// </summary>
    4952        [PropertyInfo(Direction.InputData, "Cipher Input", "Enter your cipher here", "", true, false, DisplayLevel.Beginner, QuickWatchFormat.Text, null)]
    5053        public String InputCipher
     
    6164        }
    6265
     66        /// <summary>
     67        /// The output for the found shift value (most probable keylength)
     68        /// </summary>
    6369        [PropertyInfo(Direction.OutputData, "Keylength Output", "The most probable keylength for the analysed ciphertext", "", DisplayLevel.Beginner)]
    6470        public int OutputLength
     
    8490        }
    8591
    86         /// <summary>
    87         /// HOWTO: You can provide a custom (tabbed) presentation to visualize your algorithm.
    88         /// Return null if you don't provide one.
    89         /// </summary>
    9092        public UserControl Presentation
    9193        {
     
    9395        }
    9496
    95         /// <summary>
    96         /// HOWTO: You can provide custom (quickwatch) presentation to visualize your algorithm.
    97         /// Return null if you don't provide one.
    98         /// </summary>
    9997        public UserControl QuickWatchPresentation
    10098        {
     
    106104        }
    107105
    108         /// <summary>
    109         /// HOWTO: Enter the algorithm you'd like to implement in this method.
    110         /// </summary>
    111106        public void Execute()
    112107        {
    113            
     108
     109//START------------------------------------------------------------------------------------------------------------
     110//Preparations for the Analyse-------------------------------------------------------------------------------------
     111           
    114112            ProgressChanged(0, 1);
    115113
    116 //-------------------------------------------------------------------------------------------------------------------------
    117 
    118             cipher = InputCipher;
    119             cipher = prepareForAnalyse(cipher);
    120 
    121             ak = new double[cipher.Length];
    122                
     114            cipher = InputCipher;                               //initialising the ciphertext
     115            cipher = prepareForAnalyse(cipher);                 //and prepare it for the analyse (-> see private methods section)
     116
     117            ak = new double[cipher.Length];                     //initialise ak[]...there are n possible shifts where n is cipher.length
     118
     119//-----------------------------------------------------------------------------------------------------------------
     120//Analyse----------------------------------------------------------------------------------------------------------
     121//-----------------------------------------------------------------------------------------------------------------             
     122
     123            //for each possible shift value...
    123124                    for(int t=0; t<cipher.Length; t++)
    124125                    {
    125                             a=0;
     126                            same=0;
    126127                       
     128                //...calculate how often the letters match...
    127129                            for(int x=0; x<cipher.Length-t;x++)
    128130                            {
    129131                                    if(cipher[x] == cipher[x+t])
    130132                                    {
    131                                             a++;
     133                                            same++;
    132134                                    }
    133135                            }
    134136                       
    135                             ak[t] = a;
     137                //...and save the count for the matches at the shift position
     138                            ak[t] = same;
    136139                    }
    137140               
     141            //For all observed shifts...
    138142                    for(int y=1;y<ak.Length;y++)
    139143                    {
     144                //find the one with the highest match count...
    140145                            if(ak[y] > probablekorr)
    141146                            {
    142147                                    probablekorr = ak[y];
    143                                     probablelength = y;
     148                    probablelength = y;                 //...and remember this shift value
    144149                            }
    145150                    }
    146151
    147             OutputLength = probablelength;
     152            OutputLength = probablelength;              //sending the keylength via output
    148153            OnPropertyChanged("OutputLength");         
    149154                   
    150 //-------------------------------------------------------------------------------------------------------------------------
     155
    151156
    152157            ProgressChanged(1, 1);
     158
     159//EXECUTE END------------------------------------------------------------------------------------------------------
     160       
    153161        }
    154162
     
    179187//PREPARE PART---------------------------------------------------------------------------------------------------------------------------
    180188
     189        /// <summary>
     190        /// Remove spaces and symbols not provided by the alphabet from the text
     191        /// </summary>
    181192        private String prepareForAnalyse(String c)
    182193        {
     
    196207
    197208
    198 
    199209//---------------------------------------------------------------------------------------------------------------------------------------
    200210//LETTER TO NUMBER----------------------------------------------------------------------------------------------------------------------
    201211
     212        /// <summary>
     213        /// Convert a the letter to an int-value that resembles his position in the given alphabet
     214        /// </summary>
    202215        private int getPos(char c)
    203216        {
     
    215228//---------------------------------------------------------------------------------------------------------------------------------------
    216229
    217         #endregion
     230
     231
     232        #endregion
     233
    218234        #region Event Handling
    219235
  • trunk/CrypPlugins/AutokorrelationFunction/AutokorrelationFunctionSettings.cs

    r1623 r1633  
    2020using Cryptool.PluginBase;
    2121using System.ComponentModel;
     22
     23//------------------------------------------------There is nothing unusual in here------------------------------------------------------------//
    2224
    2325namespace Cryptool.Plugins.AutokorrelationFunction
  • trunk/CrypPlugins/VigenereAutokeyAnalyser/VigenereAutokeyAnalyser.cs

    r1623 r1633  
    6363        #region Data Properties
    6464
    65 
     65        /// <summary>
     66        /// The input for the ciphertext
     67        /// </summary>
    6668        [PropertyInfo(Direction.InputData, "Cipher Input", "Enter your cipher here", "", true, false, DisplayLevel.Beginner, QuickWatchFormat.Text, null)]
    6769        public String InputCipher
     
    7880        }
    7981
     82        /// <summary>
     83        /// The input for the textkorpus (optional)
     84        /// </summary>
    8085        [PropertyInfo(Direction.InputData, "Textkorpus Input", "Enter your sample text here or choose a language from the task-pane", "", false, false, DisplayLevel.Beginner, QuickWatchFormat.Text, null)]
    8186        public String InputKorpus
     
    9297        }
    9398
     99        /// <summary>
     100        /// The assumed keylength from the autokorrelation plugin (optional)
     101        /// </summary>
    94102        [PropertyInfo(Direction.InputData, "Keylength Input", "Enter the assumed keylength from the autokorrelationfunction here", "", false, false, DisplayLevel.Beginner, QuickWatchFormat.Text, null)]
    95103        public int InputKeylength
     
    106114        }
    107115
     116        /// <summary>
     117        /// The output for the key
     118        /// </summary>
    108119        [PropertyInfo(Direction.OutputData, "Key Output", "The most probable autokey for the analysed ciphertext", "", DisplayLevel.Beginner)]
    109120        public String OutputKey
     
    129140        }
    130141
    131         /// <summary>
    132         /// HOWTO: You can provide a custom (tabbed) presentation to visualize your algorithm.
    133         /// Return null if you don't provide one.
    134         /// </summary>
    135142        public UserControl Presentation
    136143        {
     
    138145        }
    139146
    140         /// <summary>
    141         /// HOWTO: You can provide custom (quickwatch) presentation to visualize your algorithm.
    142         /// Return null if you don't provide one.
    143         /// </summary>
    144147        public UserControl QuickWatchPresentation
    145148        {
     
    151154        }
    152155
    153         /// <summary>
    154         /// HOWTO: Enter the algorithm you'd like to implement in this method.
    155         /// </summary>
    156156        public void Execute()
    157157        {
     158//START------------------------------------------------------------------------------------------------------------
     159//Preparations for the Analyse-------------------------------------------------------------------------------------           
     160           
    158161            ProgressChanged(0, 1);
    159162
    160 //Preparations for the Analyse-------------------------------------------------------------------------------------
    161 
    162             alphabet = settings.AlphabetSymbols;
    163 
    164             ciphertext = InputCipher;
    165 
    166             ciphertext = prepareForAnalyse(ciphertext);
    167 
    168             maxkeylength = (ciphertext.Length / 40) + 1;
     163            alphabet = settings.AlphabetSymbols;                //initialising the alphabet as given by the user       
    169164           
    170             language = settings.Language;
    171 
    172             if (textkorpus != null)
    173             {
     165            ciphertext = InputCipher;                           //initialising the ciphertext
     166            ciphertext = prepareForAnalyse(ciphertext);         //and prepare it for the analyse (-> see private methods section)
     167
     168           
     169            language = settings.Language;                       //initialise what language frequencys are expected
     170
     171            if (textkorpus != null)                             //1)  if there's a textkorpus given us it to calculate the expected frequency...
     172            {                                                   //    (-> see private methods section)
    174173                textkorpus = prepareForAnalyse(textkorpus);
    175174                EF = observedFrequency(textkorpus);
    176175            }
    177             else
    178             {
    179                 EF = expectedFrequency(language);
    180             }
     176            else                                                //OR
     177            {
     178                EF = expectedFrequency(language);               //2) just use the expected frequency from the guessed language
     179            }                                                   //    (-> see private methods section)
    181180
    182181
     
    188187
    189188
    190             if (InputKeylength != 0)
    191             {
     189            if (InputKeylength != 0)                            //1) if the autokorrelation function has provided an assumed
     190            {                                                   //   keylength break the AutokeyCipher with it...
    192191                assumedkeylength = InputKeylength;
    193192                breakAutoKey(assumedkeylength);
    194193            }
    195             else
    196             {
     194            else                                                //OR
     195            {
     196                maxkeylength = (ciphertext.Length / 40) + 1;    //2) Brute force the keylength up to (ciphertext.length / 40)
    197197                for (int d = 1; d <= maxkeylength; d++)
    198198                {
    199                     breakAutoKey(d);
    200                 }
    201             }           
    202 
    203             OutputKey = finalkey;
     199                    breakAutoKey(d);                            //"BREAK AUTO KEY(KEYLENGTH)" IS THE MAIN METHODE IN FINDING THE KEY FOR A GIVEN KEYLENGTH
     200                }                                               //(-> see private methods section)
     201            } 
     202           
     203           
     204
     205            OutputKey = finalkey;                               //sending the key via output
    204206            OnPropertyChanged("OutputKey");
    205207
    206 //-----------------------------------------------------------------------------------------------------------------
    207 
    208208            ProgressChanged(1, 1);
    209         }
     209       
     210        }
     211
     212//EXECUTE END------------------------------------------------------------------------------------------------------
    210213
    211214        public void PostExecution()
     
    235238//GET IC---------------------------------------------------------------------------------------------------------------------------------
    236239
     240        /// <summary>
     241        /// Calculate the index of coincidence which is required for the sum of squares analyse
     242        /// </summary>
    237243        private double getIC(String completeplain)
    238244        {
     
    253259//PREPARE PART---------------------------------------------------------------------------------------------------------------------------
    254260
     261        /// <summary>
     262        /// Remove spaces and symbols not provided by the alphabet from the text
     263        /// </summary>
    255264        private String prepareForAnalyse(String c)
    256265        {
     
    274283//SUM OF SQUARES-------------------------------------------------------------------------------------------------------------------------
    275284
     285        /// <summary>
     286        /// The shifted coloumn with the least som of squares is most probable shifted by the correct letter.
     287        /// This letter is part of the key we want to find
     288        /// </summary>
    276289        private double getSumOfSquares(String c, int s)
    277290        {
    278291            String shifted = "";
    279             //Shiften....
    280             shifted = getShift(c, s);
    281 
    282             //frequency ermitteln
    283             OF = observedFrequency(shifted);
     292            shifted = getShift(c, s);                           //"autokey shift" the whole column by the probable key-letter
     293
     294           
     295            OF = observedFrequency(shifted);                    // calculate the observed frequency of the shift
    284296
    285297
    286298            double sum = 0;
    287299            double help;
    288             //Sum ermitteln
    289             for (int letter = 0; letter < alphabet.Length; letter++)
     300
     301            for (int letter = 0; letter < alphabet.Length; letter++)                        //Calculate the sum of squares
    290302            {
    291303                help = (EF[alphabet[letter]] / 100) - OF[alphabet[letter]];
     
    301313//LETTER TO NUMBER----------------------------------------------------------------------------------------------------------------------
    302314
     315        /// <summary>
     316        /// Convert a the letter to an int-value that resembles his position in the given alphabet
     317        /// </summary>
    303318        private int getPos(char c)
    304319        {
     
    317332//SHIFT PART-----------------------------------------------------------------------------------------------------------------------------
    318333
     334        /// <summary>
     335        /// "Autokey shift" the given column by the probable key-letter
     336        /// </summary>
    319337        private String getShift(String c, int s)
    320338        {
     
    336354//---------------------------------------------------------------------------------------------------------------------------------------       
    337355//FREQUENCY ANALYSE PHASE----------------------------------------------------------------------------------------------------------------
    338        
    339        
     356
     357        /// <summary>
     358        /// Calculate the letter frequency of a provided text
     359        /// </summary>
    340360        private double[] observedFrequency(String c)
    341361        {
    342             double[] book = new double[255];    //ASCII Buch merkt sich Anzahl
    343 
    344             for (int x = 0; x < c.Length; x++)  //für jeden buchstaben in er ciphercolumn
     362            double[] book = new double[255];                   //book resembles an ASCII Table and remembers a symbol with his ASCII value
     363
     364            //count the symbols and add 1 in their ASCII Position
     365            for (int x = 0; x < c.Length; x++) 
    345366            {
    346367                book[(int)c[x]]++;
    347368            }
    348369
    349             for (int y = 0; y < book.Length; y++)  //die frequenz ermitteln
     370            //calculate the frequency by dividing through the textlength
     371            for (int y = 0; y < book.Length; y++) 
    350372            {
    351373                book[y] = book[y] / c.Length;
     
    356378
    357379
     380        /// <summary>
     381        /// Set the expected letter frequency of a language
     382        /// </summary>
    358383        private double[] expectedFrequency(int l)
    359384        {
    360             double[] book = new double[255];    //ASCII Buch merkt sich Anzahl
     385            double[] book = new double[255];                   //"ASCII-book" remembers the alphabet letters
    361386            double[] languagefrequency;
    362387
     388            //switch to the expected language and set its frequency
    363389            switch (l)
    364390            {
     
    380406            }
    381407
     408            //set the frequency for the letters in the alphabet
    382409            for (int c = 0; c < 26; c++)
    383410            {
     
    390417
    391418//-----------------------------------------------------------------------------------------------------------------------------------------
    392 //CALCULATION PART (MOST IMPORTANT METHODE)------------------------------------------------------------------------------------------------
    393 
     419//CALCULATION PART: BREAKAUTOKEY METHODE (MOST IMPORTANT METHODE)--------------------------------------------------------------------------
     420
     421        /// <summary>
     422        /// Find the key for a given keylength using "Least Sum of Squares" attack
     423        /// </summary>
    394424        private void breakAutoKey(int d)
    395425        {
    396                 completeplain = "";
    397                 key = "";
    398                 keylength = d;
    399 
    400                 maxfactor = ciphertext.Length / keylength;
    401 
     426                completeplain = "";                             //initialising completeplain,
     427                key = "";                                       //key,
     428                keylength = d;                                  //keylength
     429                maxfactor = ciphertext.Length / keylength;      //and maxfactor
     430
     431                //for all coloumns in a possible keylength
    402432                for (int column = 0; column < keylength; column++)
    403433                {
    404                     String ciphercolumn = "";
    405                     char probablekeychar = 'A';
    406                     sumofsquares = 99999999999.99999999999;
    407 
     434                    String ciphercolumn = "";                   //coloumn is reseted
     435                    char probablekeychar = 'A';                 //probablekeychar is reseted
     436                    sumofsquares = 99999999999.99999999999;     //the sum of squares is reseted
     437
     438                    //A new coloumns is calculated through  c1 , c1 + d , c1 + 2d , c1 + 3d etc.
    408439                    for (int i = 0; i <= maxfactor; i++)
    409440                    {
     
    416447                    ciphercolumn = ciphercolumn.ToUpper();
    417448
     449                    //for this coloumn the Sum Of Squares is calculated for each shift key...
    418450                    for (int shift = 0; shift < alphabet.Length; shift++)
    419451                    {
    420452                        cSofS = getSumOfSquares(ciphercolumn, shift);
    421453
    422 
     454                        //...and compared so the correct one having the least sum can be found
    423455                        if (cSofS < sumofsquares)
    424456                        {
     
    430462                    }
    431463
    432                     completeplain = getShift(ciphercolumn, getPos(probablekeychar)); //merkt sich die entciphertecolumn
    433                     key = key + probablekeychar;
     464                    completeplain = completeplain + getShift(ciphercolumn, getPos(probablekeychar)); //remembers a decrypted cipher
     465                    key = key + probablekeychar;                                                     //remembers the probable key letter of this decryption
    434466
    435467
     
    438470
    439471
    440                 IC = getIC(completeplain);
    441 
     472                IC = getIC(completeplain);                              //calculate the IC(index of coincidence)
     473
     474                //the decrypted cipher with the highest index of coincidence was decrypted with the correct key
    442475                if (IC > finalIC)
    443476                {
    444                     finalIC = IC;
    445                     finalkey = key;
     477                    finalIC = IC;                                       //remember the IC
     478                    finalkey = key;                                     //remember the key
    446479                }
    447480
  • trunk/CrypPlugins/VigenereAutokeyAnalyser/VigenereAutokeyAnalyserSettings.cs

    r1597 r1633  
    2828
    2929        private bool hasChanges = false;
    30         private int language = 0;
    31         private String alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
     30        private int language = 0;                                   //Set the expected language (0: English ; 1: German ; 2: French ; 3: Spain)
     31        private String alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";     //The standard configuration
    3232
    3333        #endregion
     
    3535        #region TaskPane Settings
    3636
     37        /// <summary>
     38        /// Choose the language frequency to work with
     39        /// </summary>
    3740        [ContextMenu("Expected Language", "Select the language you expect the plaintext to be", 2, DisplayLevel.Beginner, ContextMenuControlType.ComboBox, null, new String[] { "English", "German", "French", "Spanish" })]
    3841        [TaskPane("Expected Language", "Select the language you expect the plaintext to be", null, 2, false, DisplayLevel.Experienced, ControlType.ComboBox, new String[] { "English", "German", "French", "Spanish" })]
    39         public int Language // Expected Letter Frequencies
     42        public int Language // Expected letter frequencies
    4043        {
    4144            get { return this.language; }
     
    4851        }
    4952
     53        /// <summary>
     54        /// Choose the alphabet letters to work with
     55        /// </summary>
    5056        [TaskPane("Alphabet", "This is the used alphabet.", null, 7, false, DisplayLevel.Expert, ControlType.TextBox, null)]
    5157        public string AlphabetSymbols
     
    5460            set
    5561            {
    56                 string a = removeEqualChars(value);
    57                 if (a.Length == 0) //cannot accept empty alphabets and will just use the standard;
     62                string a = removeEqualChars(value);     //removes all char that are used twice in the alphabet
     63
     64                if (a.Length == 0)                      //cannot accept empty alphabets and will just use the standard;
    5865                {
    5966                   
     
    9198
    9299        #region Private methods
    93        
     100
     101        /// <summary>
     102        /// Removes all the letters in the provided alphabet that are used twice
     103        /// </summary>
    94104        private string removeEqualChars(string value)
    95105        {
Note: See TracChangeset for help on using the changeset viewer.