Exemplo n.º 1
0
        public void Test_Encipher()
        {
            sut = new SubstitutionCipher(testMapping);

            Assert.AreEqual('O', sut.Encipher('A'));
            Assert.AreEqual('R', sut.Encipher('B'));
            Assert.AreEqual('M', sut.Encipher('C'));
            Assert.AreEqual('S', sut.Encipher('D'));
            Assert.AreEqual('F', sut.Encipher('E'));
            Assert.AreEqual('Z', sut.Encipher('F'));
            Assert.AreEqual('B', sut.Encipher('G'));
            Assert.AreEqual('P', sut.Encipher('H'));
            Assert.AreEqual('U', sut.Encipher('I'));
            Assert.AreEqual('E', sut.Encipher('J'));
            Assert.AreEqual('K', sut.Encipher('K'));
            Assert.AreEqual('V', sut.Encipher('L'));
            Assert.AreEqual('Y', sut.Encipher('M'));
            Assert.AreEqual('A', sut.Encipher('N'));
            Assert.AreEqual('L', sut.Encipher('O'));
            Assert.AreEqual('Q', sut.Encipher('P'));
            Assert.AreEqual('T', sut.Encipher('Q'));
            Assert.AreEqual('J', sut.Encipher('R'));
            Assert.AreEqual('I', sut.Encipher('S'));
            Assert.AreEqual('W', sut.Encipher('T'));
            Assert.AreEqual('D', sut.Encipher('U'));
            Assert.AreEqual('N', sut.Encipher('V'));
            Assert.AreEqual('X', sut.Encipher('W'));
            Assert.AreEqual('H', sut.Encipher('X'));
            Assert.AreEqual('C', sut.Encipher('Y'));
            Assert.AreEqual('G', sut.Encipher('Z'));
        }
Exemplo n.º 2
0
        /// <summary>
        /// Substitution.
        /// </summary>
        /// <param name="flag">Encode/decode.</param>
        /// <returns>Result.</returns>
        private byte[] SubstitutionProcess(bool flag)
        {
            var cipher = new SubstitutionCipher(sBlock);

            GetSubKeys();
            if (!flag)
            {
                subKeys.Reverse();
            }
            byte[] res   = new byte[message.Length];
            int    index = 0;

            foreach (var chunk in ReadByChunk())
            {
                if (flag)
                {
                    Array.Copy(cipher.EncodeProcess(chunk, subKeys), 0, res, index, 8);
                }
                else
                {
                    Array.Copy(cipher.DecodeProcess(chunk, subKeys), 0, res, index, 8);
                }
                index += 8;
            }
            return(res);
        }
Exemplo n.º 3
0
        public void CipherTest_ReadDictionary()
        {
            SubstitutionCipher cipher   = new SubstitutionCipher();
            string             fullPath = Path.Combine(TestContext.CurrentContext.TestDirectory, @"testData\dictionary.txt");
            bool read = cipher.ReadDictionary(fullPath);

            Assert.That(read, Is.True);
        }
Exemplo n.º 4
0
        public void CipherTest_AllWordsFalse()
        {
            SubstitutionCipher cipher   = new SubstitutionCipher();
            string             fullPath = Path.Combine(TestContext.CurrentContext.TestDirectory, @"testData\dictionary.txt");

            cipher.ReadDictionary(fullPath);
            Assert.That(cipher.AllWords("not all wurdz"), Is.False);
        }
Exemplo n.º 5
0
        private void InitializeRotor()
        {
            var dialSettingInt = EnigmaHelper.GetInt(dialSetting);

            shiftCipher1       = new ShiftCipher(dialSettingInt - pinSetting);
            shiftCipher2       = new ShiftCipher(dialSettingInt);
            substitutionCipher = new SubstitutionCipher(this.rotorMapping);
        }
Exemplo n.º 6
0
 public void OneSubstitutionTest()
 {
     // одна подстановка
     this.cryptogram         = "I study at DNU";
     this.substitutionCipher = new SubstitutionCipher();
     this.encryptText        = this.substitutionCipher.Encrypt(this.cryptogram);
     this.decryptText        = this.substitutionCipher.Decrypt(this.encryptText);
     Assert.AreEqual(this.cryptogram, this.decryptText);
 }
Exemplo n.º 7
0
        public void CipherTest_SolvedFalse1()
        {
            SubstitutionCipher cipher   = new SubstitutionCipher();
            string             fullPath = Path.Combine(TestContext.CurrentContext.TestDirectory, @"testData\dictionary.txt");
            bool read = cipher.ReadDictionary(fullPath);

            StringBuilder[] build = { new StringBuilder("this"), new StringBuilder("is"), new StringBuilder("a"), new StringBuilder("t?st") };
            Assert.That(cipher.Solved(build), Is.False);
        }
Exemplo n.º 8
0
        public void MoreSubstitutionTest()
        {
            this.cryptogram = "I study at DNU";
            int n = 10;

            this.substitutionCipher = new SubstitutionCipher(n);
            this.encryptText        = this.substitutionCipher.Encrypt(this.cryptogram);
            this.decryptText        = this.substitutionCipher.Decrypt(this.encryptText);
            Assert.AreEqual(this.cryptogram, this.decryptText);
        }
