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 }