コード例 #1
0
ファイル: Program.cs プロジェクト: pikl91337/Cryptography
        /// <summary>
        /// ПУНКТ 2. Сериальный тест
        /// Task 2. frequency test
        /// </summary>
        /// <param name="binaryKey"></param>
        /// <returns>Returns either empty list or key</returns>
        private static List <short> FrequencyTest(List <short> binaryText, int k, string testedName)
        {
            var freqTest = new FrequencyTester(k, binaryText.Count);

            // testing sequence and getting khi value
            var khi = freqTest.Test(binaryText);

            var teorFreq         = freqTest._TeorFrequency;
            var practFrequencies = freqTest._PossibleBlocks;

            // represent to user results of executed test
            var isKeyOkay = ConsoleMessager.CheckKhiValue(khi, k, teorFreq, practFrequencies, testedName);

            // TRYING TO GET VALID KEY
            if (testedName == "ключа")
            {
                if (!isKeyOkay)
                {
                    // generating a good key
                    var newKey = ReGenerateKey(isKeyOkay, binaryText.Count, k, testedName);
                    return(newKey);
                }
                else
                {
                    return(binaryText);
                }
            }

            return(new List <short>());
        }
コード例 #2
0
ファイル: Program.cs プロジェクト: pikl91337/Cryptography
        /// <summary>
        /// Regeneration a key if it has not passed frequency test
        /// </summary>
        /// <param name="isKeyOkay"></param>
        /// <param name="binaryTextLength"></param>
        /// <param name="k"></param>
        /// <param name="testedName"></param>
        /// <returns></returns>
        private static List <short> ReGenerateKey(bool isKeyOkay, int binaryTextLength, int k, string testedName)
        {
            var newKey = new List <short>();

            while (!isKeyOkay)
            {
                Console.WriteLine("Regenerating key because it hasn't passed frequency test.");
                newKey = GenerateKey(binaryTextLength);
                var freqTest = new FrequencyTester(k, binaryTextLength);
                var khi      = freqTest.Test(newKey);

                var teorFreq         = freqTest._TeorFrequency;
                var practFrequencies = freqTest._PossibleBlocks;

                // represent to user results of executed test
                isKeyOkay = ConsoleMessager.CheckKhiValue(khi, k, teorFreq, practFrequencies, testedName);
            }

            return(newKey);
        }