AES Source Code

QuestionsCategory: C#AES Source Code
XorientXorient asked 1 year ago

Write a program demonstrate AES cryptographic algorithm using crypto

1 Answers
Best Answer
Mahesh DeshmaneMahesh Deshmane answered 1 year ago

Here is C# code snippet for AES (Advanced Encryption Standard) using .Net Framework in-build library


using System;
using System.IO;
using System.Security.Cryptography;
using System.Text;

namespace SymmetricCryptography
{
    class AdvancedEncryptionStandard
    {
        static void Main()
        {
            Console.WriteLine("Enter key to encrypt the text:");
            var key = Console.ReadLine();
            if (string.IsNullOrEmpty(key))
                return;

            Console.WriteLine("Enter message to encrypt");
            var message = Console.ReadLine();
            if (string.IsNullOrEmpty(message))
                return;

            var sha256 = SHA256.Create();
            var keyBytes = sha256.ComputeHash(Encoding.ASCII.GetBytes(key));

            var encryptedMessage = EncryptString(message, keyBytes);
            Console.WriteLine($"Encrypted Message : {encryptedMessage}");
            Console.WriteLine($"Decrypted Message : {DecryptString(encryptedMessage, keyBytes)}");
            Console.ReadLine();

        }

        static string EncryptString(string plainText, byte[] key)
        {
            var encryptor = Aes.Create();
            encryptor.Key = key;
            encryptor.IV = new byte[16]; //initialization vector

            using (var memoryStream = new MemoryStream())
            {
                var aesEncryptor = encryptor.CreateEncryptor();
                using (var cryptoStream = new CryptoStream(memoryStream, aesEncryptor, CryptoStreamMode.Write))
                {
                    var plainBytes = Encoding.ASCII.GetBytes(plainText);
                    cryptoStream.Write(plainBytes, 0, plainBytes.Length);
                    cryptoStream.FlushFinalBlock();
                    var cipherBytes = memoryStream.ToArray();
                    var cipherText = Convert.ToBase64String(cipherBytes, 0, cipherBytes.Length);
                    return cipherText;
                }
            }
        }

        static string DecryptString(string cipherText, byte[] key)
        {
            var decryptor = Aes.Create();
            decryptor.Key = key;
            decryptor.IV = new byte[16]; //initialization vector

            using (var memoryStream = new MemoryStream())
            {
                var aesDecryptor = decryptor.CreateDecryptor();

                using (var cryptoStream = new CryptoStream(memoryStream, aesDecryptor, CryptoStreamMode.Write))
                {
                    var cipherBytes = Convert.FromBase64String(cipherText);
                    cryptoStream.Write(cipherBytes, 0, cipherBytes.Length);
                    cryptoStream.FlushFinalBlock();
                    var plainBytes = memoryStream.ToArray();
                    return Encoding.ASCII.GetString(plainBytes, 0, plainBytes.Length);
                }
            }
        }
    }
}