保证一个软件系统只能在开发商授权的电脑上面运行,使用如下操作解决软件注册问题。
1.编写一个注册机(另外一套通过机器码生成注册码的小软件,由软件商保管)
1.1根据机器码生成注册码。
1 private void button1_Click(object sender, EventArgs e) 2 { 3 if (textBox1.Text == "") { 4 MessageBox.Show("输入机器码!"); 5 return; 6 } 7 if (textBox1.Text != "") { 8 textBox2.Text = getRNum(textBox1.Text); 9 } 10 } 11 public int[] intCode = new int[127];//存储密钥 12 public int[] intNumber = new int[25];//存机器码的Ascii值 13 public char[] Charcode = new char[25];//存储机器码字 14 public void setIntCode()//给数组赋值小于10的数 15 { 16 for (int i = 1; i < intCode.Length; i++) 17 { 18 intCode[i] = i % 9; 19 } 20 } 21 /// <summary> 22 /// 生成注册码 23 /// </summary> 24 /// <returns></returns> 25 public string getRNum(string PCNoStr) 26 { 27 setIntCode();//初始化127位数组 28 string MNum = PCNoStr;//获取注册码 EMKEMEMM333936G9Z6KUX7KK 29 for (int i = 1; i < Charcode.Length; i++)//把机器码存入数组中 30 { 31 Charcode[i] = Convert.ToChar(MNum.Substring(i - 1, 1)); 32 } 33 for (int j = 1; j < intNumber.Length; j++)//把字符的ASCII值存入一个整数组中。 34 { 35 intNumber[j] = intCode[Convert.ToInt32(Charcode[j])] + Convert.ToInt32(Charcode[j]); 36 } 37 string strAsciiName = "";//用于存储注册码 38 for (int j = 1; j < intNumber.Length; j++) 39 { 40 if (intNumber[j] >= 48 && intNumber[j] <= 57)//判断字符ASCII值是否0-9之间 41 { 42 strAsciiName += Convert.ToChar(intNumber[j]).ToString(); 43 } 44 else if (intNumber[j] >= 65 && intNumber[j] <= 90)//判断字符ASCII值是否A-Z之间 45 { 46 strAsciiName += Convert.ToChar(intNumber[j]).ToString(); 47 } 48 else if (intNumber[j] >= 97 && intNumber[j] <= 122)//判断字符ASCII值是否a-z之间 49 { 50 strAsciiName += Convert.ToChar(intNumber[j]).ToString(); 51 } 52 else//判断字符ASCII值不在以上范围内 53 { 54 if (intNumber[j] > 122)//判断字符ASCII值是否大于z 55 { 56 strAsciiName += Convert.ToChar(intNumber[j] - 10).ToString(); 57 } 58 else 59 { 60 strAsciiName += Convert.ToChar(intNumber[j] - 9).ToString(); 61 } 62 } 63 } 64 return strAsciiName;//返回注册码 65 }
1.2下面客户端系统运行前的界面。(Load事件将机器码填入文本框)
客户将该机器码发送给软件商要注册码。
1.3机器码的生成。
1 /////////////////// 2 /// <summary> 3 /// 取得设备硬盘的卷标号 4 /// </summary> 5 /// <returns></returns> 6 public string GetDiskVolumeSerialNumber() 7 { 8 ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT * FROM Win32_PhysicalMedia"); 9 String strHardDiskID = null; 10 foreach (ManagementObject mo in searcher.Get()) 11 { 12 strHardDiskID = mo["SerialNumber"].ToString().Trim(); break; 13 } 14 return strHardDiskID; 15 } 16 /// <summary> 17 /// 获得CPU的序列号 18 /// </summary> 19 /// <returns></returns> 20 public string getCpu() 21 { 22 string strCpu = null; 23 ManagementClass myCpu = new ManagementClass("win32_Processor"); 24 ManagementObjectCollection myCpuConnection = myCpu.GetInstances(); 25 foreach (ManagementObject myObject in myCpuConnection) 26 { 27 strCpu = myObject.Properties["Processorid"].Value.ToString(); 28 break; 29 } 30 return strCpu; 31 } 32 /// <summary> 33 /// 生成机器码 34 /// </summary> 35 /// <returns></returns> 36 public string getMNum() 37 { 38 string strNum = getCpu() + GetDiskVolumeSerialNumber();//获得24位Cpu和硬盘序列号 39 string strMNum = strNum.Substring(0, 24);//从生成的字符串中取出前24个字符做为机器码 40 return strMNum; 41 }
1.4对比检查机器码和注册号,写入注册表
1 /// <summary> 2 /// 写注册码 3 /// </summary> 4 /// <param name="keystr"></param> 5 /// <param name="value"></param> 6 public void Write_Registry(string keystr, string value) 7 { 8 RegistryKey key = Registry.LocalMachine; 9 //在HKEY_LOCAL_MACHINESOFTWARE下新建名为VangoCalibration的注册表项。如果已经存在则不影响! 10 RegistryKey software = key.CreateSubKey("software\Broadband_test_system"); 11 software.SetValue(keystr, value); 12 key.Close(); 13 }
1.5软件每次执行,读取注册表看是否注册
/// <summary> /// 读取注册码 /// </summary> /// <returns></returns> public bool Rade_Registry() { RegistryKey key = Registry.LocalMachine; //在HKEY_LOCAL_MACHINESOFTWARE下新建名为VangoCalibration的注册表项。如果已经存在则不影响! RegistryKey software = key.CreateSubKey("software\Broadband_test_system"); string pc_name = getMNum(); string v = software.GetValue(pc_name, "0").ToString(); key.Close(); string system_key = getRNum(getMNum()); if (v != system_key) { return false; } return true; }