Improved AES encryption class

Thanks to Eddie E.
This commit is contained in:
MaxXor 2015-08-15 17:13:19 +02:00
parent a7fd5327f1
commit b9e036c418
2 changed files with 110 additions and 259 deletions

View File

@ -18,170 +18,118 @@ public static void PreHashKey(string key)
}
}
public static string Encrypt(string input, string key)
{
return Convert.ToBase64String(Encrypt(Encoding.UTF8.GetBytes(input), Encoding.UTF8.GetBytes(key)));
}
public static string Encrypt(string input)
{
if (_key == null) throw new Exception("The key can not be null.");
byte[] data = Encoding.UTF8.GetBytes(input), encdata;
try
{
using (var ms = new MemoryStream())
{
using (var rd = new RijndaelManaged())
{
rd.Key = _key;
rd.GenerateIV();
byte[] iv = rd.IV;
using (var cs = new CryptoStream(ms, rd.CreateEncryptor(), CryptoStreamMode.Write))
{
ms.Write(iv, 0, iv.Length); // write first 16 bytes IV, followed by encrypted message
cs.Write(data, 0, data.Length);
cs.FlushFinalBlock();
}
iv = null;
}
encdata = ms.ToArray();
}
return Convert.ToBase64String(encdata);
}
catch
{
return string.Empty;
}
finally
{
encdata = null;
data = null;
}
return Convert.ToBase64String(Encrypt(Encoding.UTF8.GetBytes(input)));
}
public static byte[] Encrypt(byte[] input)
{
if (_key == null) throw new Exception("The key can not be null.");
if (_key == null || _key.Length == 0) throw new Exception("Key can not be empty.");
if (input == null || input.Length == 0) throw new ArgumentException("Input can not be empty.");
byte[] data = input, encdata;
byte[] data = input, encdata = new byte[0];
try
{
using (var ms = new MemoryStream())
{
using (var rd = new RijndaelManaged())
using (var rd = new RijndaelManaged { Key = _key })
{
rd.Key = _key;
rd.GenerateIV();
byte[] iv = rd.IV;
using (var cs = new CryptoStream(ms, rd.CreateEncryptor(), CryptoStreamMode.Write))
{
ms.Write(iv, 0, iv.Length); // write first 16 bytes IV, followed by encrypted message
ms.Write(rd.IV, 0, rd.IV.Length); // write first 16 bytes IV, followed by encrypted message
cs.Write(data, 0, data.Length);
cs.FlushFinalBlock();
}
iv = null;
}
encdata = ms.ToArray();
}
return encdata;
}
catch
{
return new byte[0];
}
finally
return encdata;
}
public static byte[] Encrypt(byte[] input, byte[] key)
{
if (key == null || key.Length == 0) throw new Exception("Key can not be empty.");
if (input == null || input.Length == 0) throw new ArgumentException("Input can not be empty.");
using (var md5 = new MD5CryptoServiceProvider())
{
encdata = null;
data = null;
key = md5.ComputeHash(key);
}
byte[] data = input, encdata = new byte[0];
try
{
using (var ms = new MemoryStream())
{
using (var rd = new RijndaelManaged { Key = key })
{
rd.GenerateIV();
using (var cs = new CryptoStream(ms, rd.CreateEncryptor(), CryptoStreamMode.Write))
{
ms.Write(rd.IV, 0, rd.IV.Length); // write first 16 bytes IV, followed by encrypted message
cs.Write(data, 0, data.Length);
}
}
encdata = ms.ToArray();
}
}
catch
{
}
return encdata;
}
public static string Decrypt(string input)
{
if (_key == null) throw new Exception("The key can not be null.");
byte[] data;
int i;
try
{
using (var ms = new MemoryStream(Convert.FromBase64String(input)))
{
using (var rd = new RijndaelManaged())
{
byte[] iv = new byte[IVLENGTH];
ms.Read(iv, 0, IVLENGTH); // read first 16 bytes for IV, followed by encrypted message
rd.IV = iv;
rd.Key = _key;
using (var cs = new CryptoStream(ms, rd.CreateDecryptor(), CryptoStreamMode.Read))
{
data = new byte[ms.Length - IVLENGTH + 1];
i = cs.Read(data, 0, data.Length);
}
iv = null;
}
}
return Encoding.UTF8.GetString(data, 0, i);
}
catch
{
return string.Empty;
}
finally
{
data = null;
}
return Encoding.UTF8.GetString(Decrypt(Convert.FromBase64String(input)));
}
public static byte[] Decrypt(byte[] input)
{
if (_key == null) throw new Exception("The key can not be null.");
if (_key == null || _key.Length == 0) throw new Exception("Key can not be empty.");
if (input == null || input.Length == 0) throw new ArgumentException("Input can not be empty.");
byte[] temp, data;
byte[] data = new byte[0];
try
{
using (var ms = new MemoryStream(input))
{
using (var rd = new RijndaelManaged())
using (var rd = new RijndaelManaged { Key = _key })
{
byte[] iv = new byte[IVLENGTH];
ms.Read(iv, 0, IVLENGTH); // read first 16 bytes for IV, followed by encrypted message
rd.IV = iv;
rd.Key = _key;
using (var cs = new CryptoStream(ms, rd.CreateDecryptor(), CryptoStreamMode.Read))
{
temp = new byte[ms.Length - IVLENGTH + 1];
int read = cs.Read(temp, 0, temp.Length);
data = new byte[read];
Buffer.BlockCopy(temp, 0, data, 0, read);
byte[] temp = new byte[ms.Length - IVLENGTH + 1];
data = new byte[cs.Read(temp, 0, temp.Length)];
Buffer.BlockCopy(temp, 0, data, 0, data.Length);
}
iv = null;
}
}
return data;
}
catch
{
return new byte[0];
}
finally
{
temp = null;
data = null;
}
return data;
}
}
}

View File

@ -18,215 +18,118 @@ public static void PreHashKey(string key)
}
}
public static string Encrypt(string input)
public static string Encrypt(string input, string key)
{
if (_key == null) throw new Exception("The key can not be null.");
byte[] data = Encoding.UTF8.GetBytes(input), encdata;
try
{
using (var ms = new MemoryStream())
{
using (var rd = new RijndaelManaged())
{
rd.Key = _key;
rd.GenerateIV();
byte[] iv = rd.IV;
using (var cs = new CryptoStream(ms, rd.CreateEncryptor(), CryptoStreamMode.Write))
{
ms.Write(iv, 0, iv.Length); // write first 16 bytes IV, followed by encrypted message
cs.Write(data, 0, data.Length);
cs.FlushFinalBlock();
}
iv = null;
}
encdata = ms.ToArray();
}
return Convert.ToBase64String(encdata);
}
catch
{
return string.Empty;
}
finally
{
encdata = null;
data = null;
}
return Convert.ToBase64String(Encrypt(Encoding.UTF8.GetBytes(input), Encoding.UTF8.GetBytes(key)));
}
public static string Encrypt(string input, string keyy)
public static string Encrypt(string input)
{
byte[] key, data = Encoding.UTF8.GetBytes(input), encdata;
try
{
using (var md5 = new MD5CryptoServiceProvider())
{
key = md5.ComputeHash(Encoding.UTF8.GetBytes(keyy));
}
using (var ms = new MemoryStream())
{
using (var rd = new RijndaelManaged())
{
rd.Key = key;
rd.GenerateIV();
byte[] iv = rd.IV;
using (var cs = new CryptoStream(ms, rd.CreateEncryptor(), CryptoStreamMode.Write))
{
ms.Write(iv, 0, iv.Length); // write first 16 bytes IV, followed by encrypted message
cs.Write(data, 0, data.Length);
cs.FlushFinalBlock();
}
iv = null;
}
encdata = ms.ToArray();
}
return Convert.ToBase64String(encdata);
}
catch
{
return string.Empty;
}
finally
{
encdata = null;
data = null;
}
return Convert.ToBase64String(Encrypt(Encoding.UTF8.GetBytes(input)));
}
public static byte[] Encrypt(byte[] input)
{
if (_key == null) throw new Exception("The key can not be null.");
if (_key == null || _key.Length == 0) throw new Exception("Key can not be empty.");
if (input == null || input.Length == 0) throw new ArgumentException("Input can not be empty.");
byte[] data = input, encdata;
byte[] data = input, encdata = new byte[0];
try
{
using (var ms = new MemoryStream())
{
using (var rd = new RijndaelManaged())
using (var rd = new RijndaelManaged { Key = _key })
{
rd.Key = _key;
rd.GenerateIV();
byte[] iv = rd.IV;
using (var cs = new CryptoStream(ms, rd.CreateEncryptor(), CryptoStreamMode.Write))
{
ms.Write(iv, 0, iv.Length); // write first 16 bytes IV, followed by encrypted message
ms.Write(rd.IV, 0, rd.IV.Length); // write first 16 bytes IV, followed by encrypted message
cs.Write(data, 0, data.Length);
cs.FlushFinalBlock();
}
iv = null;
}
encdata = ms.ToArray();
}
return encdata;
}
catch
{
return new byte[0];
}
finally
return encdata;
}
public static byte[] Encrypt(byte[] input, byte[] key)
{
if (key == null || key.Length == 0) throw new Exception("Key can not be empty.");
if (input == null || input.Length == 0) throw new ArgumentException("Input can not be empty.");
using (var md5 = new MD5CryptoServiceProvider())
{
encdata = null;
data = null;
key = md5.ComputeHash(key);
}
byte[] data = input, encdata = new byte[0];
try
{
using (var ms = new MemoryStream())
{
using (var rd = new RijndaelManaged { Key = key })
{
rd.GenerateIV();
using (var cs = new CryptoStream(ms, rd.CreateEncryptor(), CryptoStreamMode.Write))
{
ms.Write(rd.IV, 0, rd.IV.Length); // write first 16 bytes IV, followed by encrypted message
cs.Write(data, 0, data.Length);
}
}
encdata = ms.ToArray();
}
}
catch
{
}
return encdata;
}
public static string Decrypt(string input)
{
if (_key == null) throw new Exception("The key can not be null.");
byte[] data;
int i;
try
{
using (var ms = new MemoryStream(Convert.FromBase64String(input)))
{
using (var rd = new RijndaelManaged())
{
byte[] iv = new byte[IVLENGTH];
ms.Read(iv, 0, IVLENGTH); // read first 16 bytes for IV, followed by encrypted message
rd.IV = iv;
rd.Key = _key;
using (var cs = new CryptoStream(ms, rd.CreateDecryptor(), CryptoStreamMode.Read))
{
data = new byte[ms.Length - IVLENGTH + 1];
i = cs.Read(data, 0, data.Length);
}
iv = null;
}
}
return Encoding.UTF8.GetString(data, 0, i);
}
catch
{
return string.Empty;
}
finally
{
data = null;
}
return Encoding.UTF8.GetString(Decrypt(Convert.FromBase64String(input)));
}
public static byte[] Decrypt(byte[] input)
{
if (_key == null) throw new Exception("The key can not be null.");
if (_key == null || _key.Length == 0) throw new Exception("Key can not be empty.");
if (input == null || input.Length == 0) throw new ArgumentException("Input can not be empty.");
byte[] temp, data;
byte[] data = new byte[0];
try
{
using (var ms = new MemoryStream(input))
{
using (var rd = new RijndaelManaged())
using (var rd = new RijndaelManaged { Key = _key })
{
byte[] iv = new byte[IVLENGTH];
ms.Read(iv, 0, IVLENGTH); // read first 16 bytes for IV, followed by encrypted message
rd.IV = iv;
rd.Key = _key;
using (var cs = new CryptoStream(ms, rd.CreateDecryptor(), CryptoStreamMode.Read))
{
temp = new byte[ms.Length - IVLENGTH + 1];
int read = cs.Read(temp, 0, temp.Length);
data = new byte[read];
Buffer.BlockCopy(temp, 0, data, 0, read);
byte[] temp = new byte[ms.Length - IVLENGTH + 1];
data = new byte[cs.Read(temp, 0, temp.Length)];
Buffer.BlockCopy(temp, 0, data, 0, data.Length);
}
iv = null;
}
}
return data;
}
catch
{
return new byte[0];
}
finally
{
temp = null;
data = null;
}
return data;
}
}
}