Пример #1
0
        private ValueKey createKey(byte[] key)
        {
            ValueKey result = new ValueKey();

            decrypt(result, (byte[])key.Clone());
            return(result);
        }
Пример #2
0
        private void addToBestList(int[] k)
        {
            ValueKey vk = new ValueKey();

            int[] first = (int[])k.Clone();

            do
            {
                bestlist.Add((int[])k.Clone());

                int[] keyPlusOne = new int[k.Length];
                for (int i = 0; i < k.Length; i++)
                {
                    keyPlusOne[i] = k[i] + 1;
                }

                byte[] key = intArrayToByteArray(keyPlusOne);

                decrypt(vk, key);

                if (TOPLIST.isBetter(vk))
                {
                    Output = vk.plaintext;
                }

                TOPLIST.Add(vk);

                k = shiftKey(k);
            } while (!arrayEquals(k, first));
        }
Пример #3
0
        public object Clone()
        {
            ValueKey v = new ValueKey();

            v.score     = score;
            v.key       = (byte[])key.Clone();
            v.plaintext = (byte[])plaintext.Clone();

            return(v);
        }
Пример #4
0
        public int CompareTo(object obj)
        {
            ValueKey v = (ValueKey)obj;

            int result;

            if ((result = score.CompareTo(v.score)) == 0)
            {
                if ((result = key.Length.CompareTo(v.key.Length)) == 0)
                {
                    for (int i = 0; i < key.Length; i++)
                    {
                        if ((result = key[i].CompareTo(v.key[i])) != 0)
                        {
                            break;
                        }
                    }
                }
            }

            return(result);
        }
Пример #5
0
        private void BruteforceAnalysis()
        {
            int[] set = getBruteforceSettings();

            if (set == null)
            {
                GuiLogMessage("Specify the type of transposition to examine.", NotificationLevel.Error);
                return;
            }

            if (settings.MaxLength < 2 || settings.MaxLength > 20)
            {
                GuiLogMessage("Check transposition bruteforce length. Min length is 2, max length is 20!", NotificationLevel.Error);
                return;
            }

            ValueKey vk = new ValueKey();

            //Just for fractional-calculation:
            PermutationGenerator per = new PermutationGenerator(2);

            DateTime startTime  = DateTime.Now;
            DateTime nextUpdate = DateTime.Now.AddMilliseconds(100);

            ulong totalKeys = 0;

            for (int i = 1; i <= settings.MaxLength; i++)
            {
                totalKeys += (ulong)per.getFactorial(i);
            }
            totalKeys *= (ulong)set.Length;

            ulong doneKeys = 0;

            stop = false;

            for (int keylength = 1; keylength <= settings.MaxLength; keylength++)
            {
                if (stop)
                {
                    break;
                }

                // for every selected bruteforce mode:
                for (int s = 0; s < set.Length; s++)
                {
                    if (stop)
                    {
                        break;
                    }

                    switch (set[s])
                    {
                    case (0):
                        controlMaster.changeSettings("ReadIn", ReadInMode.byColumn);
                        controlMaster.changeSettings("Permute", PermutationMode.byColumn);
                        controlMaster.changeSettings("ReadOut", ReadOutMode.byColumn);
                        break;

                    case (1):
                        controlMaster.changeSettings("ReadIn", ReadInMode.byColumn);
                        controlMaster.changeSettings("Permute", PermutationMode.byColumn);
                        controlMaster.changeSettings("ReadOut", ReadOutMode.byRow);
                        break;

                    case (2):
                        controlMaster.changeSettings("ReadIn", ReadInMode.byRow);
                        controlMaster.changeSettings("Permute", PermutationMode.byColumn);
                        controlMaster.changeSettings("ReadOut", ReadOutMode.byColumn);
                        break;

                    case (3):
                        controlMaster.changeSettings("ReadIn", ReadInMode.byRow);
                        controlMaster.changeSettings("Permute", PermutationMode.byColumn);
                        controlMaster.changeSettings("ReadOut", ReadOutMode.byRow);
                        break;
                    }

                    byte[] key = new byte[keylength];

                    per = new PermutationGenerator(keylength);

                    while (per.hasMore() && !stop)
                    {
                        int[] keyInt = per.getNext();

                        for (int i = 0; i < key.Length; i++)
                        {
                            key[i] = Convert.ToByte(keyInt[i]);
                        }

                        decrypt(vk, key);

                        if (TOPLIST.isBetter(vk))
                        {
                            Output = vk.plaintext;
                        }

                        TOPLIST.Add(vk);
                        doneKeys++;

                        if (DateTime.Now >= nextUpdate)
                        {
                            UpdatePresentationList(totalKeys, doneKeys, startTime);
                            nextUpdate = DateTime.Now.AddMilliseconds(1000);
                        }
                    }
                }
            }

            UpdatePresentationList(totalKeys, doneKeys, startTime);
        }
Пример #6
0
        private void showProgress(DateTime startTime, ulong totalKeys, ulong doneKeys)
        {
            if (!Presentation.IsVisible || stop)
            {
                return;
            }

            long ticksPerSecond = 10000000;

            TimeSpan elapsedtime  = DateTime.Now.Subtract(startTime);
            double   totalSeconds = elapsedtime.TotalSeconds;

            if (totalSeconds == 0)
            {
                totalSeconds = 0.001;
            }
            elapsedtime = new TimeSpan(elapsedtime.Ticks - (elapsedtime.Ticks % ticksPerSecond));   // truncate to seconds

            TimeSpan timeleft = new TimeSpan();
            DateTime endTime  = new DateTime();
            double   secstodo;

            double keysPerSec = doneKeys / totalSeconds;

            if (keysPerSec > 0)
            {
                if (totalKeys < doneKeys)
                {
                    totalKeys = doneKeys;
                }
                secstodo = (totalKeys - doneKeys) / keysPerSec;
                timeleft = new TimeSpan((long)secstodo * ticksPerSecond);
                endTime  = DateTime.Now.AddSeconds(secstodo);
                endTime  = new DateTime(endTime.Ticks - (endTime.Ticks % ticksPerSecond));  // truncate to seconds
            }

            ((TranspositionAnalyserQuickWatchPresentation)Presentation).Dispatcher.BeginInvoke(DispatcherPriority.Normal, (SendOrPostCallback) delegate
            {
                var culture = System.Threading.Thread.CurrentThread.CurrentUICulture;

                myPresentation.startTime.Content     = "" + startTime;
                myPresentation.keysPerSecond.Content = String.Format(culture, "{0:##,#}", (ulong)keysPerSec);

                if (keysPerSec > 0)
                {
                    myPresentation.timeLeft.Content    = "" + timeleft;
                    myPresentation.elapsedTime.Content = "" + elapsedtime;
                    myPresentation.endTime.Content     = "" + endTime;
                }
                else
                {
                    myPresentation.timeLeft.Content = "incalculable";
                    myPresentation.endTime.Content  = "in a galaxy far, far away...";
                }

                myPresentation.entries.Clear();

                for (int i = 0; i < TOPLIST.Count; i++)
                {
                    ValueKey v        = TOPLIST[i];
                    ResultEntry entry = new ResultEntry();

                    entry.Ranking  = (i + 1).ToString();
                    entry.Value    = String.Format("{0:0.00000}", v.score);
                    entry.KeyArray = v.key;
                    entry.Key      = "[" + String.Join(",", v.key) + "]";
                    entry.Text     = Encoding.GetEncoding(1252).GetString(v.plaintext);

                    myPresentation.entries.Add(entry);
                }
            }
                                                                                               , null);
        }
Пример #7
0
 private void decrypt(ValueKey vk, byte[] key)
 {
     vk.key       = key;
     vk.plaintext = this.controlMaster.Decrypt(this.input, vk.key);
     vk.score     = this.costMaster.CalculateCost(vk.plaintext);
 }
Пример #8
0
        private void HillClimbingAnalysis()
        {
            if (settings.Iterations < 2 || settings.KeySize < 2)
            {
                GuiLogMessage("Check keylength and iterations", NotificationLevel.Error);
                return;
            }

            DateTime startTime  = DateTime.Now;
            DateTime nextUpdate = DateTime.Now.AddMilliseconds(100);

            HighscoreList ROUNDLIST = new HighscoreList(comparer, 10);

            ValueKey vk = new ValueKey();

            ulong totalKeys = (ulong)settings.Repeatings * (ulong)settings.Iterations;
            ulong doneKeys  = 0;

            stop = false;

            for (int repeating = 0; repeating < settings.Repeatings; repeating++)
            {
                if (stop)
                {
                    break;
                }

                ROUNDLIST.Clear();

                byte[] key    = randomArray(settings.KeySize);
                byte[] oldkey = new byte[settings.KeySize];

                for (int iteration = 0; iteration < settings.Iterations; iteration++)
                {
                    if (stop)
                    {
                        break;
                    }

                    Array.Copy(key, oldkey, key.Length);

                    int r = rd.Next(100);
                    if (r < 50)
                    {
                        for (int i = 0; i < rd.Next(10); i++)
                        {
                            swap(key, rd.Next(key.Length), rd.Next(key.Length));
                        }
                    }
                    else if (r < 70)
                    {
                        for (int i = 0; i < rd.Next(3); i++)
                        {
                            int l = rd.Next(key.Length - 1) + 1;
                            int f = rd.Next(key.Length);
                            int t = (f + l + rd.Next(key.Length - l)) % key.Length;
                            blockswap(key, f, t, l);
                        }
                    }
                    else if (r < 90)
                    {
                        int l = 1 + rd.Next(key.Length - 1);
                        int f = rd.Next(key.Length);
                        int t = (f + 1 + rd.Next(key.Length - 1)) % key.Length;
                        blockshift(key, f, t, l);
                    }
                    else
                    {
                        pivot(key, rd.Next(key.Length - 1) + 1);
                    }

                    decrypt(vk, key);

                    if (ROUNDLIST.Add(vk))
                    {
                        if (TOPLIST.isBetter(vk))
                        {
                            TOPLIST.Add(vk);
                            Output = vk.plaintext;
                        }
                    }
                    else
                    {
                        Array.Copy(oldkey, key, key.Length);
                    }

                    doneKeys++;

                    if (DateTime.Now >= nextUpdate)
                    {
                        TOPLIST.Merge(ROUNDLIST);
                        UpdatePresentationList(totalKeys, doneKeys, startTime);
                        nextUpdate = DateTime.Now.AddMilliseconds(1000);
                    }
                }
            }

            TOPLIST.Merge(ROUNDLIST);
            UpdatePresentationList(totalKeys, doneKeys, startTime);
        }
Пример #9
0
        private void GeneticAnalysis()
        {
            if (settings.Iterations < 2 || settings.KeySize < 2 || settings.Repeatings < 1)
            {
                GuiLogMessage("Check keylength and iterations", NotificationLevel.Error);
                return;
            }

            ValueKey vk = new ValueKey();

            DateTime startTime  = DateTime.Now;
            DateTime nextUpdate = DateTime.Now.AddMilliseconds(100);

            HighscoreList ROUNDLIST = new HighscoreList(comparer, 12);

            ulong totalKeys = (ulong)settings.Repeatings * (ulong)settings.Iterations * 6;
            ulong doneKeys  = 0;

            stop = false;

            for (int repeating = 0; repeating < settings.Repeatings; repeating++)
            {
                if (stop)
                {
                    break;
                }

                ROUNDLIST.Clear();

                for (int i = 0; i < ROUNDLIST.Capacity; i++)
                {
                    ROUNDLIST.Add(createKey(randomArray(settings.KeySize)));
                }

                for (int iteration = 0; iteration < settings.Iterations; iteration++)
                {
                    if (stop)
                    {
                        break;
                    }

                    // Kinder der besten Keys erstellen
                    int rndInt = 0;

                    for (int a = 0; a < 6; a++)
                    {
                        if (a % 2 == 0)
                        {
                            rndInt = rd.Next(settings.KeySize - 1) + 1;
                        }

                        // combine DNA of two parents
                        ValueKey parent1 = ROUNDLIST[a];
                        ValueKey parent2 = ROUNDLIST[(a % 2 == 0) ? a + 1 : a - 1];

                        byte[] child = new byte[parent1.key.Length];
                        Array.Copy(parent1.key, child, rndInt);

                        int pos = rndInt;
                        for (int b = 0; b < parent2.key.Length; b++)
                        {
                            for (int c = rndInt; c < parent1.key.Length; c++)
                            {
                                if (parent1.key[c] == parent2.key[b])
                                {
                                    child[pos] = parent1.key[c];
                                    pos++;
                                    break;
                                }
                            }
                        }

                        // add a single mutation
                        int apos = rd.Next(settings.KeySize);
                        int bpos = (apos + rd.Next(1, settings.KeySize)) % settings.KeySize;
                        swap(child, apos, bpos);

                        decrypt(vk, child);

                        ROUNDLIST.Add(vk);

                        if (TOPLIST.isBetter(vk))
                        {
                            TOPLIST.Add(vk);
                            Output = vk.plaintext;
                        }

                        doneKeys++;
                    }

                    if (DateTime.Now >= nextUpdate)
                    {
                        TOPLIST.Merge(ROUNDLIST);
                        UpdatePresentationList(totalKeys, doneKeys, startTime);
                        nextUpdate = DateTime.Now.AddMilliseconds(1000);
                    }
                }
            }

            TOPLIST.Merge(ROUNDLIST);
            UpdatePresentationList(totalKeys, doneKeys, startTime);
        }