Skip to content

Instantly share code, notes, and snippets.

@enif-lee
Created March 27, 2020 05:50
Show Gist options
  • Save enif-lee/0218083e64d1974449b595467b0d7f8b to your computer and use it in GitHub Desktop.
Save enif-lee/0218083e64d1974449b595467b0d7f8b to your computer and use it in GitHub Desktop.
C# AES256 encryption and decryption example for using easily.
public class CryptoUtil
{
public static byte[] EncryptAes256(byte[] bytes, byte[] key)
{
using var aes = new RijndaelManaged
{
Key = CreateDeriveBytes(key, 32),
};
aes.GenerateIV();
using var encryptor = aes.CreateEncryptor();
using var encryptedStream = new MemoryStream();
// Do not replace implicit using expression, when CryptoStream aws disposed, final block deliver to memory stream.
using (var cryptoStream = new CryptoStream(encryptedStream, encryptor, CryptoStreamMode.Write))
{
cryptoStream.Write(bytes);
}
var encrypted = encryptedStream.ToArray();
return aes.IV.Concat(encrypted).ToArray();
}
public static string DecryptAes256(string encrypted, string key)
{
var decryptAes256 = DecryptAes256(Convert.FromBase64String(encrypted), Encoding.UTF8.GetBytes(key));
return Encoding.UTF8.GetString(decryptAes256);
}
public static byte[] DecryptAes256(byte[] encrypted, byte[] key)
{
using var rijndeal = new RijndaelManaged
{
Key = CreateDeriveBytes(key, 32)
};
var iv = encrypted.Take(16).ToArray();
rijndeal.IV = iv;
var encryptedBytes = encrypted.Skip(16).ToArray();
using var memoryStream = new MemoryStream();
using var decryptor = rijndeal.CreateDecryptor();
using (var cryptoStream = new CryptoStream(memoryStream, decryptor, CryptoStreamMode.Write))
{
cryptoStream.Write(encryptedBytes);
}
return memoryStream.ToArray();
}
private static byte[] CreateDeriveBytes(byte[] keyBytes, int size)
{
using var sha512 = SHA512.Create();
var saltBytes = sha512.ComputeHash(keyBytes);
using var derivBytes = new Rfc2898DeriveBytes(keyBytes, saltBytes, 65536);
return derivBytes.GetBytes(size);
}
}
@CSaratakij
Copy link

Thank you, you save my ass.

@enif-lee
Copy link
Author

enif-lee commented Mar 29, 2021 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment