Beispiel #1
0
        private static void TestAesCBC()
        {
            const string original = "Text to encrypt";
            var          aes      = new AesEncryption();
            var          key      = aes.GenerateRandomNumber(32);
            var          iv       = aes.GenerateRandomNumber(16);


            var encrypted = aes.Encrypt(Encoding.UTF8.GetBytes(original), key, iv);
            var decrypted = aes.Decrypt(encrypted, key, iv);

            var decryptedMessage = Encoding.UTF8.GetString(decrypted);

            Console.WriteLine("AES Encryption Demonstration in .NET");
            Console.WriteLine("------------------------------------");
            Console.WriteLine();
            Console.WriteLine("Original Text = " + original);
            Console.WriteLine("Encrypted Text = " + Convert.ToBase64String(encrypted));
            Console.WriteLine("Decrypted Text = " + decryptedMessage);
        }
Beispiel #2
0
        static void Main(string[] args)
        {
            // TODO : Generate a 32 byte key
            // TODO : Generate a 16 byte initialization vector
            // TODO : Encrypt some text, base64 the result and display to the console.
            // TODO : Decrypt the encrypted text, base64 the result and display to the console.

            var key = AesEncryption.GenerateRandomNumber(32);
            var iv  = AesEncryption.GenerateRandomNumber(16);

            var input        = "Hello, World!";
            var inputAsBytes = Encoding.UTF8.GetBytes(input);

            var encrypted = AesEncryption.Encrypt(inputAsBytes, key, iv);
            var decrypted = AesEncryption.Decrypt(encrypted, key, iv);

            Console.WriteLine($"'{input}'");
            Console.WriteLine($"encrypted = '{Convert.ToBase64String(encrypted)}'");
            Console.WriteLine($"decrypted = '{Encoding.UTF8.GetString(decrypted)}'");
        }