Ignore:
Timestamp:
May 2, 2009, 6:29:32 PM (13 years ago)
Author:
Arno Wacker
Message:
  • Code cleanup for Friedman test.
  • Added some more error checking and some more info messages in log (i.e. what it gets and what it processes)
  • Added new output (double) for the index of coincidences (IC) of analyzed letters
  • Added possibility to see double values in quickwatch window
  • Extended string output to output more information, i.e. probable key length, IC analyzed, IC provided and suggestion if analyzed string was mono or polyalphabetic. The output follows a well define format of key = value, hence it can be parsed if necessary.
  • Adapted samples to reflect changes.
Location:
trunk/CrypPlugins/FriedmanTest
Files:
2 edited

Legend:

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

    r298 r300  
    77using System.ComponentModel;
    88using System.Collections;
     9using Cryptool.PluginBase.Miscellaneous;
    910
    1011namespace FriedmanTest
     
    1718            "FriedmanTest/friedman.png")]
    1819    public class FriedmanTest : IStatistic
    19     {public FriedmanTest()
     20    {
     21        public FriedmanTest()
    2022        {
    2123            settings = new FriedmanTestSettings();
    22 
    23         }
    24     #region Private Variables
    25     private double keyLength;
    26     private string stringOutput="";
    27     private int [] arrayInput;
    28     #endregion
    29 
    30 
    31     #region Properties (Inputs/Outputs)
    32 
    33     [PropertyInfo(Direction.Output,"Probable key length.", "For greater accuracy, please refer to the string output.", "",false , false, DisplayLevel.Beginner, QuickWatchFormat.Text, null)]
    34     public double KeyLength
    35     {
    36         get { return keyLength; }
    37         set
    38         {
    39             if (value != keyLength)
     24        }
     25
     26        #region Private Variables
     27
     28        private FriedmanTestSettings settings;
     29        private double keyLength = 0;
     30        private double kappaCiphertext = 0;
     31        private string stringOutput="";
     32        private int [] arrayInput;
     33
     34        #endregion
     35
     36        #region Private methods
     37
     38        private void ShowStatusBarMessage(string message, NotificationLevel logLevel)
     39        {
     40            EventsHelper.GuiLogMessage(OnGuiLogNotificationOccured, this, new GuiLogEventArgs(message, this, logLevel));
     41        }
     42
     43        private void ShowProgress(double value, double max)
     44        {
     45            EventsHelper.ProgressChanged(OnPluginProgressChanged, this, new PluginProgressEventArgs(value, max));
     46        }
     47
     48        #endregion
     49
     50        #region Properties (Inputs/Outputs)
     51
     52        [PropertyInfo(Direction.Output, "Results", "This ouput provides the results of the Friedman test \nas key/value pairs, one per each line.", "", false,false, DisplayLevel.Beginner, QuickWatchFormat.Text,null)]
     53        public string StringOutput
     54        {
     55            get { return this.stringOutput; }
     56            set { }
     57        }
     58
     59        [PropertyInfo(Direction.Output, "Probable key length value.", "This value gives the probable key length of the analyzed text.", "", false, false, DisplayLevel.Beginner, QuickWatchFormat.Text, "KeyLengthQuickWatchConverter")]
     60        public double KeyLength
     61        {
     62            get { return keyLength; }
     63            set { }
     64        }
     65
     66        [PropertyInfo(Direction.Output, "Index of coincidence (IC)", "This output provides the calculated index of coincidence of the provided \noccurences of letters (frequency analysis).", "", false, false, DisplayLevel.Expert, QuickWatchFormat.Text, "KappaCiphertextQuickWatchConverter")]
     67        public double KappaCiphertext
     68        {
     69            get { return kappaCiphertext; }
     70            set { }
     71        }
     72
     73        [PropertyInfo(Direction.Input, "Letter count input", "This input accepts a list (array) with the absolute number of letter \noccurences in an encrypted text. This list can be generated e.g. with the frequency test.", "", true, false, DisplayLevel.Beginner, QuickWatchFormat.None, null)]
     74        public int [] ArrayInput
     75        {
     76            get { return arrayInput; }
     77            set
    4078            {
    41                 keyLength = value;
    42                 OnPropertyChanged("KeyLength");
     79                arrayInput = value;
     80                OnPropertyChanged("ArrayInput");
    4381            }
    4482        }
    45     }
    46 
    47     [PropertyInfo(Direction.Output, "Probable key length", "If the key length result seems to be ilogical...", "", false,false, DisplayLevel.Beginner, QuickWatchFormat.Text,null)]
    48     public string StringOutput
    49     {
    50         get { return this.stringOutput; }
    51         set
    52         {
    53             stringOutput = value;
    54             OnPropertyChanged("StringOutput");
    55         }
    56     }
    57     [PropertyInfo(Direction.Input, "List input", "absolute frequency of the letter, as calculated by FrequencyTest", "", false, false, DisplayLevel.Beginner, QuickWatchFormat.Text, null)]
    58     public int [] ArrayInput
    59     {
    60         get { return arrayInput; }
    61         set
    62         {
    63             arrayInput = value;
    64             OnPropertyChanged("ArrayInput");
    65 
    66         }
    67     }
    68     #endregion
    69 
    70 
    71     #region IPlugin Members
    72 
    73     public event StatusChangedEventHandler OnPluginStatusChanged;
    74 
     83        #endregion
     84
     85        #region Input/Output convertor
     86
     87        public object KappaCiphertextQuickWatchConverter(string PropertyNameToConvert)
     88        {
     89            return kappaCiphertext.ToString();
     90        }
     91
     92        public object KeyLengthQuickWatchConverter(string PropertyNameToConvert)
     93        {
     94            return keyLength.ToString();
     95        }
     96
     97        #endregion
     98
     99        #region IPlugin Members
     100
     101        public event StatusChangedEventHandler OnPluginStatusChanged;
    75102        public event GuiLogNotificationEventHandler OnGuiLogNotificationOccured;
    76 
    77103        public event PluginProgressChangedEventHandler OnPluginProgressChanged;
    78104
    79         private FriedmanTestSettings settings;
     105       
    80106        public ISettings Settings
    81107        {
     
    96122        public void PreExecution()
    97123        {
    98             //throw new NotImplementedException();
     124            //nothing to do
    99125        }
    100126
    101127        public void Execute()
    102128        {
    103            
    104129            if (arrayInput != null)
    105130            {
     
    107132                                long cipherTextLength = 0; //n
    108133                                long countDoubleCharacters = 0;
     134                string ciphermode = "monoalphabetic/cleartext";
    109135
    110136                                //Now we set the Kappa plain-text coefficient. Default is English.
     
    118144                    default: Kp = 0.0667; break;
    119145                }
    120  
     146
     147                ShowStatusBarMessage("Using IC = " + Kp.ToString() + " for analysis...", NotificationLevel.Info);
     148
     149
     150                if (arrayInput.Length < 2)
     151                {
     152                    // error, only one letter?
     153                    ShowStatusBarMessage("Error - cannot analyze an array of a single letter.", NotificationLevel.Error);
     154                    return;
     155                }
     156
    121157                                for (int i = 0; i < arrayInput.Length; i++)
    122158                                {
    123159                                        cipherTextLength += arrayInput[i];
    124160                                        countDoubleCharacters += (arrayInput[i] * (arrayInput[i] - 1));
     161
     162                    // show some progress
     163                    ShowProgress(i, (arrayInput.Length + 1));
    125164                                }
    126165
    127                                 double kappaCiphertext = ((double)countDoubleCharacters / (double)(cipherTextLength * (cipherTextLength - 1)));
    128                                 double keyLen = 0.0377 * cipherTextLength / (((cipherTextLength - 1) * kappaCiphertext) - (0.0385 * cipherTextLength) + Kp);
    129                
    130                                 stringOutput = Convert.ToString(keyLen);
    131                 keyLength = keyLen;
    132                 OnPropertyChanged("OutputString");
     166                ShowStatusBarMessage(String.Format("Input analyzed: Got {0} different letters in a text of total length {1}.",arrayInput.Length,cipherTextLength), NotificationLevel.Info);
     167
     168                                kappaCiphertext = ((double)countDoubleCharacters / (double)(cipherTextLength * (cipherTextLength - 1)));
     169                keyLength = 0.0377 * cipherTextLength / (((cipherTextLength - 1) * kappaCiphertext) - (0.0385 * cipherTextLength) + Kp);
     170
     171                if (Math.Abs(Kp - kappaCiphertext) > 0.01)
     172                {
     173                    ciphermode = "polyalphabetic";
     174                }
     175
     176                stringOutput = String.Format("KeyLen = {0}\nIC_analyzed = {1}\nIC_provided = {2}\nMode = {3}", keyLength.ToString("0.00000"), kappaCiphertext.ToString("0.00000"), Kp,ciphermode);
     177
     178           
     179                OnPropertyChanged("StringOutput");
    133180                OnPropertyChanged("KeyLength");
    134                 if (OnPluginProgressChanged != null)
    135                 {
    136                                         OnPluginProgressChanged(this, new PluginProgressEventArgs(cipherTextLength, cipherTextLength));
    137                 }
     181                OnPropertyChanged("KappaCiphertext");
     182                // final step of progress
     183                ShowProgress(100, 100);
    138184            }
    139185
  • trunk/CrypPlugins/FriedmanTest/FriedmanTestSettings.cs

    r98 r300  
    1515        private int kappa = 0; //0="English", 1="German", 2="French", 3="Spanish", 4="Italian",5="Portugeese"
    1616        #region ISettings Members
     17
    1718        [ContextMenu("Kappa plain-text", "Select the language to be analysed", 2, DisplayLevel.Beginner, ContextMenuControlType.ComboBox, null, new String[] { "English", "German", "French", "Spanish", "Italian", "Portugeese" })]
    1819        [TaskPane("Kappa plain-text", "Select the language to be analysed", null, 2, false, DisplayLevel.Experienced, ControlType.ComboBox, new String[] { "English", "German", "French", "Spanish", "Italian", "Portugeese" })]
Note: See TracChangeset for help on using the changeset viewer.