Exemplo n.º 9
0
        public void CipherTest_DecryptResult()
        {
            SubstitutionCipher cipher   = new SubstitutionCipher();
            string             fullPath = Path.Combine(TestContext.CurrentContext.TestDirectory, @"testData\dictionary.txt");
            bool read = cipher.ReadDictionary(fullPath);

            bool   solved;
            string result = cipher.Decrypt("pjihzkl dipd hkpi dt hriho zm dri jbtvbka zp klgkep gtbozuv htbbihdle mtb ikhr hkpi", out solved).Trim();

            Assert.That(result.Equals("special test case to check if the program is always working correctly for each case"), Is.True);
        }
Exemplo n.º 10
0
        public void CipherTest_DecryptTrue()
        {
            SubstitutionCipher cipher   = new SubstitutionCipher();
            string             fullPath = Path.Combine(TestContext.CurrentContext.TestDirectory, @"testData\dictionary.txt");
            bool read = cipher.ReadDictionary(fullPath);

            bool solved;

            cipher.Decrypt("this is a test", out solved);
            Assert.That(solved, Is.True);
        }
Exemplo n.º 11
0
        public static string SubstitutionCipher(string text, int n, CryptType cryptType)
        {
            string result = string.Empty;
            substitutionCipher = new SubstitutionCipher(n);
            switch (cryptType)
            {
                case CryptType.Encrypt:
                    result = substitutionCipher.Encrypt(text);
                    break;
                case CryptType.Decrypt:
                    result = substitutionCipher.Decrypt(text);
                    break;
            }

            return result;
        }
Exemplo n.º 12
0
        public void CipherTest_EncryptTest()
        {
            SubstitutionCipher cipher   = new SubstitutionCipher();
            string             fullPath = Path.Combine(TestContext.CurrentContext.TestDirectory, @"testData\dictionary.txt");
            bool   read   = cipher.ReadDictionary(fullPath);
            string result = cipher.Encrypt("abcdefghijklmnopqrstuvwxyz");

            bool[] used = new bool[26];
            foreach (char c in result)
            {
                used[c - 'a'] = true;
            }
            foreach (bool val in used)
            {
                Assert.That(val, Is.True);
            }
        }
Exemplo n.º 13
0
        /// <summary>
        /// Шифры простой и многократной подстановки
        /// </summary>
        public static void SubstitutionCipherTest()
        {
            Console.WriteLine("1. Шифры простой и многократной подстановки");

            // Шифр простой подстановки
            Console.WriteLine("Шифр простой подстановки");
            Console.WriteLine("Пример работы программы");
            cryptogram = "I study at DNU";
            Console.WriteLine("Текст: " + cryptogram);
            substitutionCipher = new SubstitutionCipher();
            encryptText        = substitutionCipher.Encrypt(cryptogram);
            Console.WriteLine("Зашифрованый текст: " + encryptText);
            decryptText = substitutionCipher.Decrypt(encryptText);
            Console.WriteLine("Расшифрованый текст: " + decryptText);

            // Упражнение. Используя перестановку примера, расшифруйте криптограмму:
            Console.WriteLine("Упражнение. Используя перестановку примера, расшифруйте криптограмму: ");
            cryptogram = "MEUSCXZOCXVQM";
            Console.WriteLine(cryptogram);
            Console.WriteLine(substitutionCipher.Decrypt(cryptogram));
            Console.WriteLine();

            // Шифр многократной подстановки
            Console.WriteLine("Шифр многократной подстановки");
            Console.WriteLine("Пример работы программы");
            cryptogram = "INPUT";
            Console.WriteLine("Текст: " + cryptogram);
            int n = 3;

            substitutionCipher = new SubstitutionCipher(n);
            encryptText        = substitutionCipher.Encrypt(cryptogram);
            Console.WriteLine($"Зашифрованый текст при n = {n}: {encryptText}");
            decryptText = substitutionCipher.Decrypt(encryptText);
            Console.WriteLine($"Расшифрованый текст при n = {n}: {decryptText}");

            Console.WriteLine("\n");
        }
Exemplo n.º 14
0
        public void CipherTest_ContainsInvalidTrue()
        {
            SubstitutionCipher cipher = new SubstitutionCipher();

            Assert.That(cipher.ContainsInvalid("Not all valid!"), Is.True);
        }
Exemplo n.º 15
0
        public void CipherTest_ContainsInvalidFalse()
        {
            SubstitutionCipher cipher = new SubstitutionCipher();

            Assert.That(cipher.ContainsInvalid("these are all valid"), Is.False);
        }
Exemplo n.º 16
0
 public void Test_Constructor_Throws_Exception_When_Mapping_Is_Null()
 {
     char[] nullMapping = null;
     Assert.That(() => sut = new SubstitutionCipher(nullMapping), Throws.TypeOf <ArgumentNullException>());
 }
Exemplo n.º 17
0
 public void Test_Constructor_Throws_Exception_When_Mapping_Is_Too_Large()
 {
     char[] mappingOf27Letters = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'A' };
     Assert.That(() => sut = new SubstitutionCipher(mappingOf27Letters), Throws.TypeOf <ArgumentException>());
 }
Exemplo n.º 18
0
 public MacGenerator(ISBlock sBlock)
 {
     substitution = new SubstitutionCipher(sBlock);
 }