Ejemplo n.º 1
0
        static void Main(string[] args)
        {
            var startString =
                "A Cardan grille is made from a sheet of fairly rigid paper or parchment, or from thin metal. The paper is ruled to represent lines of handwriting and rectangular areas are cut out at arbitrary intervals between these lines.\r\n\r\nAn encipherer places the grille on a sheet of paper and writes his message in the rectangular apertures, some of which might allow a single letter, a syllable, or a whole word. Then, removing the grille, the fragments are filled out to create a note or letter that disguises the true message. Cardano suggested drafting the text three times in order to smooth any irregularities that might indicate the hidden words.";
            string text = File.ReadAllText(@"C:\Users\user\source\repos\Cryptology\Cryptology\Texts\text.txt");
            //var cypher = new CaeserCipher(text, 1);
            var cypher = new AffineCipher(text, 3, 4);

            Console.WriteLine("Encoded text:");
            var encryptedText = cypher.Encrypt();

            Console.WriteLine(encryptedText);
            File.WriteAllText(@"C:\Users\user\source\repos\Cryptology\Cryptology\Texts\textEncrypted.txt", encryptedText);


            Console.WriteLine("\nDecrypted text:");
            var decryptedText = cypher.Decrypt();

            Console.WriteLine(decryptedText);
            File.WriteAllText(@"C:\Users\user\source\repos\Cryptology\Cryptology\Texts\textDecrypted.txt", decryptedText);

            Console.WriteLine("\nDecoded text:");
            var decodedText = cypher.Decode();

            Console.WriteLine(decodedText);
            File.WriteAllText(@"C:\Users\user\source\repos\Cryptology\Cryptology\Texts\textDecoded.txt", decodedText);
            Console.ReadKey();
        }
Ejemplo n.º 2
0
        public void AffineEncryptValidInputWithSpace()
        {
            // Arrange
            AffineCipher cipher   = new AffineCipher();
            string       input    = "PUSZEK pusz$#ek";
            string       expected = "IFBCZL ifbczl";
            // Act
            string actual = cipher.Encrypt(input);

            // Assert
            Assert.AreEqual(expected, actual);
        }
Ejemplo n.º 3
0
        public void AffineEncryptValidInput()
        {
            // Arrange
            AffineCipher cipher   = new AffineCipher();
            string       input    = "PUSZEK#@$$%";
            string       expected = "IFBCZL";
            // Act
            string actual = cipher.Encrypt(input);

            // Assert
            Assert.AreEqual(expected, actual);
        }
Ejemplo n.º 4
0
        public void AffineCipherTest()
        {
            Random random = new Random();
            int    time   = 100;

            for (int i = 0; i < time; i++)
            {
                var _      = RandomHelper.GetCharList();
                int a      = RandomHelper.GetAffineKeyA();
                int b      = RandomHelper.GetAffineKeyB();
                var affine = new AffineCipher(a, b);
                Assert.AreEqual(_, affine.Decrypt(affine.Encrypt(_)));
            }
        }
Ejemplo n.º 5
0
        public IActionResult AffineEncrypt([FromBody] AffineCipherViewModel viewModel)
        {
            AffineCipher cipher = new AffineCipher(viewModel.KeyA, viewModel.KeyB)
            {
                Alphabet = Alphabets.GetAlphabet((Alphabets.AlphabetType)viewModel.AlphabetType)
            };

            string encrypted = "";

            try
            {
                encrypted = cipher.Encrypt(viewModel.Message);
            }
            catch (Exception)
            {
                return(BadRequest(new { Result = false, Message = Text.InvalidCharacter }));
            }

            return(Json(encrypted));
        }
Ejemplo n.º 6
0
        public string Encrypt(string plainText)
        {
            string cipherText = plainText;

            CaesarCipher caesarCipher = new CaesarCipher();

            caesarCipher.SetKey(CAESAR_CIPHER_KEY);
            cipherText = caesarCipher.Encrypt(cipherText);

            AffineCipher affineCipher = new AffineCipher();

            affineCipher.SetKeys(new int[] { AFFINE_CIPHER_KEY_A, AFFINE_CIPHER_KEY_B });
            cipherText = affineCipher.Encrypt(cipherText);

            SimpleSubstitutionCipher simpleSubstitutionCipher = new SimpleSubstitutionCipher();

            simpleSubstitutionCipher.SetKey(SIMPLE_SUBSTITUTION_CIPHER_KEY);
            cipherText = simpleSubstitutionCipher.Encrypt(cipherText);

            return(cipherText);
        }
Ejemplo n.º 7
0
        public IActionResult AffineVisualization([FromBody] AffineCipherViewModel viewModel)
        {
            AffineCipher cipher = new AffineCipher(viewModel.KeyA, viewModel.KeyB)
            {
                Alphabet = Alphabets.GetAlphabet((Alphabets.AlphabetType)viewModel.AlphabetType)
            };

            cipher.Alphabet = Alphabets.GetAlphabet((Alphabets.AlphabetType)viewModel.AlphabetType);

            string[] results = new string[3] {
                "alphabet", "output", "input"
            };
            string encrypted = "";
            string input     = viewModel.Message;

            input = StringHelper.ReplaceWhitespace(input, "");
            input = input.ToUpper();
            int alphabetLength = cipher.Alphabet.Length;

            input      = StringHelper.ReplaceWhitespace(input, "");
            input      = input.ToUpper();
            results[0] = cipher.Alphabet;
            results[2] = input;
            try
            {
                encrypted  = cipher.Encrypt(viewModel.Message);
                results[1] = encrypted;
            }
            catch (NullReferenceException)
            {
            }
            catch (Exception)
            {
                return(BadRequest(new { Result = false, Message = Text.InvalidCharacter }));
            }
            return(Json(results));
        }