using System.Security.Cryptography;
public static class AESEncryption
#region ENCRYPT / DECRYPT
public static byte[] AES_Encrypt(byte[] bytesToBeEncrypted, byte[] passwordBytes)
byte[] encryptedBytes = null;
byte[] saltBytes = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 };
using (MemoryStream ms = new MemoryStream())
using (RijndaelManaged AES = new RijndaelManaged())
var key = new Rfc2898DeriveBytes(passwordBytes, saltBytes, 1000);
AES.Key = key.GetBytes(AES.KeySize / 8);
AES.IV = key.GetBytes(AES.BlockSize / 8);
AES.Mode = CipherMode.CBC;
using (var cs = new CryptoStream(ms, AES.CreateEncryptor(), CryptoStreamMode.Write))
cs.Write(bytesToBeEncrypted, 0, bytesToBeEncrypted.Length);
encryptedBytes = ms.ToArray();
public static byte[] AES_Decrypt(byte[] bytesToBeDecrypted, byte[] passwordBytes)
byte[] decryptedBytes = null;
byte[] saltBytes = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 };
using (MemoryStream ms = new MemoryStream())
using (RijndaelManaged AES = new RijndaelManaged())
var key = new Rfc2898DeriveBytes(passwordBytes, saltBytes, 1000);
AES.Key = key.GetBytes(AES.KeySize / 8);
AES.IV = key.GetBytes(AES.BlockSize / 8);
AES.Mode = CipherMode.CBC;
using (var cs = new CryptoStream(ms, AES.CreateDecryptor(), CryptoStreamMode.Write))
cs.Write(bytesToBeDecrypted, 0, bytesToBeDecrypted.Length);
decryptedBytes = ms.ToArray();
#endregion ENCRYPT / DECRYPT
public static string EncryptText(string input, string password = "E6t187^D43%F")
byte[] bytesToBeEncrypted = Encoding.UTF8.GetBytes(input);
byte[] passwordBytes = Encoding.UTF8.GetBytes(password);
passwordBytes = SHA256.Create().ComputeHash(passwordBytes);
byte[] bytesEncrypted = AES_Encrypt(bytesToBeEncrypted, passwordBytes);
string result = Convert.ToBase64String(bytesEncrypted);
public static string DecryptText(string input, string password = "E6t187^D43%F")
byte[] bytesToBeDecrypted = Convert.FromBase64String(input);
byte[] passwordBytes = Encoding.UTF8.GetBytes(password);
passwordBytes = SHA256.Create().ComputeHash(passwordBytes);
byte[] bytesDecrypted = AES_Decrypt(bytesToBeDecrypted, passwordBytes);
string result = Encoding.UTF8.GetString(bytesDecrypted);
public static class KeyGenerator
public static string GetUniqueKey(int maxSize = 15)
char[] chars = new char[62];
chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890".ToCharArray();
byte[] data = new byte[1];
using (RNGCryptoServiceProvider crypto = new RNGCryptoServiceProvider())
crypto.GetNonZeroBytes(data);
data = new byte[maxSize];
crypto.GetNonZeroBytes(data);
StringBuilder result = new StringBuilder(maxSize);
result.Append(chars[b % (chars.Length)]);
return result.ToString();
public static void Main()
var str = "Konsulan IT Bandung";
Console.WriteLine(AESEncryption.EncryptText(str));