Exemplo n.º 1
0
        /// <summary>
        /// Provádí samotný test dešifrování. 
        /// </summary>
        /// <param name="cipher">Šifra, nad kterou budeme Unit test provádět</param>
        /// <param name="crackMehod">Identifikátor útočící metody.</param>
        /// <param name="count">Počet textů, které chceme zkoušet.</param>
        /// <param name="start">Index prvního testovaného řetězce</param>
        /// <param name="runComplete">Jaká akce má být provedena po dokončení testu.</param>
        /// <param name="progressBar">Do kterého progressBaru chceme zobrazovat průběh</param>
        private static void DoTest(Cipher cipher, int crackMehod, int count, int start, RunComplete runComplete)
        {
            int errorsCounter = 0;

            string[] testStrings = File.ReadAllText(Storage.StatsFolderPath + Storage.TextsFile + ".czech.txt")
                .ToLower()
                .Split('\n')
                .Select(x => x.Filter(y => TextAnalysis.IsEnglishLetter(y)))
                .Where(x => x.Length >= 500)
                .Select(x => x.ToCharArray().Take(500).ToList().Implode(""))
                .Where(x => x.Length > 0)
                .Take(count)
                .ToArray();

            //File.WriteAllText("delky.txt", testStrings.ToList().Select(x => x.Length).ToList().Implode("\n"));

            List<string> keys;
            string opentext = string.Empty;
            string ciphertext;
            for (int i = start; i < testStrings.Length; i += coresInComputer)
            {
                ciphertext = cipher.Encrypt(testStrings[i], cipher.RandomKey());

                try
                {
                    keys = cipher.Crack(ciphertext, crackMehod, Storage.Languages.czech);
                    opentext = cipher.Decrypt(ciphertext, keys[0]);
                }
                catch (CryptanalysisCore.Exceptions.MatchNotFound)
                {
                    errorsCounter++;
                }

                if (opentext != testStrings[i])
                    errorsCounter++;
            }

            /*
             * Atomicky přičteme počet zjištěných neshod, snížíme počet běžících vláken
             * o jedničku a následně zjistíme, jestli je toto vlákno poslední běžící vlákno.
             */
            lock (incrementResultsLock)
            {
                errors += errorsCounter;
                threads--;

                if (threads == 0)
                {
                    runComplete(errors, count);
                }
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Provádí samotný test dešifrování. 
        /// </summary>
        /// <param name="cipher">Šifra, nad kterou budeme Unit test provádět</param>
        /// <param name="crackMehod">Identifikátor útočící metody.</param>
        /// <param name="count">Počet textů, které chceme zkoušet.</param>
        /// <param name="start">Index prvního testovaného řetězce</param>
        /// <param name="runComplete">Jaká akce má být provedena po dokončení testu.</param>
        /// <param name="progressBar">Do kterého progressBaru chceme zobrazovat průběh</param>
        private static void DoTest(Cipher cipher, int crackMehod, int count, int start, RunComplete runComplete, System.Windows.Forms.ProgressBar progressBar)
        {
            int errorsCounter = 0;

            string[] testStrings = texts.Where(x => x.Length >= 500)
                .Select(x => x.ToCharArray().Take(500).ToList().Implode(""))
                .Where(x => x.Length > 0)
                .Take(count)
                .ToArray();

            List<string> keys;
            string opentext = string.Empty;
            string ciphertext;
            for (int i = start; i < testStrings.Length; i += coresInComputer)
            {
                ciphertext = cipher.Encrypt(testStrings[i], cipher.RandomKey());

                try
                {
                    keys = cipher.Crack(ciphertext, crackMehod, Storage.Languages.czech);
                    opentext = cipher.Decrypt(ciphertext, keys[0]);
                }
                catch (CryptanalysisCore.Exceptions.MatchNotFound)
                {
                    errorsCounter++;
                }

                if (opentext != testStrings[i])
                    errorsCounter++;

                if (progressBar != null)
                {
                    Action incrementProgressBar = () => progressBar.Value++;
                    if (progressBar.InvokeRequired)
                        progressBar.Invoke(incrementProgressBar);
                    else
                        incrementProgressBar();
                }
            }

            /*
             * Atomicky přičteme počet zjištěných neshod, snížíme počet běžících vláken
             * o jedničku a následně zjistíme, jestli je toto vlákno poslední běžící vlákno.
             */
            lock (incrementResultsLock)
            {
                errors += errorsCounter;
                threads--;

                if (threads == 0)
                {
                    runComplete(errors, count);

                    if (progressBar != null)
                    {
                        Action zeroProgressBar = () => progressBar.Value = 0;
                        if (progressBar.InvokeRequired)
                            progressBar.Invoke(zeroProgressBar);
                        else
                            zeroProgressBar();
                    }
                }
            }
        }