Ejemplo n.º 1
0
        /// <summary>
        /// Provede útok testováním všech dostupných klíčů 
        /// a následně vybere nejpravděpodobnější variantu.
        /// </summary>
        /// <param name="packet"></param>
        /// <returns></returns>
        public List<string> BruteForceAttack(string ciphertext, Storage.Languages language)
        {
            Dictionary<char, double> probability = new Dictionary<char, double>();
            Caesar caesar = new Caesar();
            string opentext;

            Analyse.DoAlphabet(letter =>
            {
                opentext = caesar.Decrypt(ciphertext, letter.ToString());
                probability[letter] = Analyse.SimilarityIndex(Analyse.NormalizeText(opentext, Analyse.TextTypes.WithoutSpacesLower), Storage.GetLangChar(language));
            });

            var resultPacket = probability.OrderBy(x => x.Value).ToArray()[0].Key;

            return new List<string>() { resultPacket.ToString() };
        }
Ejemplo n.º 2
0
 public CaesarTest(Action progress, Action<string> afterFinish, Texts texts)
     : base(progress, afterFinish, texts)
 {
     cipher = new Caesar();
 }
Ejemplo n.º 3
0
        private List<string> BruteForceAttack(string ciphertext, Storage.Languages language, int[] keysLength)
        {
            ciphertext = Analyse.NormalizeText(ciphertext, Analyse.TextTypes.WithoutSpacesLower);
            Caesar caesar = new Caesar();
            Dictionary<int, string> crackedKeys = new Dictionary<int, string>();

            foreach(int keyLength in keysLength)
            {
                char[][] splitedChars = new char[keyLength][];

                for (int i = 0; i < keyLength; i++)
                    splitedChars[i] = GetNthChars(ciphertext, keyLength, i);

                char[] crackKeys = new char[keyLength];

                for (int i = 0; i < keyLength; i++)
                {
                    try
                    {
                        crackKeys[i] = caesar.Crack(Analyse.NormalizeText(new string(splitedChars[i]), Analyse.TextTypes.WithoutSpacesUpper), Caesar.TriangleID, language).First()[0];
                    }
                    catch (Exceptions.MatchNotFound)
                    {
                        crackKeys[i] = '?';
                    }
                }

                crackedKeys[keyLength] = new string(crackKeys);
            }

            var possKeys = crackedKeys.Where(x => x.Value.Length > 0).Select(x => x.Value).ToArray();

            Dictionary<string, double> simIndexes = new Dictionary<string, double>();

            for (int i = 0; i < possKeys.Length; i++)
            {
                string openttext = Decrypt(ciphertext, possKeys[i]);
                double simIndex = Analyse.SimilarityIndex(openttext, Storage.GetLangChar(language));
                simIndexes[possKeys[i]] = simIndex;
            }

            var result = simIndexes.OrderBy(x => x.Value).Select(x => x.Key).ToList();

            return result;
        }