A complete example of AES encryption and decryption implemented by C

  • 2021-10-27 08:42:56
  • OfStack

In this paper, an example is given to describe AES encryption and decryption realized by C #. Share it for your reference, as follows:


/******************************************************************
 *  Founder: HTL
 *  Description: C# AES Encryption and decryption 
 *******************************************************************/
using System;
using System.Security.Cryptography;
using System.Text;
using System.IO;
public class Test
{
 public static void Main()
 {
 // Password 
 string password="1234567890123456";
 // Encryption initialization vector 
 string iv="  ";
 string message=AESEncrypt("abcdefghigklmnopqrstuvwxyz0123456789",password,iv);
 Console.WriteLine(message);
 message=AESDecrypt("8Z3dZzqn05FmiuBLowExK0CAbs4TY2GorC2dDPVlsn/tP+VuJGePqIMv1uSaVErr",password,iv);
 Console.WriteLine(message);
 }
 /// <summary>
 /// AES Encryption 
 /// </summary>
 /// <param name="text"> Encrypted character </param>
 /// <param name="password"> Encrypted password </param>
 /// <param name="iv"> Key </param>
 /// <returns></returns>
 public static string AESEncrypt(string text, string password, string iv)
 {
 RijndaelManaged rijndaelCipher = new RijndaelManaged();
 rijndaelCipher.Mode = CipherMode.CBC;
 rijndaelCipher.Padding = PaddingMode.PKCS7;
 rijndaelCipher.KeySize = 128;
 rijndaelCipher.BlockSize = 128;
 byte[] pwdBytes = System.Text.Encoding.UTF8.GetBytes(password);
 byte[] keyBytes = new byte[16];
 int len = pwdBytes.Length;
 if (len > keyBytes.Length) len = keyBytes.Length;
 System.Array.Copy(pwdBytes, keyBytes, len);
 rijndaelCipher.Key = keyBytes;
 byte[] ivBytes = System.Text.Encoding.UTF8.GetBytes(iv);
 rijndaelCipher.IV = new byte[16];
 ICryptoTransform transform = rijndaelCipher.CreateEncryptor();
 byte[] plainText = Encoding.UTF8.GetBytes(text);
 byte[] cipherBytes = transform.TransformFinalBlock(plainText, 0, plainText.Length);
 return Convert.ToBase64String(cipherBytes);
 }
 /// <summary>
 /// AES Decryption 
 /// </summary>
 /// <param name="text"></param>
 /// <param name="password"></param>
 /// <param name="iv"></param>
 /// <returns></returns>
 public static string AESDecrypt(string text, string password, string iv)
 {
 RijndaelManaged rijndaelCipher = new RijndaelManaged();
 rijndaelCipher.Mode = CipherMode.CBC;
 rijndaelCipher.Padding = PaddingMode.PKCS7;
 rijndaelCipher.KeySize = 128;
 rijndaelCipher.BlockSize = 128;
 byte[] encryptedData = Convert.FromBase64String(text);
 byte[] pwdBytes = System.Text.Encoding.UTF8.GetBytes(password);
 byte[] keyBytes = new byte[16];
 int len = pwdBytes.Length;
 if (len > keyBytes.Length) len = keyBytes.Length;
 System.Array.Copy(pwdBytes, keyBytes, len);
 rijndaelCipher.Key = keyBytes;
 byte[] ivBytes = System.Text.Encoding.UTF8.GetBytes(iv);
 rijndaelCipher.IV = ivBytes;
 ICryptoTransform transform = rijndaelCipher.CreateDecryptor();
 byte[] plainText = transform.TransformFinalBlock(encryptedData, 0, encryptedData.Length);
 return Encoding.UTF8.GetString(plainText);
 }
}

PS: Friends who are interested in encryption and decryption can also refer to the online tools of this site:

On-line detection of password security:

tools.ofstack.com/password/my_password_safe

High Strength Password Generator:

tools.ofstack.com/password/CreateStrongPassword

MD5 Online Encryption Tool:

tools.ofstack.com/password/CreateMD5Password

Thunderbolt, Express, Cyclone URL Encryption/Decryption Tool:

tools.ofstack.com/password/urlrethunder

Online hash/hash algorithm encryption tool:

tools.ofstack.com/password/hash_encrypt

For more information about C #, please see the topic of this site: "C # Encryption and Decryption Algorithms and Skills Summary", "C # Form Operation Skills Summary", "C # Common Control Usage Tutorial", "WinForm Control Usage Summary", "C # Programming Thread Use Skills Summary", "C # Operating Excel Skills Summary", "XML File Operation Skills Summary in C #", "C # Data Structure and Algorithm Tutorial", "C # Array Operation Skills Summary" and "C # Object-Oriented Programming Introduction Tutorial"

I hope this article is helpful to everyone's C # programming.


Related articles: