Hi
How i can encrypt and decrypt value using AES 256
Hi
How i can encrypt and decrypt value using AES 256
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Pankajkumar PatelRank #181 · 13.4K reputationPosted Dec 14, 2022, 2:27 AM EST
Hi Piyush Mohan,
Below is one of the good article with practical example:
Hope this will help you!
Amit MohantyRank #112 · 20K reputationPosted Dec 14, 2022, 3:49 AM EST
Check the below code
class AesEncryption { // Create secret IV static byte[] iv = new byte[16] { 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0 }; public static string EncryptString(string plainText, byte[] key) { // Instantiate a new Aes object to perform string symmetric encryption Aes encryptor = Aes.Create(); encryptor.Mode = CipherMode.CBC; // Set key and IV byte[] aesKey = new byte[32]; Array.Copy(key, 0, aesKey, 0, 32); encryptor.Key = aesKey; encryptor.IV = iv; // Instantiate a new MemoryStream object to contain the encrypted bytes MemoryStream memoryStream = new MemoryStream(); // Instantiate a new encryptor from our Aes object ICryptoTransform aesEncryptor = encryptor.CreateEncryptor(); // Instantiate a new CryptoStream object to process the data and write it to the // memory stream CryptoStream cryptoStream = new CryptoStream(memoryStream, aesEncryptor, CryptoStreamMode.Write); // Convert the plainText string into a byte array byte[] plainBytes = Encoding.ASCII.GetBytes(plainText); // Encrypt the input plaintext string cryptoStream.Write(plainBytes, 0, plainBytes.Length); // Complete the encryption process cryptoStream.FlushFinalBlock(); // Convert the encrypted data from a MemoryStream to a byte array byte[] cipherBytes = memoryStream.ToArray(); // Close both the MemoryStream and the CryptoStream memoryStream.Close(); cryptoStream.Close(); // Convert the encrypted byte array to a base64 encoded string string cipherText = Convert.ToBase64String(cipherBytes, 0, cipherBytes.Length); // Return the encrypted data as a string return cipherText; } public static string DecryptString(string cipherText, byte[] key) { // Instantiate a new Aes object to perform string symmetric encryption Aes encryptor = Aes.Create(); encryptor.Mode = CipherMode.CBC; // Set key and IV byte[] aesKey = new byte[32]; Array.Copy(key, 0, aesKey, 0, 32); encryptor.Key = aesKey; encryptor.IV = iv; // Instantiate a new MemoryStream object to contain the encrypted bytes MemoryStream memoryStream = new MemoryStream(); // Instantiate a new encryptor from our Aes object ICryptoTransform aesDecryptor = encryptor.CreateDecryptor(); // Instantiate a new CryptoStream object to process the data and write it to the // memory stream CryptoStream cryptoStream = new CryptoStream(memoryStream, aesDecryptor, CryptoStreamMode.Write); // Will contain decrypted plaintext string plainText = String.Empty; try { // Convert the ciphertext string into a byte array byte[] cipherBytes = Convert.FromBase64String(cipherText); // Decrypt the input ciphertext string cryptoStream.Write(cipherBytes, 0, cipherBytes.Length); // Complete the decryption process cryptoStream.FlushFinalBlock(); // Convert the decrypted data from a MemoryStream to a byte array byte[] plainBytes = memoryStream.ToArray(); // Convert the decrypted byte array to string plainText = Encoding.ASCII.GetString(plainBytes, 0, plainBytes.Length); } finally { // Close both the MemoryStream and the CryptoStream memoryStream.Close(); cryptoStream.Close(); } // Return the decrypted data as a string return plainText; } public static void HowToUse() { string SecurityKey = "SecurityKey$%^&%*W"; Console.WriteLine("Enter a text to encrypt : "); string inputValue = Console.ReadLine(); // Create sha256 hash SHA256 mySHA256 = SHA256Managed.Create(); byte[] key = mySHA256.ComputeHash(Encoding.ASCII.GetBytes(SecurityKey)); string encrypted = EncryptString(inputValue.ToString(), key); string decrypted = DecryptString(encrypted, key); Console.Write("\n"); Console.WriteLine("Encrypted test is: {0}", encrypted); Console.WriteLine("Original text is: {0}", decrypted); } }