最近做了一个绿色免安装软件,领导临时要求加个注册机制,不能让现场工程师随意复制。事出突然,只能在现场开发(离开现场软件就不受我们控了)。花了不到两个小时实现了简单的注册机制,稍作整理。 基本原理:1.软件一运行就把计算机的CPU、主板、BIOS、MAC地址记录下来,然后加密(key=key1)生成文件;2.注册机将该文件内容MD5加密后再进行一次加密(key=key2)保存成注册文件;3.注册验证的逻辑,计算机信息加密后(key=key1)加密md5==注册文件解密(key=key2);另外,采用ConfuserEx将可执行文件加密;这样别人要破解也就需要点力气了(没打算防破解,本意只想防复制的),有能力破解的人也不在乎破解这个软件了(开发这个软件前后只花了一周时间而已); 技术上主要三个模块:1.获取电脑相关硬件信息(可参考);2.加密解密;3.读写文件;
1.获取电脑相关硬件信息代码:
[csharp] view plain copypublic class ComputerInfo { public static string GetComputerInfo() { string info = string.Empty; string cpu = GetCPUInfo(); string baseBoard = GetBaseBoardInfo(); string bios = GetBIOSInfo(); string mac = GetMACInfo(); info = string.Concat(cpu, baseBoard, bios, mac); return info; } private static string GetCPUInfo() { string info = string.Empty; info = GetHardWareInfo("Win32_Processor", "ProcessorId"); return info; } private static string GetBIOSInfo() { string info = string.Empty; info = GetHardWareInfo("Win32_BIOS", "SerialNumber"); return info; } private static string GetBaseBoardInfo() { string info = string.Empty; info = GetHardWareInfo("Win32_BaseBoard", "SerialNumber"); return info; } private static string GetMACInfo() { string info = string.Empty; info = GetHardWareInfo("Win32_BaseBoard", "SerialNumber"); return info; } private static string GetHardWareInfo(string typePath, string key) { try { ManagementClass managementClass = new ManagementClass(typePath); ManagementObjectCollection mn = managementClass.GetInstances(); PropertyDataCollection properties = managementClass.Properties; foreach (PropertyData property in properties) { if (property.Name == key) { foreach (ManagementObject m in mn) { return m.Properties[property.Name].Value.ToString(); } } } } catch (Exception ex) { //这里写异常的处理 } return string.Empty; } private static string GetMacAddressByNetworkInformation() { string key = "SYSTEM\\CurrentControlSet\\Control\\Network\\{4D36E972-E325-11CE-BFC1-08002BE10318}\\"; string macAddress = string.Empty; try { NetworkInterface[] nics = NetworkInterface.GetAllNetworkInterfaces(); foreach (NetworkInterface adapter in nics) { if (adapter.NetworkInterfaceType == NetworkInterfaceType.Ethernet && adapter.GetPhysicalAddress().ToString().Length != 0) { string fRegistryKey = key + adapter.Id + "\\Connection"; RegistryKey rk = Registry.LocalMachine.OpenSubKey(fRegistryKey, false); if (rk != null) { string fPnpInstanceID = rk.GetValue("PnpInstanceID", "").ToString(); int fMediaSubType = Convert.ToInt32(rk.GetValue("MediaSubType", 0)); if (fPnpInstanceID.Length > 3 && fPnpInstanceID.Substring(0, 3) == "PCI") { macAddress = adapter.GetPhysicalAddress().ToString(); for (int i = 1; i |