2014-07-08 12:58:53 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Security.Cryptography;
|
|
|
|
|
using System.Text;
|
|
|
|
|
|
2015-01-13 18:29:11 +00:00
|
|
|
|
namespace xServer.Core.Encryption
|
2014-07-08 12:58:53 +00:00
|
|
|
|
{
|
2015-01-13 18:43:55 +00:00
|
|
|
|
public static class AES
|
2014-07-08 12:58:53 +00:00
|
|
|
|
{
|
2014-08-13 20:59:57 +00:00
|
|
|
|
private const int IVLENGTH = 16;
|
|
|
|
|
|
2014-07-08 12:58:53 +00:00
|
|
|
|
public static string Encrypt(string input, string keyy)
|
|
|
|
|
{
|
2015-03-27 10:13:02 +00:00
|
|
|
|
byte[] key, data = Encoding.UTF8.GetBytes(input), encdata;
|
2014-08-13 20:59:57 +00:00
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
key = null;
|
|
|
|
|
}
|
|
|
|
|
}
|
2014-07-08 12:58:53 +00:00
|
|
|
|
|
2014-08-13 20:59:57 +00:00
|
|
|
|
public static byte[] Encrypt(byte[] input, byte[] keyy)
|
|
|
|
|
{
|
|
|
|
|
byte[] key, data = input, encdata;
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
using (var md5 = new MD5CryptoServiceProvider())
|
|
|
|
|
{
|
|
|
|
|
key = md5.ComputeHash(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 encdata;
|
|
|
|
|
}
|
|
|
|
|
catch
|
|
|
|
|
{
|
|
|
|
|
return new byte[0];
|
|
|
|
|
}
|
|
|
|
|
finally
|
|
|
|
|
{
|
|
|
|
|
encdata = null;
|
|
|
|
|
data = null;
|
|
|
|
|
key = null;
|
|
|
|
|
}
|
2014-07-08 12:58:53 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static string Decrypt(string input, string keyy)
|
|
|
|
|
{
|
2014-08-13 20:59:57 +00:00
|
|
|
|
byte[] key, data;
|
|
|
|
|
int i;
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
using (var md5 = new MD5CryptoServiceProvider())
|
|
|
|
|
{
|
|
|
|
|
key = md5.ComputeHash(Encoding.UTF8.GetBytes(keyy));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-03-27 10:13:02 +00:00
|
|
|
|
return Encoding.UTF8.GetString(data, 0, i);
|
2014-08-13 20:59:57 +00:00
|
|
|
|
}
|
|
|
|
|
catch
|
|
|
|
|
{
|
|
|
|
|
return string.Empty;
|
|
|
|
|
}
|
|
|
|
|
finally
|
|
|
|
|
{
|
|
|
|
|
data = null;
|
|
|
|
|
key = null;
|
|
|
|
|
}
|
|
|
|
|
}
|
2014-07-08 12:58:53 +00:00
|
|
|
|
|
2014-08-13 20:59:57 +00:00
|
|
|
|
public static byte[] Decrypt(byte[] input, byte[] keyy)
|
|
|
|
|
{
|
2015-05-14 09:06:17 +00:00
|
|
|
|
byte[] key, temp, data;
|
2014-08-13 20:59:57 +00:00
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
using (var md5 = new MD5CryptoServiceProvider())
|
|
|
|
|
{
|
|
|
|
|
key = md5.ComputeHash(keyy);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
using (var ms = new MemoryStream(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))
|
|
|
|
|
{
|
2015-05-14 09:06:17 +00:00
|
|
|
|
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);
|
2014-08-13 20:59:57 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
iv = null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return data;
|
|
|
|
|
}
|
|
|
|
|
catch
|
|
|
|
|
{
|
|
|
|
|
return new byte[0];
|
|
|
|
|
}
|
|
|
|
|
finally
|
|
|
|
|
{
|
2015-05-14 09:06:17 +00:00
|
|
|
|
temp = null;
|
2014-08-13 20:59:57 +00:00
|
|
|
|
data = null;
|
|
|
|
|
key = null;
|
|
|
|
|
}
|
2014-07-08 12:58:53 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2015-04-21 18:27:52 +00:00
|
|
|
|
}
|