Skip to content

Instantly share code, notes, and snippets.

@jaguire
Created May 9, 2016 18:50
Show Gist options
  • Save jaguire/774b5c9adf1d5faa8dce6dc2299d163c to your computer and use it in GitHub Desktop.
Save jaguire/774b5c9adf1d5faa8dce6dc2299d163c to your computer and use it in GitHub Desktop.
AES Encryption
/// <summary>
/// AES (Advanced Encryption Standard) encryptor and dencryptor.
/// </summary>
public interface IAesEncryptor
{
/// <summary>
/// Pass in a UTF8 encoded string and get back a base64 encoded string of the encrypted data.
/// </summary>
/// <param name="utf8String">The UTF8 encoded string to encode.</param>
/// <param name="password">The password.</param>
/// <param name="salt">The salt.</param>
/// <returns>A base 64 encoded string of the encrypted data.</returns>
string EncryptString(string utf8String, string password, string salt);
/// <summary>
/// Encrypt data.
/// </summary>
/// <param name="data">The data to encrypt.</param>
/// <param name="password">The password.</param>
/// <param name="salt">The salt.</param>
/// <returns>The encrypted data.</returns>
byte[] Encrypt(byte[] data, string password, string salt);
/// <summary>
/// Pass in a base64 encoded string of endrypted data and get back a the decrypted data as a UTF8 string.
/// </summary>
/// <param name="base64String">The base 64 encoded string of the encrypted data.</param>
/// <param name="password">The password.</param>
/// <param name="salt">The salt.</param>
/// <returns>A UTF8 encoded string of decrypted data.</returns>
string DecryptString(string base64String, string password, string salt);
/// <summary>
/// Decrypt data.
/// </summary>
/// <param name="data">The data to decrypt.</param>
/// <param name="password">The password.</param>
/// <param name="salt">The salt.</param>
/// <returns>The decrypted data.</returns>
byte[] Decrypt(byte[] data, string password, string salt);
}
public class AesEncryptor : IAesEncryptor
{
public string EncryptString(string utf8String, string password, string salt)
{
return Convert.ToBase64String(Encrypt(Encoding.UTF8.GetBytes(utf8String), password, salt));
}
public byte[] Encrypt(byte[] data, string password, string salt)
{
using (var algorithm = GetAlgorithm(password, salt))
using (var transform = algorithm.CreateEncryptor())
return Transform(transform, data);
}
public string DecryptString(string base64String, string password, string salt)
{
return Encoding.UTF8.GetString(Decrypt(Convert.FromBase64String(base64String), password, salt));
}
public byte[] Decrypt(byte[] data, string password, string salt)
{
using (var algorithm = GetAlgorithm(password, salt))
using (var transform = algorithm.CreateDecryptor())
return Transform(transform, data);
}
private static SymmetricAlgorithm GetAlgorithm(string password, string salt)
{
var derived = new Rfc2898DeriveBytes(password, Encoding.Default.GetBytes(salt.PadRight(8)), 1000);
var algorithm = new AesManaged(); // Fully managed implementation. Slower but has no dependencies.
////var algorithm = new AesCryptoServiceProvider(); // Native OS based implementation. Faster but depends on the OS.
algorithm.Key = derived.GetBytes(algorithm.KeySize >> 3);
algorithm.IV = derived.GetBytes(algorithm.BlockSize >> 3);
return algorithm;
}
private static byte[] Transform(ICryptoTransform transform, byte[] data)
{
using (var buffer = new MemoryStream())
using (var stream = new CryptoStream(buffer, transform, CryptoStreamMode.Write))
{
stream.Write(data, 0, data.Length);
stream.FlushFinalBlock();
return buffer.ToArray();
}
}
}
var testData = "test text";
var password = "password";
var salt = "salt";
var aes = new AesEncryptor();
// convert whatever you want to encrypt to binary data
var data = Encoding.Default.GetBytes(testData);
// encrypt it
var encrypted = aes.Encrypt(data, password, salt);
// you can base64 encode encrypted data to allow safe copy pasting
var safeString = Convert.ToBase64String(encrypted);
encrypted = Convert.FromBase64String(safeString);
// decript it
var decrypted = aes.Decrypt(encrypted, password, salt);
// convert binary data back to original type
var finalValue = Encoding.Default.GetString(decrypted);
$"{finalValue} = {safeString}".Dump();
// For string data that you also want to store as strings, you can use the helper methods
var stringEncrypted = aes.EncryptString(testData, password, salt);
var stringDecrypted = aes.DecryptString(stringEncrypted, password, salt);
$"{stringDecrypted} = {stringEncrypted}".Dump("String Helpers");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment