Skip to content

Instantly share code, notes, and snippets.

@MrChrisHammond
Last active September 4, 2018 02:34
Show Gist options
  • Save MrChrisHammond/d78cf78c02b435824c1806e2850b193f to your computer and use it in GitHub Desktop.
Save MrChrisHammond/d78cf78c02b435824c1806e2850b193f to your computer and use it in GitHub Desktop.
CryptoTools
/// <summary>
/// Encrypt data with AesCryptoServiceProvider, which should use FIPS 140-2 compliant library
/// </summary>
/// <param name="key"></param>
/// <param name="IV"></param>
/// <param name="data"></param>
/// <returns></returns>
public byte[] EncryptDataAESCryptoServiceProvider(byte[] key, byte[] IV, object data)
{
byte[] encryptedData;
using (AesCryptoServiceProvider Aes = new AesCryptoServiceProvider())
{
Aes.Mode = CipherMode.CBC;
ICryptoTransform t = Aes.CreateEncryptor(key, IV);
BinaryFormatter bf = new BinaryFormatter();
using (MemoryStream ms = new MemoryStream())
{
using (CryptoStream cs = new CryptoStream(ms, t, CryptoStreamMode.Write))
{
bf.Serialize(cs, data);
cs.FlushFinalBlock();
cs.Close();
encryptedData = ms.ToArray();
}
}
}
return encryptedData;
}
/// <summary>
/// Decrypt data with AesCryptoServiceProvider, which should use FIPS 140-2 compliant library
/// </summary>
/// <param name="key"></param>
/// <param name="IV"></param>
/// <param name="data"></param>
/// <returns></returns>
public static object DecryptDataAESCryptoServiceProvider(byte[] key, byte[] IV, byte[] data)
{
object decryptedData;
using (AesCryptoServiceProvider Aes = new AesCryptoServiceProvider())
{
Aes.Mode = CipherMode.CBC;
ICryptoTransform t = Aes.CreateDecryptor(key, IV);
BinaryFormatter bf = new BinaryFormatter();
using (MemoryStream ms = new MemoryStream(data))
{
using (CryptoStream cs = new CryptoStream(ms, t, CryptoStreamMode.Read))
{
decryptedData = bf.Deserialize(cs);
cs.Close();
}
}
}
return decryptedData;
}
/// <summary>
/// Encrypt byte array.
/// </summary>
/// <param name="key"></param>
/// <param name="IV"></param>
/// <param name="data"></param>
/// <returns></returns>
public static byte[] EncryptData(byte[] key, byte[] IV, object data)
{
byte[] encryptedData;
RijndaelManaged rj = new RijndaelManaged();
rj.Mode = CipherMode.CBC;
ICryptoTransform t = rj.CreateEncryptor(key, IV);
BinaryFormatter bf = new BinaryFormatter();
using (MemoryStream ms = new MemoryStream())
{
using (CryptoStream cs = new CryptoStream(ms, t, CryptoStreamMode.Write))
{
bf.Serialize(cs, data);
cs.FlushFinalBlock();
cs.Close();
encryptedData = ms.ToArray();
}
}
return encryptedData;
}
/// <summary>
/// Decrypt byte array
/// </summary>
/// <param name="key"></param>
/// <param name="IV"></param>
/// <param name="data"></param>
/// <returns></returns>
public static object DecryptData(byte[] key, byte[] IV, byte[] data)
{
object decryptedData;
RijndaelManaged rj = new RijndaelManaged();
rj.Mode = CipherMode.CBC;
ICryptoTransform t = rj.CreateDecryptor(key, IV);
BinaryFormatter bf = new BinaryFormatter();
using (MemoryStream ms = new MemoryStream(data))
{
using (CryptoStream cs = new CryptoStream(ms, t, CryptoStreamMode.Read))
{
decryptedData = bf.Deserialize(cs);
cs.Close();
}
}
return decryptedData;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment