257 lines
9.7 KiB
C#
257 lines
9.7 KiB
C#
namespace LSServerService
|
|
{
|
|
using System;
|
|
using System.IO;
|
|
using System.Runtime.InteropServices;
|
|
using System.Security.Cryptography;
|
|
using System.Text;
|
|
|
|
public static class GLEncrypt
|
|
{
|
|
private static byte[] Aes_Key;
|
|
private static byte[] Aes_Vector;
|
|
private static byte[] Des_Key;
|
|
private static byte[] Des_Vector;
|
|
|
|
public static string AESDecrypt(string DeString)
|
|
{
|
|
byte[] bytes = AESDecrypt(Convert.FromBase64String(DeString), Aes_Key, Aes_Vector);
|
|
return Encoding.Default.GetString(bytes);
|
|
}
|
|
|
|
public static string AESDecrypt(string DeString, string Key)
|
|
{
|
|
byte[] bytes = AESDecrypt(Convert.FromBase64String(DeString), Key, "�S\x000e�L�\x00154Zf�-y9 �");
|
|
return Encoding.Default.GetString(bytes);
|
|
}
|
|
|
|
public static string AESDecrypt(string DeString, byte[] bKey, byte[] bVector)
|
|
{
|
|
byte[] bytes = AESDecrypt(Convert.FromBase64String(DeString), bKey, bVector);
|
|
return Encoding.Default.GetString(bytes);
|
|
}
|
|
|
|
public static byte[] AESDecrypt(byte[] Data, string Key, string Vector)
|
|
{
|
|
byte[] destinationArray = new byte[32];
|
|
Array.Copy(Encoding.UTF8.GetBytes(Key.PadRight(destinationArray.Length)), destinationArray, destinationArray.Length);
|
|
byte[] buffer2 = new byte[16];
|
|
Array.Copy(Encoding.UTF8.GetBytes(Vector.PadRight(buffer2.Length)), buffer2, buffer2.Length);
|
|
return AESDecrypt(Data, destinationArray, buffer2);
|
|
}
|
|
|
|
public static byte[] AESDecrypt(byte[] Data, byte[] bKey, byte[] bVector)
|
|
{
|
|
byte[] buffer = null;
|
|
Rijndael rijndael = Rijndael.Create();
|
|
try
|
|
{
|
|
using (MemoryStream stream = new MemoryStream(Data))
|
|
{
|
|
using (CryptoStream stream2 = new CryptoStream(stream, rijndael.CreateDecryptor(bKey, bVector), CryptoStreamMode.Read))
|
|
{
|
|
using (MemoryStream stream3 = new MemoryStream())
|
|
{
|
|
byte[] buffer2 = new byte[1024];
|
|
int count = 0;
|
|
while ((count = stream2.Read(buffer2, 0, buffer2.Length)) > 0)
|
|
{
|
|
stream3.Write(buffer2, 0, count);
|
|
}
|
|
return stream3.ToArray();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
catch
|
|
{
|
|
buffer = null;
|
|
}
|
|
return buffer;
|
|
}
|
|
|
|
public static string AESEncrypt(string EnString)
|
|
{
|
|
return Convert.ToBase64String(AESEncrypt(Encoding.Default.GetBytes(EnString), Aes_Key, Aes_Vector));
|
|
}
|
|
|
|
public static string AESEncrypt(string EnString, string Key)
|
|
{
|
|
return Convert.ToBase64String(AESEncrypt(Encoding.Default.GetBytes(EnString), Key, "�S\x000e�L�\x00154Zf�-y9 �"));
|
|
}
|
|
|
|
public static string AESEncrypt(string EnString, byte[] bKey, byte[] bVector)
|
|
{
|
|
return Convert.ToBase64String(AESEncrypt(Encoding.Default.GetBytes(EnString), bKey, bVector));
|
|
}
|
|
|
|
public static byte[] AESEncrypt(byte[] Data, string Key, string Vector)
|
|
{
|
|
byte[] destinationArray = new byte[32];
|
|
Array.Copy(Encoding.UTF8.GetBytes(Key.PadRight(destinationArray.Length)), destinationArray, destinationArray.Length);
|
|
byte[] buffer2 = new byte[16];
|
|
Array.Copy(Encoding.UTF8.GetBytes(Vector.PadRight(buffer2.Length)), buffer2, buffer2.Length);
|
|
return AESEncrypt(Data, destinationArray, buffer2);
|
|
}
|
|
|
|
public static byte[] AESEncrypt(byte[] Data, byte[] bKey, byte[] bVector)
|
|
{
|
|
byte[] buffer = null;
|
|
Rijndael rijndael = Rijndael.Create();
|
|
try
|
|
{
|
|
using (MemoryStream stream = new MemoryStream())
|
|
{
|
|
using (CryptoStream stream2 = new CryptoStream(stream, rijndael.CreateEncryptor(bKey, bVector), CryptoStreamMode.Write))
|
|
{
|
|
stream2.Write(Data, 0, Data.Length);
|
|
stream2.FlushFinalBlock();
|
|
return stream.ToArray();
|
|
}
|
|
}
|
|
}
|
|
catch
|
|
{
|
|
buffer = null;
|
|
}
|
|
return buffer;
|
|
}
|
|
|
|
public static void CreateAesKeyVector(out byte[] Key, out byte[] Vector)
|
|
{
|
|
Rijndael rijndael = Rijndael.Create();
|
|
rijndael.GenerateKey();
|
|
rijndael.GenerateIV();
|
|
Key = rijndael.Key;
|
|
Vector = rijndael.IV;
|
|
rijndael = null;
|
|
}
|
|
|
|
public static void CreateDesKeyVector(out byte[] Key, out byte[] Vector)
|
|
{
|
|
DESCryptoServiceProvider provider = new DESCryptoServiceProvider();
|
|
provider.GenerateKey();
|
|
provider.GenerateIV();
|
|
Key = provider.Key;
|
|
Vector = provider.IV;
|
|
provider = null;
|
|
}
|
|
|
|
public static void CreateKeyVector()
|
|
{
|
|
DESCryptoServiceProvider provider = new DESCryptoServiceProvider();
|
|
provider.GenerateKey();
|
|
provider.GenerateIV();
|
|
Des_Key = provider.Key;
|
|
Des_Vector = provider.IV;
|
|
provider = null;
|
|
Rijndael rijndael = Rijndael.Create();
|
|
rijndael.GenerateKey();
|
|
rijndael.GenerateIV();
|
|
Aes_Key = rijndael.Key;
|
|
Aes_Vector = rijndael.IV;
|
|
rijndael = null;
|
|
}
|
|
|
|
public static string DESDecrypt(string DeString)
|
|
{
|
|
byte[] bytes = DESDecrypt(Convert.FromBase64String(DeString), Des_Key, Des_Vector);
|
|
return Encoding.Default.GetString(bytes);
|
|
}
|
|
|
|
public static string DESDecrypt(string DeString, string Key)
|
|
{
|
|
byte[] bytes = DESDecrypt(Convert.FromBase64String(DeString), Key, "�ޠj$6\x000fH");
|
|
return Encoding.Default.GetString(bytes);
|
|
}
|
|
|
|
public static byte[] DESDecrypt(byte[] Data, string Key, string Vector)
|
|
{
|
|
byte[] destinationArray = new byte[8];
|
|
Array.Copy(Encoding.UTF8.GetBytes(Key.PadRight(destinationArray.Length)), destinationArray, destinationArray.Length);
|
|
byte[] buffer2 = new byte[8];
|
|
Array.Copy(Encoding.UTF8.GetBytes(Vector.PadRight(buffer2.Length)), buffer2, buffer2.Length);
|
|
return DESDecrypt(Data, destinationArray, buffer2);
|
|
}
|
|
|
|
public static byte[] DESDecrypt(byte[] Data, byte[] bKey, byte[] bVector)
|
|
{
|
|
byte[] buffer = null;
|
|
DESCryptoServiceProvider provider = new DESCryptoServiceProvider {
|
|
Mode = CipherMode.CBC,
|
|
Padding = PaddingMode.Zeros
|
|
};
|
|
try
|
|
{
|
|
using (MemoryStream stream = new MemoryStream(Data))
|
|
{
|
|
using (CryptoStream stream2 = new CryptoStream(stream, provider.CreateDecryptor(bKey, bVector), CryptoStreamMode.Read))
|
|
{
|
|
using (MemoryStream stream3 = new MemoryStream())
|
|
{
|
|
byte[] buffer2 = new byte[1024];
|
|
int count = 0;
|
|
while ((count = stream2.Read(buffer2, 0, buffer2.Length)) > 0)
|
|
{
|
|
stream3.Write(buffer2, 0, count);
|
|
}
|
|
return stream3.ToArray();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
catch
|
|
{
|
|
buffer = null;
|
|
}
|
|
return buffer;
|
|
}
|
|
|
|
public static string DESEncrypt(string EnString)
|
|
{
|
|
return Convert.ToBase64String(DESEncrypt(Encoding.Default.GetBytes(EnString), Des_Key, Des_Vector));
|
|
}
|
|
|
|
public static string DESEncrypt(string EnString, string Key)
|
|
{
|
|
return Convert.ToBase64String(DESEncrypt(Encoding.Default.GetBytes(EnString), Key, "�ޠj$6\x000fH"));
|
|
}
|
|
|
|
public static byte[] DESEncrypt(byte[] Data, string Key, string Vector)
|
|
{
|
|
byte[] destinationArray = new byte[8];
|
|
Array.Copy(Encoding.UTF8.GetBytes(Key.PadRight(destinationArray.Length)), destinationArray, destinationArray.Length);
|
|
byte[] buffer2 = new byte[8];
|
|
Array.Copy(Encoding.UTF8.GetBytes(Vector.PadRight(buffer2.Length)), buffer2, buffer2.Length);
|
|
return DESEncrypt(Data, destinationArray, buffer2);
|
|
}
|
|
|
|
public static byte[] DESEncrypt(byte[] Data, byte[] bKey, byte[] bVector)
|
|
{
|
|
byte[] buffer = null;
|
|
DESCryptoServiceProvider provider = new DESCryptoServiceProvider {
|
|
Mode = CipherMode.CBC,
|
|
Padding = PaddingMode.Zeros
|
|
};
|
|
try
|
|
{
|
|
using (MemoryStream stream = new MemoryStream())
|
|
{
|
|
using (CryptoStream stream2 = new CryptoStream(stream, provider.CreateEncryptor(bKey, bVector), CryptoStreamMode.Write))
|
|
{
|
|
stream2.Write(Data, 0, Data.Length);
|
|
stream2.FlushFinalBlock();
|
|
return stream.ToArray();
|
|
}
|
|
}
|
|
}
|
|
catch
|
|
{
|
|
buffer = null;
|
|
}
|
|
return buffer;
|
|
}
|
|
}
|
|
}
|
|
|