コード例 #1
0
        private static void RunTests(int numberOfTests, int textLength, int keyLength, int lgramParameter,
                                     double eps, string fileName)
        {
            for (int i = 1; i <= numberOfTests; i++)
            {
                startPosition = RandomNumber(textForTestsGenerating.Length - textLength - 1);
                randomText    = textForTestsGenerating.Substring(startPosition, textLength);

                startPosition = RandomNumber(keyForTestGenerating.Length - keyLength);
                randomKey     = keyForTestGenerating.Substring(startPosition, keyLength);

                FileHandler.WriteText("      Text:" + randomText, fileName, true);
                FileHandler.WriteText("      KeyWord:" + randomKey, fileName, true);

                encodedText = VigenereMethod.Encode(randomText, randomKey);

                guessedKeyWordLength = KasiskiMethod.GuessKeyWordLength(encodedText, lgramParameter);

                if (guessedKeyWordLength == keyLength)
                {
                    successGuess++;
                    guessedKeyWord = KasiskiMethod.GuessKey(encodedText, guessedKeyWordLength);

                    match = Match(randomKey, guessedKeyWord);
                    if (match >= eps)
                    {
                        successGuessEps++;
                    }
                    if (match == 1.0)
                    {
                        successGuessWhole++;
                    }

                    FileHandler.WriteText("      Guessed key word: " + guessedKeyWord, fileName, true);
                }
                else
                {
                    FileHandler.WriteText("      Guessed key word length: " + guessedKeyWordLength, fileName, true);;
                }

                FileHandler.WriteText("******************************************************", fileName, true);
            }

            FileHandler.WriteText(" Summary: TextLength: " + textLength
                                  + " Guessed key word length: " + successGuess / numberOfTests
                                  + " Guessed key word (" + eps * 100 + "% match): " + successGuessEps / numberOfTests
                                  + " Guessed key word (100% match): " + successGuessWhole / numberOfTests, fileName, true);

            FileHandler.WriteText("----------------------------------------------------------", fileName, true);
        }
コード例 #2
0
        static void Main(string[] args)
        {
            string text;
            string encodedText;
            int    position       = 3;
            int    lgramParameter = 4;
            int    numberOfTests  = 30;
            double eps            = 0.2;
            int    textLength     = 1000;
            int    keyLength      = 8;

            string fileInput = "Text.txt";
            string fileOutputCaesarEncode   = "CaesarEncode.txt";
            string fileOutputCaesarDecode   = "CaesarDecode.txt";
            string fileOutputVigenereEncode = "VigenereEncode.txt";
            string fileOutputVigenereDecode = "VigenereDecode.txt";

            text = FileHandler.ReadText(fileInput);

            encodedText = CaesarMethod.Encode(text, position);
            FileHandler.WriteText(encodedText, fileOutputCaesarEncode, false);
            FileHandler.WriteText(CaesarMethod.Decode(encodedText, position), fileOutputCaesarDecode, false);

            string key = "supernatural".ToUpper();

            encodedText = VigenereMethod.Encode(text, key);
            FileHandler.WriteText(encodedText, fileOutputVigenereEncode, false);
            FileHandler.WriteText(VigenereMethod.Decode(encodedText, key), fileOutputVigenereDecode, false);

            int guessedKeyLength = KasiskiMethod.GuessKeyWordLength(encodedText, lgramParameter);

            FileHandler.WriteText("----------------------------------------------------------",
                                  fileOutputVigenereDecode, true);
            FileHandler.WriteText("Guessed key word length: " + guessedKeyLength, fileOutputVigenereDecode, true);

            TestsHandler.AttackAnalysisTextLength(numberOfTests, eps, keyLength, lgramParameter);
            TestsHandler.AttackAnalysisKeyLength(numberOfTests, eps, textLength, lgramParameter);

            Console.WriteLine("Press any key to continue...");
            Console.ReadKey();
        }