1 class RegisterClass 2 { 3 //步驟一: 獲得CUP序列號和硬盤序列號的實現代碼如下: 4 //獲得CPU的序列號 5 bool Stupids = true; 6 bool Cat = false; 7 public string getCpu() 8 { 9 string strCpu = null; 10 ManagementClass myCpu = new ManagementClass("win32_Processor"); 11 ManagementObjectCollection myCpuConnection = myCpu.GetInstances(); 12 foreach (ManagementObject myObject in myCpuConnection) 13 { 14 strCpu = myObject.Properties["Processorid"].Value.ToString(); 15 break; 16 } 17 return strCpu; 18 } 19 20 //取得設備硬盤的卷標號 21 public string GetDiskVolumeSerialNumber() 22 { 23 ManagementClass mc = 24 new ManagementClass("Win32_NetworkAdapterConfiguration"); 25 ManagementObject disk = 26 new ManagementObject("win32_logicaldisk.deviceid=\"c:\""); 27 disk.Get(); 28 return disk.GetPropertyValue("VolumeSerialNumber").ToString(); 29 } 30 31 //步驟二: 收集硬件信息生成機器碼, 代碼如下: 32 //生成機器碼 33 public string CreateCode() 34 { 35 string temp = getCpu() + GetDiskVolumeSerialNumber();//獲得24位Cpu和硬盤序列號 36 string[] strid = new string[24];// 37 for (int i = 0; i < 24; i++)//把字符賦給數組 38 { 39 strid[i] = temp.Substring(i, 1); 40 } 41 temp = ""; 42 //Random rdid = new Random(); 43 for (int i = 0; i < 24; i++)//從數組隨機抽取24個字符組成新的字符生成機器三 44 { 45 //temp += strid[rdid.Next(0, 24)]; 46 temp += strid[i + 3 >= 24 ? 0 : i + 3]; 47 } 48 return GetMd5(temp); 49 } 50 51 52 //步驟三: 使用機器碼生成軟件注冊碼, 代碼如下: 53 //使用機器碼生成注冊碼 54 public int[] intCode = new int[127];//用於存密鑰 55 public void setIntCode()//給數組賦值個小於10的隨機數 56 { 57 //Random ra = new Random(); 58 //for (int i = 1; i < intCode.Length;i++ ) 59 //{ 60 // intCode[i] = ra.Next(0, 9); 61 //} 62 for (int i = 1; i < intCode.Length; i++) 63 { 64 intCode[i] = i + 3 > 9 ? 0 : i + 3; 65 } 66 } 67 public int[] intNumber = new int[25];//用於存機器碼的Ascii值 68 public char[] Charcode = new char[25];//存儲機器碼字 69 70 //生成注冊碼 71 public string GetCode(string code) 72 { 73 if (code != "") 74 { 75 //把機器碼存入數組中 76 setIntCode();//初始化127位數組 77 for (int i = 1; i < Charcode.Length; i++)//把機器碼存入數組中 78 { 79 Charcode[i] = Convert.ToChar(code.Substring(i - 1, 1)); 80 }// 81 for (int j = 1; j < intNumber.Length; j++)//把字符的ASCII值存入一個整數組中。 82 { 83 intNumber[j] =intCode[Convert.ToInt32(Charcode[j])] +Convert.ToInt32(Charcode[j]); 84 } 85 string strAsciiName = null;//用於存儲機器碼 86 for (int j = 1; j < intNumber.Length; j++) 87 { 88 //MessageBox.Show((Convert.ToChar(intNumber[j])).ToString()); 89 //判斷字符ASCII值是否0-9之間 90 if (intNumber[j] >= 48 && intNumber[j] <= 57) 91 { 92 strAsciiName += Convert.ToChar(intNumber[j]).ToString(); 93 } 94 //判斷字符ASCII值是否A-Z之間 95 else if (intNumber[j] >= 65 && intNumber[j] <= 90) 96 { 97 strAsciiName += Convert.ToChar(intNumber[j]).ToString(); 98 } 99 //判斷字符ASCII值是否a-z之間 100 else if (intNumber[j] >= 97 && intNumber[j] <= 122) 101 { 102 strAsciiName += Convert.ToChar(intNumber[j]).ToString(); 103 } 104 else//判斷字符ASCII值不在以上范圍內 105 { 106 if (intNumber[j] > 122)//判斷字符ASCII值是否大於z 107 { 108 strAsciiName += Convert.ToChar(intNumber[j] - 10).ToString(); 109 } 110 else 111 { 112 strAsciiName += Convert.ToChar(intNumber[j] - 9).ToString(); 113 } 114 } 115 //label3.Text = strAsciiName;//得到注冊碼 116 } 117 return strAsciiName; 118 } 119 else 120 { 121 return ""; 122 } 123 } 124 125 //步驟四: 用戶輸入注冊碼注冊軟件, 演示代碼如下: 126 //注冊 127 public bool RegistIt(string currentCode, string realCode) 128 { 129 if (realCode != "") 130 { 131 if (currentCode.TrimEnd().Equals(realCode.TrimEnd())) 132 { 133 Microsoft.Win32.RegistryKey retkey = 134 Microsoft.Win32.Registry.CurrentUser. 135 OpenSubKey("software", true).CreateSubKey("StupidsCat"). 136 CreateSubKey("StupidsCat.ini"). 137 CreateSubKey(currentCode.TrimEnd()); 138 retkey.SetValue("StupidsCat", "BBC6D58D0953F027760A046D58D52786"); 139 140 141 retkey = Microsoft.Win32.Registry.LocalMachine. 142 OpenSubKey("software", true).CreateSubKey("StupidsCat"). 143 CreateSubKey("StupidsCat.ini"). 144 CreateSubKey(currentCode.TrimEnd()); 145 retkey.SetValue("StupidsCat", "BBC6D58D0953F027760A046D58D52786"); 146 147 148 return Stupids; 149 } 150 else 151 { 152 return Cat; 153 } 154 } 155 else { return Cat; } 156 } 157 158 159 public bool BoolRegist(string sn) 160 { 161 string[] keynames; bool flag = false; 162 Microsoft.Win32.RegistryKey localRegKey = Microsoft.Win32.Registry.LocalMachine; 163 Microsoft.Win32.RegistryKey userRegKey = Microsoft.Win32.Registry.CurrentUser; 164 try 165 { 166 keynames = localRegKey.OpenSubKey("software\\StupidsCat\\StupidsCat.ini\\" + GetMd5(sn)).GetValueNames(); 167 foreach (string name in keynames) 168 { 169 if (name == "StupidsCat") 170 { 171 if (localRegKey.OpenSubKey("software\\StupidsCat\\StupidsCat.ini\\" + GetMd5(sn)).GetValue("StupidsCat").ToString() == "BBC6D58D0953F027760A046D58D52786") 172 flag = true; 173 } 174 } 175 keynames = userRegKey.OpenSubKey("software\\StupidsCat\\StupidsCat.ini\\" + GetMd5(sn)).GetValueNames(); 176 foreach (string name in keynames) 177 { 178 if (name == "StupidsCat") 179 { 180 if (flag && userRegKey.OpenSubKey("software\\StupidsCat\\StupidsCat.ini\\" + GetMd5(sn)).GetValue("StupidsCat").ToString() == "BBC6D58D0953F027760A046D58D52786") 181 return true; 182 } 183 } 184 return false; 185 } 186 catch 187 { 188 return false; 189 } 190 finally 191 { 192 localRegKey.Close(); 193 userRegKey.Close(); 194 } 195 } 196 197 198 public string GetMd5(object text) 199 { 200 string path = text.ToString(); 201 202 MD5CryptoServiceProvider MD5Pro = new MD5CryptoServiceProvider(); 203 Byte[] buffer = Encoding.GetEncoding("utf-8").GetBytes(text.ToString()); 204 Byte[] byteResult = MD5Pro.ComputeHash(buffer); 205 206 string md5result = BitConverter.ToString(byteResult).Replace("-", ""); 207 return md5result; 208 } 209 }