接上一篇:Licensing实例(一)
/**/
/// <summary>
/// 加密许可证提供者
/// </summary>
public class EncryptedLicenseProvider : LicenseProvider
... {
private static byte[] _designSignature;
private static byte[] _desIV = new byte[] ...{ 173, 63, 198, 17, 71, 144, 221, 161 };
private static byte[] _desKey = new byte[] ...{ 146, 21, 56, 161, 18, 237, 179, 194 };
private static string _rsaParameters;
private static byte[] _runtimeSignature;
private static EncryptedLicense _systemLicense;
private const string _systemParameters = "";
private const int keyLength = 7;
public EncryptedLicenseProvider()
...{
}
/**//// <summary>
/// 数组匹配
/// </summary>
/// <param name="a1"></param>
/// <param name="a2"></param>
/// <returns></returns>
private static bool ArrayEqual(byte[] a1, byte[] a2)
...{
if (a1 != a2)
...{
if ((a1 == null) || (a2 == null))
...{
return false;
}
if (a1.Length != a2.Length)
...{
return false;
}
for (int num1 = 0; num1 < a1.Length; num1++)
...{
if (a1[num1] != a2[num1])
...{
return false;
}
}
}
return true;
}
/**//// <summary>
/// 序列尺寸
/// </summary>
/// <param name="length"></param>
/// <returns></returns>
private static int ArraySize(int length)
...{
return length;
}
/**//// <summary>
/// 提取许可证信息
/// </summary>
/// <param name="licenseKey">许可证键值</param>
/// <param name="password">密码</param>
/// <returns></returns>
internal EncryptedLicense ExtractLicense(string licenseKey, string password)
...{
try
...{
//从哈希转到byte
byte[] buffer1 = EncryptedLicenseProvider.FromHex(licenseKey);
byte[] buffer2 = new byte[] ...{ 62, 126, 142, 55, 68, 165, 193, 63 };
//获取公钥标识
byte[] buffer3 = Assembly.GetExecutingAssembly().GetName().GetPublicKeyToken();
if (!EncryptedLicenseProvider.ArrayEqual(buffer3, buffer2))
...{
EncryptedLicenseProvider._desKey.CopyTo(buffer2, 0);
}
DESCryptoServiceProvider provider1 = new DESCryptoServiceProvider();
provider1.Key = EncryptedLicenseProvider._desKey;
provider1.IV = EncryptedLicenseProvider._desIV;
byte[] buffer4 = provider1.CreateDecryptor().TransformFinalBlock(buffer1, 0, buffer1.Length);
byte[] buffer5 = new byte[EncryptedLicenseProvider.ArraySize(8)];
byte[] buffer6 = new byte[EncryptedLicenseProvider.ArraySize(buffer4.Length - 7)];
Array.Copy(buffer4, 0, buffer5, 0, 7);
Array.Copy(buffer4, 7, buffer6, 0, buffer6.Length);
byte[] buffer7 = EncryptedLicenseProvider.GetEncryptionKey(password);
if (!EncryptedLicenseProvider.ArrayEqual(buffer5, buffer7))
...{
return null;
}
provider1.IV = buffer5;
byte[] buffer8 = provider1.CreateDecryptor().TransformFinalBlock(buffer6, 0, buffer6.Length);
byte[] buffer9 = new byte[EncryptedLicenseProvider.ArraySize(buffer8.Length - 2)];
Array.Copy(buffer8, 2, buffer9, 0, buffer9.Length);
ushort num1 = BitConverter.ToUInt16(buffer8, 0);
string text1 = Encoding.UTF8.GetString(buffer9);
return new EncryptedLicense(licenseKey, num1, text1);
}
catch
...{
return null;
}
}
/**//// <summary>
/// 从哈希转到byte
/// </summary>
/// <param name="hex"></param>
/// <returns></returns>
private static byte[] FromHex(string hex)
...{
string text1 = EncryptedLicenseProvider.Strip(hex, " -");
if ((text1 == null) || ((text1.Length % 2) != 0))
...{
throw new FormatException("Invalid hexadecimal string");
}
byte[] buffer1 = new byte[EncryptedLicenseProvider.ArraySize(text1.Length / 2)];
int num1 = 0;
for (int num2 = 0; num1 < text1.Length; num2++)
...{
string text2 = text1.Substring(num1, 2);
buffer1[num2] = byte.Parse(text2, NumberStyles.HexNumber);
num1 += 2;
}
return buffer1;
/// 加密许可证提供者
/// </summary>
public class EncryptedLicenseProvider : LicenseProvider
... {
private static byte[] _designSignature;
private static byte[] _desIV = new byte[] ...{ 173, 63, 198, 17, 71, 144, 221, 161 };
private static byte[] _desKey = new byte[] ...{ 146, 21, 56, 161, 18, 237, 179, 194 };
private static string _rsaParameters;
private static byte[] _runtimeSignature;
private static EncryptedLicense _systemLicense;
private const string _systemParameters = "";
private const int keyLength = 7;
public EncryptedLicenseProvider()
...{
}
/**//// <summary>
/// 数组匹配
/// </summary>
/// <param name="a1"></param>
/// <param name="a2"></param>
/// <returns></returns>
private static bool ArrayEqual(byte[] a1, byte[] a2)
...{
if (a1 != a2)
...{
if ((a1 == null) || (a2 == null))
...{
return false;
}
if (a1.Length != a2.Length)
...{
return false;
}
for (int num1 = 0; num1 < a1.Length; num1++)
...{
if (a1[num1] != a2[num1])
...{
return false;
}
}
}
return true;
}
/**//// <summary>
/// 序列尺寸
/// </summary>
/// <param name="length"></param>
/// <returns></returns>
private static int ArraySize(int length)
...{
return length;
}
/**//// <summary>
/// 提取许可证信息
/// </summary>
/// <param name="licenseKey">许可证键值</param>
/// <param name="password">密码</param>
/// <returns></returns>
internal EncryptedLicense ExtractLicense(string licenseKey, string password)
...{
try
...{
//从哈希转到byte
byte[] buffer1 = EncryptedLicenseProvider.FromHex(licenseKey);
byte[] buffer2 = new byte[] ...{ 62, 126, 142, 55, 68, 165, 193, 63 };
//获取公钥标识
byte[] buffer3 = Assembly.GetExecutingAssembly().GetName().GetPublicKeyToken();
if (!EncryptedLicenseProvider.ArrayEqual(buffer3, buffer2))
...{
EncryptedLicenseProvider._desKey.CopyTo(buffer2, 0);
}
DESCryptoServiceProvider provider1 = new DESCryptoServiceProvider();
provider1.Key = EncryptedLicenseProvider._desKey;
provider1.IV = EncryptedLicenseProvider._desIV;
byte[] buffer4 = provider1.CreateDecryptor().TransformFinalBlock(buffer1, 0, buffer1.Length);
byte[] buffer5 = new byte[EncryptedLicenseProvider.ArraySize(8)];
byte[] buffer6 = new byte[EncryptedLicenseProvider.ArraySize(buffer4.Length - 7)];
Array.Copy(buffer4, 0, buffer5, 0, 7);
Array.Copy(buffer4, 7, buffer6, 0, buffer6.Length);
byte[] buffer7 = EncryptedLicenseProvider.GetEncryptionKey(password);
if (!EncryptedLicenseProvider.ArrayEqual(buffer5, buffer7))
...{
return null;
}
provider1.IV = buffer5;
byte[] buffer8 = provider1.CreateDecryptor().TransformFinalBlock(buffer6, 0, buffer6.Length);
byte[] buffer9 = new byte[EncryptedLicenseProvider.ArraySize(buffer8.Length - 2)];
Array.Copy(buffer8, 2, buffer9, 0, buffer9.Length);
ushort num1 = BitConverter.ToUInt16(buffer8, 0);
string text1 = Encoding.UTF8.GetString(buffer9);
return new EncryptedLicense(licenseKey, num1, text1);
}
catch
...{
return null;
}
}
/**//// <summary>
/// 从哈希转到byte
/// </summary>
/// <param name="hex"></param>
/// <returns></returns>
private static byte[] FromHex(string hex)
...{
string text1 = EncryptedLicenseProvider.Strip(hex, " -");
if ((text1 == null) || ((text1.Length % 2) != 0))
...{
throw new FormatException("Invalid hexadecimal string");
}
byte[] buffer1 = new byte[EncryptedLicenseProvider.ArraySize(text1.Length / 2)];
int num1 = 0;
for (int num2 = 0; num1 < text1.Length; num2++)
...{
string text2 = text1.Substring(num1, 2);
buffer1[num2] = byte.Parse(text2, NumberStyles.HexNumber);
num1 += 2;
}
return buffer1;