コード例 #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();
        }
コード例 #2
0
        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);
        }
コード例 #3
0
        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);
        }
コード例 #4
0
        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);
        }
コード例 #5
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(_)));
            }
        }
コード例 #6
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));
        }
コード例 #7
0
ファイル: CipherManager.cs プロジェクト: PcloD/TEDCore
        public string Decrypt(string cipherText)
        {
            string plainText = cipherText;

            SimpleSubstitutionCipher simpleSubstitutionCipher = new SimpleSubstitutionCipher();

            simpleSubstitutionCipher.SetKey(SIMPLE_SUBSTITUTION_CIPHER_KEY);
            plainText = simpleSubstitutionCipher.Decrypt(plainText);

            AffineCipher affineCipher = new AffineCipher();

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

            CaesarCipher caesarCipher = new CaesarCipher();

            caesarCipher.SetKey(CAESAR_CIPHER_KEY);
            plainText = caesarCipher.Decrypt(plainText);

            return(plainText);
        }