Exemple #1
0
        private static void TestAesGCM()
        {
            const string original = "Text to encrypt";

            var aesGCM = new AesGCMEncryption();

            var gcmKey = aesGCM.GenerateRandomNumber(32);
            var nonce  = aesGCM.GenerateRandomNumber(12);

            try
            {
                (byte[] ciphereText, byte[] tag)result = aesGCM.Encrypt(Encoding.UTF8.GetBytes(original), gcmKey, nonce, Encoding.UTF8.GetBytes("some metadata"));
                byte[] decryptedText = aesGCM.Decrypt(result.ciphereText, gcmKey, nonce, result.tag, Encoding.UTF8.GetBytes("some metadata"));

                Console.WriteLine("AES GCM Encryption Demonstration in .NET");
                Console.WriteLine("----------------------------------------");
                Console.WriteLine();
                Console.WriteLine("Original Text = " + original);
                Console.WriteLine("Encrypted Text = " + Convert.ToBase64String(result.ciphereText));
                Console.WriteLine("Decrypted Text = " + Encoding.UTF8.GetString(decryptedText));
            }
            catch (CryptographicException ex)
            {
                Console.WriteLine(ex.Message);
            }
            catch (ArgumentException ex)
            {
                Console.WriteLine(ex.Message);
            }
        }