using System.Security.Cryptography;
private const int keysize = 256;
private const string password="32461312";
private const string text= "<root><user NTID='3344-000001' Timestamp='2019 Aug 08 13:42:53' /></root>";
public static void Main()
SHA256 mySHA256 = SHA256Managed.Create();
byte[] key = mySHA256.ComputeHash(Encoding.ASCII.GetBytes(password));
byte[] iv = new byte[16] { 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0 };
string encrypted = EncryptString(text, key, iv);
string decrypted = DecryptString(encrypted, key, iv);
Console.WriteLine(encrypted);
Console.WriteLine(decrypted);
public static string EncryptString(string plainText, byte[] key, byte[] iv)
Aes encryptor = Aes.Create();
encryptor.Mode = CipherMode.CBC;
byte[] aesKey = new byte[32];
Array.Copy(key, 0, aesKey, 0, 32);
MemoryStream memoryStream = new MemoryStream();
ICryptoTransform aesEncryptor = encryptor.CreateEncryptor();
CryptoStream cryptoStream = new CryptoStream(memoryStream, aesEncryptor, CryptoStreamMode.Write);
byte[] plainBytes = Encoding.ASCII.GetBytes(plainText);
cryptoStream.Write(plainBytes, 0, plainBytes.Length);
cryptoStream.FlushFinalBlock();
byte[] cipherBytes = memoryStream.ToArray();
string cipherText = Convert.ToBase64String(cipherBytes, 0, cipherBytes.Length);
public static string DecryptString(string cipherText, byte[] key, byte[] iv)
Aes encryptor = Aes.Create();
encryptor.Mode = CipherMode.CBC;
byte[] aesKey = new byte[32];
Array.Copy(key, 0, aesKey, 0, 32);
MemoryStream memoryStream = new MemoryStream();
ICryptoTransform aesDecryptor = encryptor.CreateDecryptor();
CryptoStream cryptoStream = new CryptoStream(memoryStream, aesDecryptor, CryptoStreamMode.Write);
string plainText = String.Empty;
byte[] cipherBytes = Convert.FromBase64String(cipherText);
cryptoStream.Write(cipherBytes, 0, cipherBytes . Length);
cryptoStream.FlushFinalBlock();
byte[] plainBytes = memoryStream.ToArray();
plainText = Encoding.ASCII.GetString(plainBytes, 0, plainBytes.Length);