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
|
|
|
|
{
|
|
|
|
|
class AES
|
|
|
|
|
{
|
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)
|
|
|
|
|
{
|
2014-08-13 20:59:57 +00:00
|
|
|
|
byte[] key, data = System.Text.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;
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return System.Text.Encoding.UTF8.GetString(data, 0, i);
|
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
|
{
|
|
|
|
|
byte[] key, data;
|
|
|
|
|
int i;
|
|
|
|
|
|
|
|
|
|
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))
|
|
|
|
|
{
|
|
|
|
|
data = new byte[ms.Length - IVLENGTH + 1];
|
|
|
|
|
i = cs.Read(data, 0, data.Length);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
iv = null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return data;
|
|
|
|
|
}
|
|
|
|
|
catch
|
|
|
|
|
{
|
|
|
|
|
return new byte[0];
|
|
|
|
|
}
|
|
|
|
|
finally
|
|
|
|
|
{
|
|
|
|
|
data = null;
|
|
|
|
|
key = null;
|
|
|
|
|
}
|
2014-07-08 12:58:53 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|