Exemple #1
0
        public static CrackResult[] Attack(string ciphertext, Cipher[] ciphers, Storage.Languages language)
        {
            var crackResults = GetCrackResults(ciphertext, ciphers, language);

            LangCharacteristic langChar = Storage.GetLangChar(language);
            var results = crackResults.OrderBy(x => Analyse.SimilarityIndex(x.opentext, langChar)).ToArray();

            return results;
        }
Exemple #2
0
        /// <summary>
        /// Metoda vyzkouší dešifrovat několik textů a po skončení zavolá 
        /// předanou metodu runComplete s argumentem počet nalezených neshod.
        /// Metoda funguje paralelně, testovací kód je spouštěn v tolika
        /// vláknech, kolik má počítač jader. 
        /// </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="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>
        public static void Test(Cipher cipher, int crackMehod, int count, RunComplete runComplete)
        {
            threads = coresInComputer;
            Thread thread;

            errors = 0;

            coresInComputer.Times(i =>
                {
                    thread = new Thread(() => DoTest(cipher, crackMehod, count, i, runComplete));
                    thread.IsBackground = true;
                    thread.Priority = 0;
                    thread.Start();
                });
        }
        public void Attack(string ciphertext, Cipher[] ciphers, Storage.Languages language)
        {
            GetCrackResults(ciphertext, ciphers, language, crackResults =>
                {

                    LangCharacteristic langChar = Storage.GetLangChar(language);
                    var results = crackResults.Where(x => x != null)
                        .OrderBy(x => Analyse.SimilarityIndex(x.opentext, langChar))
                        .ToArray();

                    results = FixVigenere(results);

                    finish(results.First());
                });
        }
Exemple #4
0
        private static List<CrackResult> GetCrackResults(string ciphertext, Cipher[] ciphers, Storage.Languages language)
        {
            List<CrackResult> results = new List<CrackResult>();

            foreach (var cipher in ciphers)
            {
                try
                {
                    var keys = cipher.Crack(ciphertext, 0, language);
                    if (keys.Count > 0)
                    {
                        var result = new CrackResult();
                        result.key = keys[0];
                        result.opentext = cipher.Decrypt(ciphertext, result.key);
                        result.cipher = cipher;
                        results.Add(result);
                    }
                }
                catch (Exceptions.MatchNotFound)
                { }
            }

            return results;
        }
Exemple #5
0
        private MainForm Decrypt(string ciphertext, string key, Cipher decryptCipher)
        {
            ciphertext = Analyse.NormalizeText(ciphertext, Analyse.TextTypes.WithoutSpacesLower);

            try
            {
                OutputText = decryptCipher.Decrypt(ciphertext, key);
            }
            catch (CryptanalysisCore.Exceptions.CryptanalysisException ex)
            {
                MessageBox.Show(ex.Message, "Dešifrování se nezdařilo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }

            return this;
        }
Exemple #6
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);
                }
            }
        }
        private void GetCrackResults(string ciphertext, Cipher[] ciphers, Storage.Languages language, Action<CrackResult[]> afterCrack)
        {
            int threadsCount = ciphers.Length;
            CrackResult[] results = new CrackResult[threadsCount];
            bool[] syncArray = new bool[threadsCount].Fill(false);
            object progressLockObject = new object();
            object endTestLockObject = new object();

            threadsCount.Times(i =>
                {
                    Thread thread = new Thread(() =>
                        {
                            try
                            {
                                var currentCipher = ciphers[i];
                                var keys = currentCipher.Crack(ciphertext, 0, language);
                                if (keys.Count > 0)
                                {
                                    var crackResult = new CrackResult();
                                    crackResult.key = keys[0];
                                    crackResult.opentext = currentCipher.Decrypt(ciphertext, crackResult.key);
                                    crackResult.cipher = currentCipher;
                                    results[i] = crackResult;
                                }
                                else
                                {
                                    throw new Exceptions.MatchNotFound();
                                }

                                lock (progressLockObject)
                                {
                                    progress();
                                }
                            }
                            catch (Exceptions.MatchNotFound)
                            {
                                lock (progressLockObject)
                                {
                                    progress();
                                }
                            }

                            lock (endTestLockObject)
                            {
                                syncArray[i] = true;
                                if (syncArray.All(x => x))
                                {
                                    afterCrack(results);
                                }
                            }
                        });

                    thread.Priority = ThreadPriority.Lowest;
                    thread.IsBackground = true;
                    addThread(thread);
                    thread.Start();
                });
        }
Exemple #8
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();
                    }
                }
            }
        }
Exemple #9
0
 /// <summary>
 /// Metoda vyzkouší dešifrovat několik textů a po skončení zavolá 
 /// předanou metodu runComplete s argumentem počet nalezených neshod.
 /// Metoda funguje paralelně, testovací kód je spouštěn v tolika
 /// vláknech, kolik má počítač jader. 
 /// </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="runComplete">Jaká akce má být provedena po dokončení testu.</param>
 public static void Test(Cipher cipher, int crackMehod, int count, RunComplete runComplete)
 {
     Test(cipher, crackMehod, count, runComplete, null);
 }
Exemple #10
0
        /// <summary>
        /// Metoda vyzkouší dešifrovat několik textů a po skončení zavolá 
        /// předanou metodu runComplete s argumentem počet nalezených neshod.
        /// Metoda funguje paralelně, testovací kód je spouštěn v tolika
        /// vláknech, kolik má počítač jader. 
        /// </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="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>
        public static void Test(Cipher cipher, int crackMehod, int count, RunComplete runComplete, System.Windows.Forms.ProgressBar progressBar)
        {
            threads = coresInComputer;
            Thread thread;

            if(progressBar != null)
                progressBar.Maximum = count;

            errors = 0;

            coresInComputer.Times(i =>
                {
                    thread = new Thread(() => DoTest(cipher, crackMehod, count, i, runComplete, progressBar));
                    thread.IsBackground = true;
                    thread.Priority = 0;
                    thread.Start();
                });
        }