軟件註冊
阿新 • • 發佈:2017-08-08
cts war arc urn ctc from code process image
保證一個軟件系統只能在開發商授權的電腦上面運行,使用如下操作解決軟件註冊問題。
1.編寫一個註冊機(另外一套通過機器碼生成註冊碼的小軟件,由軟件商保管)
1.1根據機器碼生成註冊碼。
1 private void button1_Click(object sender, EventArgs e) 2 { 3 if (textBox1.Text == "") { 4 MessageBox.Show("輸入機器碼!"); 5 return; 6 } 7View Codeif (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 publicvoid 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 }View Code
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_MACHINE\SOFTWARE下新建名為VangoCalibration的註冊表項。如果已經存在則不影響! 10 RegistryKey software = key.CreateSubKey("software\\Broadband_test_system"); 11 software.SetValue(keystr, value); 12 key.Close(); 13 }View Code
1.5軟件每次執行,讀取註冊表看是否註冊
/// <summary> /// 讀取註冊碼 /// </summary> /// <returns></returns> public bool Rade_Registry() { RegistryKey key = Registry.LocalMachine; //在HKEY_LOCAL_MACHINE\SOFTWARE下新建名為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; }
軟件註冊