Example #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();
        }
        public void AffineDecryptValidInputUpperCase()
        {
            // Arrange
            AffineCipher cipher   = new AffineCipher();
            string       input    = "TEST";
            string       expected = "ONHO";
            // Act
            string actual = cipher.Decrypt(input);

            // Assert
            Assert.AreEqual(expected, actual);
        }
        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);
        }
        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);
        }
        public void AffineDecryptValidInputWithSpace()
        {
            // Arrange
            AffineCipher cipher   = new AffineCipher();
            string       input    = "test zxyq asdf";
            string       expected = "onho eqxt lhgu";
            // Act
            string actual = cipher.Decrypt(input);

            // Assert
            Assert.AreEqual(expected, actual);
        }
        public void AffineDecryptValidInputMixedCase()
        {
            // Arrange
            AffineCipher cipher   = new AffineCipher();
            string       input    = "testTESTtEsT";
            string       expected = "onhoONHOoNhO";
            // Act
            string actual = cipher.Decrypt(input);

            // Assert
            Assert.AreEqual(expected, actual);
        }
Example #7
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(_)));
            }
        }
Example #8
0
        public IActionResult AffineDecrypt([FromBody] AffineCipherViewModel viewModel)
        {
            AffineCipher cipher = new AffineCipher(viewModel.KeyA, viewModel.KeyB)
            {
                Alphabet = Alphabets.GetAlphabet((Alphabets.AlphabetType)viewModel.AlphabetType)
            };

            string decrypted = "";

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

            return(Json(decrypted));
        }
Example #9
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);
        }
Example #10
0
        static void Main(string[] args)
        {
            ICryptoKey key = new AffineKey(2, 5);

            Console.WriteLine(key.Validator.IsValid(key));

            //SuperKey k = new SuperKey();
            //k.SomeNewShit.Add("some string");

            //ICryptoKey key2 = k;
            //Console.WriteLine(key.Validator.IsValid(key2));

            ////int[,] newKyes = new int[2,2] { {1,2}, {3,4} };
            ////key.KeyCodes = newKyes;

            AffineCipher cipher = new AffineCipher(new CharactersAlphabet(), (AffineKey)key);

            //cipher.Key = key2;

            Console.Read();
        }
Example #11
0
        private void tbAffineKeyA_TextChanged(object sender, EventArgs e)
        {
            if (!regexAffineKeyAText.IsMatch(this.tbAffineKeyA.Text))
            {
                this.errorProvider1.SetError(this.tbAffineKeyA,
                                             $"数据格式错误,数据为0-25之间的整数");
                this.myCipherKeys.AffineKeyA = MyCipherKeys.InvalidIntValue;
                return;
            }

            int affineKeyA = int.Parse(tbAffineKeyA.Text);

            if (!AffineCipher.IsKeyASuitable(affineKeyA, out string message))
            {
                this.errorProvider1.SetError(this.tbAffineKeyA, message);
                this.myCipherKeys.AffineKeyA = MyCipherKeys.InvalidIntValue;
                return;
            }

            this.myCipherKeys.AffineKeyA = affineKeyA;

            this.errorProvider1.SetError(this.tbAffineKeyA, null);
        }
Example #12
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));
        }
Example #13
0
 public void Encode_no()
 {
     Assert.Equal("fu", AffineCipher.Encode("no", 15, 18));
 }
Example #14
0
 public void Encode_deep_thought()
 {
     Assert.Equal("iynia fdqfb ifje", AffineCipher.Encode("Truth is fiction.", 5, 17));
 }
Example #15
0
 public void Encode_o_m_g()
 {
     Assert.Equal("hjp", AffineCipher.Encode("O M G", 25, 47));
 }
Example #16
0
 public void Encode_omg()
 {
     Assert.Equal("lvz", AffineCipher.Encode("OMG", 21, 3));
 }
Example #17
0
 public void Decode_with_too_many_spaces()
 {
     Assert.Equal("jollygreengiant", AffineCipher.Decode("vszzm    cly   yd cg    qdp", 15, 16));
 }
Example #18
0
 public void Decode_with_a_not_coprime_to_m()
 {
     Assert.Throws <ArgumentException>(() => AffineCipher.Decode("Test", 13, 5));
 }
Example #19
0
 public void Decode_all_the_letters()
 {
     Assert.Equal("thequickbrownfoxjumpsoverthelazydog", AffineCipher.Decode("swxtj npvyk lruol iejdc blaxk swxmh qzglf", 17, 33));
 }
Example #20
0
 public void Decode_with_no_spaces_in_input()
 {
     Assert.Equal("thequickbrownfoxjumpsoverthelazydog", AffineCipher.Decode("swxtjnpvyklruoliejdcblaxkswxmhqzglf", 17, 33));
 }
Example #21
0
 public void Decode_numbers()
 {
     Assert.Equal("testing123testing", AffineCipher.Decode("odpoz ub123 odpoz ub", 25, 7));
 }
Example #22
0
 public void Encode_yes()
 {
     Assert.Equal("xbt", AffineCipher.Encode("yes", 5, 7));
 }
Example #23
0
 public void Decode_a_sentence()
 {
     Assert.Equal("anobstacleisoftenasteppingstone", AffineCipher.Decode("qdwju nqcro muwhn odqun oppmd aunwd o", 19, 16));
 }
Example #24
0
 public void Decode_exercism()
 {
     Assert.Equal("exercism", AffineCipher.Decode("tytgn fjr", 3, 7));
 }
Example #25
0
 public void Encode_with_a_not_coprime_to_m()
 {
     Assert.Throws <ArgumentException>(() => AffineCipher.Encode("This is a test.", 6, 17));
 }
Example #26
0
 public void Encode_mindblowingly()
 {
     Assert.Equal("rzcwa gnxzc dgt", AffineCipher.Encode("mindblowingly", 11, 15));
 }
Example #27
0
 public void Encode_numbers()
 {
     Assert.Equal("jqgjc rw123 jqgjc rw", AffineCipher.Encode("Testing,1 2 3, testing.", 3, 4));
 }