Skip to content

Instantly share code, notes, and snippets.

@cosminpopescu14
Created August 21, 2019 03:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cosminpopescu14/33724b9af049ec1578b790f546ef15ec to your computer and use it in GitHub Desktop.
Save cosminpopescu14/33724b9af049ec1578b790f546ef15ec to your computer and use it in GitHub Desktop.
private byte[] EncryptAes(byte[] data, byte[] password)
{
byte[] array = null;
byte[] bytes = Encoding.Default.GetBytes(configuration.Salt);
using (MemoryStream memoryStream = new MemoryStream())
{
using (RijndaelManaged rijndaelManaged = new RijndaelManaged())
{
rijndaelManaged.KeySize = 256;
rijndaelManaged.BlockSize = 128;
Rfc2898DeriveBytes rfc2898DeriveBytes = new Rfc2898DeriveBytes(password, bytes, 2048);
rijndaelManaged.Key = rfc2898DeriveBytes.GetBytes(rijndaelManaged.KeySize / 8);
rijndaelManaged.IV = rfc2898DeriveBytes.GetBytes(rijndaelManaged.BlockSize / 8);
rijndaelManaged.Mode = CipherMode.CBC;
using (CryptoStream cryptoStream = new CryptoStream(memoryStream, rijndaelManaged.CreateEncryptor(), CryptoStreamMode.Write))
{
cryptoStream.Write(data, 0, data.Length);
}
return memoryStream.ToArray();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment