using System;
using SecurityDriven.Inferno;
using SecurityDriven.Inferno.Extensions;

namespace RandomAES_final {
    class Program {
        static void Main(string[] args) {
            // Generate key
            var rng = new CryptoRandom();
            var key = rng.NextBytes(32);
            Console.WriteLine($"Key: {key.ToBase16()}");
            Console.WriteLine();

            // Get message
            Console.Write("Get message to encrypt: ");
            var message = Console.ReadLine();
            var messageArray = message.ToBytes().AsArraySegment();

            // AEAD encrypt
            var encData = SuiteB.Encrypt(key, messageArray);
            var encString = encData.ToBase16();
            Console.WriteLine($"Encrypted data: {encString}");
            Console.WriteLine();

            // Get message to decrypt
            Console.Write("Enter message to decrypt (or just ENTER for original one): ");
            var ciphertextString = Console.ReadLine();
            if (string.IsNullOrEmpty(ciphertextString)) ciphertextString = encString;
            var ciphertextArray = ciphertextString.FromBase16().AsArraySegment();

            // Authenticate
            var isAuthenticated = SuiteB.Authenticate(key, ciphertextArray);
            if (!isAuthenticated) {
                Console.WriteLine("Message is not authenticated!");
            }
            else {
                Console.WriteLine("Message authenticated successfully.");

                var plaintextBytes = SuiteB.Decrypt(key, ciphertextArray);
                var plaintextString = plaintextBytes.FromBytes();
                Console.WriteLine($"Decrypted message: {plaintextString}");
            }
        }
    }
}
