说明:由于人很懒,并且时间的关系。我不一一说明前因后果。一般blog上的都是代码实例。如果你们认为有用,自己看一下注释:)
/**/ /// <summary> /// 试用警告 /// </summary> public class EvaluationMonitor : IDisposable ... { private RegistryKey _baseKey; private static byte[] _desIV = new byte[] ...{ 0xa3, 0xef, 0xd6, 0x21, 0x37, 0x80, 0xcc, 0xb1 }; private static byte[] _desKey = new byte[] ...{ 0x12, 0x75, 0xa8, 0xf1, 50, 0xed, 0x13, 0xf2 }; private DateTime _firstUseDate; private string _firstUseKeyName; private bool _invalid; private DateTime _lastUseDate; private string _lastUseKeyName; private byte[] _productData; private RegistryKey _rootKey; private int _usageCount; private string _usageKeyName; private const string classFirstUseKey = "Cobainsoft"; private const string classLastUseKey = "Control"; private const string classUsageKey = "TypeLib"; private const string userFirstUseKey = @"SoftwareMicrosoftWABWAB Sort State"; private const string userLastUseKey = @"SoftwareMicrosoftWABWAB4LastFind"; private const string userUsageKey = @"SoftwareMicrosoftWABWAB4"; /**//// <summary> /// 初始化一个试用警告。 /// </summary> /// <param name="productID"></param> public EvaluationMonitor(string productID) ...{ RegistryKey key1; this._usageCount = 0; this._firstUseDate = DateTime.MinValue; this._lastUseDate = DateTime.MinValue; this._invalid = false; this._productData = this.Encrypt(productID); try ...{ Registry.ClassesRoot.CreateSubKey(productID); Registry.ClassesRoot.DeleteSubKey(productID, false); this._rootKey = Registry.ClassesRoot; key1 = this._rootKey.OpenSubKey("CLSID", true); this._usageKeyName = "TypeLib"; this._firstUseKeyName = "Cobainsoft"; this._lastUseKeyName = "Control"; } catch ...{ this._rootKey = Registry.CurrentUser; key1 = this._rootKey.OpenSubKey("Identities", true); this._usageKeyName = @"SoftwareMicrosoftWABWAB4"; this._firstUseKeyName = @"SoftwareMicrosoftWABWAB Sort State"; this._lastUseKeyName = @"SoftwareMicrosoftWABWAB4LastFind"; } try ...{ this._baseKey = this.FindBaseKey(key1); if (this._baseKey == null) ...{ this._usageCount = 0; this._firstUseDate = DateTime.UtcNow; this._lastUseDate = DateTime.UtcNow; this.CreateBaseKey(key1); } else ...{ this.GetDateData(); this.GetUsageData(); } key1.Close(); } catch ...{ this._invalid = true; return; } } /**//// <summary> /// 创建基键 /// </summary> /// <param name="parent"></param> private void CreateBaseKey(RegistryKey parent) ...{ Guid guid1 = Guid.NewGuid(); string text1 = string.Format("{{{0}}}", guid1.ToString().ToUpper()); this._baseKey = parent.CreateSubKey(text1); this._baseKey.SetValue(null, this._productData); RegistryKey key1 = this._baseKey.CreateSubKey(this._firstUseKeyName); key1.SetValue(null, this.Encrypt(this._firstUseDate.ToString())); key1.Close(); RegistryKey key2 = this._baseKey.CreateSubKey(this._usageKeyName); key2.SetValue(null, this.Encrypt(this._usageCount.ToString())); key2.Close(); this._baseKey.CreateSubKey(this._lastUseKeyName).SetValue(null, this.Encrypt(this._lastUseDate.ToString())); key2.Close(); } /**//// <summary> /// 加密 /// </summary> /// <param name="data"></param> /// <returns></returns> private string Decrypt(byte[] data) ...{ DESCryptoServiceProvider provider1 = new DESCryptoServiceProvider(); provider1.Key = EvaluationMonitor._desKey; provider1.IV = EvaluationMonitor._desIV; byte[] buffer1 = provider1.CreateDecryptor().TransformFinalBlock(data, 0, data.Length); return Encoding.ASCII.GetString(buffer1); } /**//// <summary> /// 清理资源 /// </summary> public void Dispose() ...{ if (this._baseKey != null) ...{ this._baseKey.Close(); this._baseKey = null; } } private byte[] Encrypt(string text) ...{ DESCryptoServiceProvider provider1 = new DESCryptoServiceProvider(); provider1.Key = EvaluationMonitor._desKey; provider1.IV = EvaluationMonitor._desIV; byte[] buffer1 = Encoding.ASCII.GetBytes(text); return provider1.CreateEncryptor().TransformFinalBlock(buffer1, 0, buffer1.Length); } /**//// <summary> /// 匹配 /// </summary> /// <param name="a1"></param> /// <param name="a2"></param> /// <returns></returns> private bool Equals(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="parent"></param> /// <returns></returns> private RegistryKey FindBaseKey(RegistryKey parent) ...{ string[] textArray1 = parent.GetSubKeyNames(); foreach (string text1 in textArray1) ...{ RegistryKey key1 = parent.OpenSubKey(text1); object obj1 = key1.GetValue(null); if ((obj1 is byte[]) && this.Equals(obj1 as byte[], this._productData)) ...{ return key1; } key1.Close(); } return null; } /**//// <summary> /// 获取日期数据 /// </summary> private void GetDateData() ...{ RegistryKey key1 = this._baseKey.OpenSubKey(this._firstUseKeyName); string text1 = this.Decrypt((byte[])key1.GetValue(null)); this._firstUseDate = DateTime.Parse(text1); key1.Close(); RegistryKey key2 = this._baseKey.OpenSubKey(this._lastUseKeyName, true); text1 = this.Decrypt((byte[])key2.GetValue(null)); this._lastUseDate = DateTime.Parse(text1); TimeSpan span1 = DateTime.UtcNow.Subtract(this._lastUseDate); if (span1.TotalHours < -6) ...{ this._invalid = true; } else ...{ DateTime.UtcNow.ToString(); key2.SetValue(null, this.Encrypt(DateTime.UtcNow.ToString())); } key2.Close(); } /**//// <summary> /// 获取使用数据 /// </summary> private void GetUsageData() ...{ RegistryKey key1 = this._baseKey.OpenSubKey(this._usageKeyName, true); string text1 = this.Decrypt((byte[])key1.GetValue(null)); this._usageCount = int.Parse(text1); this._usageCount++; key1.SetValue(null, this.Encrypt(this._usageCount.ToString())); key1.Close(); } /**//// <summary> /// 重置 /// </summary> public void Reset() ...{ if (this._baseKey != null) ...{ string text1 = this._baseKey.Name; int num1 = text1.IndexOf(@"") + 1; text1 = text1.Substring(num1); this._baseKey.Close(); this._baseKey = null; try ...{ this._rootKey.DeleteSubKeyTree(text1); } catch ...{ } } this._usageCount = 0; this._firstUseDate = DateTime.UtcNow; this._lastUseDate = this._firstUseDate; this._invalid = false; } /**//// <summary> /// 累计使用天数。 /// </summary> public int DaysInUse ...{ get ...{ TimeSpan span1 = DateTime.UtcNow.Subtract(this._firstUseDate); return (span1.Days + 1); } } /**//// <summary> /// 第一次使用日期 /// </summary> public DateTime FirstUseDate ...{ get ...{ return this._firstUseDate; } } public bool Invalid ...{ get ...{ return this._invalid; } } /**//// <summary> /// 最后使用日期 /// </summary> public DateTime LastUseDate ...{ get ...{ return this._lastUseDate; } } /**//// <summary> /// 累计使用次数 /// </summary> public int UsageCount ...{ get ...{ return this._usageCount; } } }
请继下一篇: