相信很多初学编程的人都会对这个软件注册功能很感兴趣,我也不例外,刚学asp.net时,竞找不到这方面的实例,结果自己参考微软的一些文档自己做了一个,其实我做的这个注册功能很简单,读取计算机的CPU序列号,用MD5加密(经过处理)生成一个机器码,然后再把这个机器码处理一下就生成了注册码,在asp.net的配置文件web.config中加了一个键,用于存储注册码,程序运行时读取机器码,然后再生成注册码,对照一下配置文件内的注册码,如果一致就说明程序已注册,否则未注册。同时也做了一个注册机,用于生成注册码。注册机运行结果:
下面是代码:
1、首先是读取机器序列号的类Hardware,网上找来的
1
using
System;
2
using
System.Runtime.InteropServices;
3
using
System.Management;
4
namespace
Hardware
5
{
6
///
<summary>
7
///
Hardware_Mac 的摘要说明。
8
///
</summary>
9
public
class
HardwareInfo
10
{
11
//
取机器名
12
public
string
GetHostName()
13
{
14
return
System.Net.Dns.GetHostName();
15
}
16
17
//
取CPU编号
18
public
String GetCpuID()
19
{
20
try
21
{
22
ManagementClass mc
=
new
ManagementClass(
"
Win32_Processor
"
);
23
ManagementObjectCollection moc
=
mc.GetInstances();
24
25
String strCpuID
=
null
;
26
foreach
(ManagementObject mo
in
moc)
27
{
28
strCpuID
=
mo.Properties[
"
ProcessorId
"
].Value.ToString();
29
break
;
30
}
31
return
strCpuID;
32
}
33
catch
34
{
35
return
""
;
36
}
37
38
}
//
end method
39
40
//
取第一块硬盘编号
41
public
String GetHardDiskID()
42
{
43
try
44
{
45
ManagementObjectSearcher searcher
=
new
ManagementObjectSearcher(
"
SELECT * FROM Win32_PhysicalMedia
"
);
46
String strHardDiskID
=
null
;
47
foreach
(ManagementObject mo
in
searcher.Get())
48
{
49
strHardDiskID
=
mo[
"
SerialNumber
"
].ToString().Trim();
50
break
;
51
}
52
return
strHardDiskID;
53
}
54
catch
55
{
56
return
""
;
57
}
58
}
//
end
59
60
public
enum
NCBCONST
61
{
62
NCBNAMSZ
=
16
,
/*
absolute length of a net name
*/
63
MAX_LANA
=
254
,
/*
lana's in range 0 to MAX_LANA inclusive
*/
64
NCBENUM
=
0x37
,
/*
NCB ENUMERATE LANA NUMBERS
*/
65
NRC_GOODRET
=
0x00
,
/*
good return
*/
66
NCBRESET
=
0x32
,
/*
NCB RESET
*/
67
NCBASTAT
=
0x33
,
/*
NCB ADAPTER STATUS
*/
68
NUM_NAMEBUF
=
30
,
/*
Number of NAME's BUFFER
*/
69
}
70
71
[StructLayout(LayoutKind.Sequential)]
72
public
struct
ADAPTER_STATUS
73
{
74
[MarshalAs(UnmanagedType.ByValArray, SizeConst
=
6
)]
75
public
byte
[] adapter_address;
76
public
byte
rev_major;
77
public
byte
reserved0;
78
public
byte
adapter_type;
79
public
byte
rev_minor;
80
public
ushort
duration;
81
public
ushort
frmr_recv;
82
public
ushort
frmr_xmit;
83
public
ushort
iframe_recv_err;
84
public
ushort
xmit_aborts;
85
public
uint
xmit_success;
86
public
uint
recv_success;
87
public
ushort
iframe_xmit_err;
88
public
ushort
recv_buff_unavail;
89
public
ushort
t1_timeouts;
90
public
ushort
ti_timeouts;
91
public
uint
reserved1;
92
public
ushort
free_ncbs;
93
public
ushort
max_cfg_ncbs;
94
public
ushort
max_ncbs;
95
public
ushort
xmit_buf_unavail;
96
public
ushort
max_dgram_size;
97
public
ushort
pending_sess;
98
public
ushort
max_cfg_sess;
99
public
ushort
max_sess;
100
public
ushort
max_sess_pkt_size;
101
public
ushort
name_count;
102
}
103
104
[StructLayout(LayoutKind.Sequential)]
105
public
struct
NAME_BUFFER
106
{
107
[MarshalAs(UnmanagedType.ByValArray, SizeConst
=
(
int
)NCBCONST.NCBNAMSZ)]
108
public
byte
[] name;
109
public
byte
name_num;
110
public
byte
name_flags;
111
}
112
113
[StructLayout(LayoutKind.Sequential)]
114
public
struct
NCB
115
{
116
public
byte
ncb_command;
117
public
byte
ncb_retcode;
118
public
byte
ncb_lsn;
119
public
byte
ncb_num;
120
public
IntPtr ncb_buffer;
121
public
ushort
ncb_length;
122
[MarshalAs(UnmanagedType.ByValArray, SizeConst
=
(
int
)NCBCONST.NCBNAMSZ)]
123
public
byte
[] ncb_callname;
124
[MarshalAs(UnmanagedType.ByValArray, SizeConst
=
(
int
)NCBCONST.NCBNAMSZ)]
125
public
byte
[] ncb_name;
126
public
byte
ncb_rto;
127
public
byte
ncb_sto;
128
public
IntPtr ncb_post;
129
public
byte
ncb_lana_num;
130
public
byte
ncb_cmd_cplt;
131
[MarshalAs(UnmanagedType.ByValArray, SizeConst
=
10
)]
132
public
byte
[] ncb_reserve;
133
public
IntPtr ncb_event;
134
}
135
136
[StructLayout(LayoutKind.Sequential)]
137
public
struct
LANA_ENUM
138
{
139
public
byte
length;
140
[MarshalAs(UnmanagedType.ByValArray, SizeConst
=
(
int
)NCBCONST.MAX_LANA)]
141
public
byte
[] lana;
142
}
143
144
[StructLayout(LayoutKind.Auto)]
145
public
struct
ASTAT
146
{
147
public
ADAPTER_STATUS adapt;
148
[MarshalAs(UnmanagedType.ByValArray, SizeConst
=
(
int
)NCBCONST.NUM_NAMEBUF)]
149
public
NAME_BUFFER[] NameBuff;
150
}
151
public
class
Win32API
152
{
153
[DllImport(
"
NETAPI32.DLL
"
)]
154
public
static
extern
char
Netbios(
ref
NCB ncb);
155
}
156
157
public
string
GetMacAddress()
158
{
159
string
addr
=
""
;
160
try
161
{
162
int
cb;
163
ASTAT adapter;
164
NCB Ncb
=
new
NCB();
165
char
uRetCode;
166
LANA_ENUM lenum;
167
168
Ncb.ncb_command
=
(
byte
)NCBCONST.NCBENUM;
169
cb
=
Marshal.SizeOf(
typeof
(LANA_ENUM));
170
Ncb.ncb_buffer
=
Marshal.AllocHGlobal(cb);
171
Ncb.ncb_length
=
(
ushort
)cb;
172
uRetCode
=
Win32API.Netbios(
ref
Ncb);
173
lenum
=
(LANA_ENUM)Marshal.PtrToStructure(Ncb.ncb_buffer,
typeof
(LANA_ENUM));
174
Marshal.FreeHGlobal(Ncb.ncb_buffer);
175
if
(uRetCode
!=
(
short
)NCBCONST.NRC_GOODRET)
176
return
""
;
177
178
for
(
int
i
=
0
; i
<
lenum.length; i
++
)
179
{
180
Ncb.ncb_command
=
(
byte
)NCBCONST.NCBRESET;
181
Ncb.ncb_lana_num
=
lenum.lana[i];
182
uRetCode
=
Win32API.Netbios(
ref
Ncb);
183
if
(uRetCode
!=
(
short
)NCBCONST.NRC_GOODRET)
184
return
""
;
185
186
Ncb.ncb_command
=
(
byte
)NCBCONST.NCBASTAT;
187
Ncb.ncb_lana_num
=
lenum.lana[i];
188
Ncb.ncb_callname[
0
]
=
(
byte
)
'
*
'
;
189
cb
=
Marshal.SizeOf(
typeof
(ADAPTER_STATUS))
+ Marshal.SizeOf(
typeof
(NAME_BUFFER))
*
(
int
)NCBCONST.NUM_NAMEBUF;
190
Ncb.ncb_buffer
=
Marshal.AllocHGlobal(cb);
191
Ncb.ncb_length
=
(
ushort
)cb;
192
uRetCode
=
Win32API.Netbios(
ref
Ncb);
193
adapter.adapt
=
(ADAPTER_STATUS)Marshal.PtrToStructure(Ncb.ncb_buffer,
typeof
(ADAPTER_STATUS));
194
Marshal.FreeHGlobal(Ncb.ncb_buffer);
195
196
if
(uRetCode
==
(
short
)NCBCONST.NRC_GOODRET)
197
{
198
if
(i
>
0
)
199
addr
+=
"
:
"
;
200
addr
=
string
.Format(
"
{0,2:X}{1,2:X}{2,2:X}{3,2:X}{4,2:X}{5,2:X}
"
,
201
adapter.adapt.adapter_address[
0
],
202
adapter.adapt.adapter_address[
1
],
203
adapter.adapt.adapter_address[
2
],
204
adapter.adapt.adapter_address[
3
],
205
adapter.adapt.adapter_address[
4
],
206
adapter.adapt.adapter_address[
5
]);
207
}
208
}
209
}
210
catch
211
{ }
212
return
addr.Replace(
'
'
,
'
0
'
);
213
}
214
215
}
216
}
217
1 using System;
2 using System.Runtime.InteropServices;
3 using System.Management;
4 namespace Hardware
5 {
6 /// <summary>
7 /// Hardware_Mac 的摘要说明。
8 /// </summary>
9 public class HardwareInfo
10 {
11 //取机器名
12 public string GetHostName()
13 {
14 return System.Net.Dns.GetHostName();
15 }
16
17 //取CPU编号
18 public String GetCpuID()
19 {
20 try
21 {
22 ManagementClass mc = new ManagementClass("Win32_Processor");
23 ManagementObjectCollection moc = mc.GetInstances();
24
25 String strCpuID = null;
26 foreach (ManagementObject mo in moc)
27 {
28 strCpuID = mo.Properties["ProcessorId"].Value.ToString();
29 break;
30 }
31 return strCpuID;
32 }
33 catch
34 {
35 return "";
36 }
37
38 }//end method
39
40 //取第一块硬盘编号
41 public String GetHardDiskID()
42 {
43 try
44 {
45 ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT * FROM Win32_PhysicalMedia");
46 String strHardDiskID = null;
47 foreach (ManagementObject mo in searcher.Get())
48 {
49 strHardDiskID = mo["SerialNumber"].ToString().Trim();
50 break;
51 }
52 return strHardDiskID;
53 }
54 catch
55 {
56 return "";
57 }
58 }//end
59
60 public enum NCBCONST
61 {
62 NCBNAMSZ = 16, /* absolute length of a net name */
63 MAX_LANA = 254, /* lana's in range 0 to MAX_LANA inclusive */
64 NCBENUM = 0x37, /* NCB ENUMERATE LANA NUMBERS */
65 NRC_GOODRET = 0x00, /* good return */
66 NCBRESET = 0x32, /* NCB RESET */
67 NCBASTAT = 0x33, /* NCB ADAPTER STATUS */
68 NUM_NAMEBUF = 30, /* Number of NAME's BUFFER */
69 }
70
71 [StructLayout(LayoutKind.Sequential)]
72 public struct ADAPTER_STATUS
73 {
74 [MarshalAs(UnmanagedType.ByValArray, SizeConst = 6)]
75 public byte[] adapter_address;
76 public byte rev_major;
77 public byte reserved0;
78 public byte adapter_type;
79 public byte rev_minor;
80 public ushort duration;
81 public ushort frmr_recv;
82 public ushort frmr_xmit;
83 public ushort iframe_recv_err;
84 public ushort xmit_aborts;
85 public uint xmit_success;
86 public uint recv_success;
87 public ushort iframe_xmit_err;
88 public ushort recv_buff_unavail;
89 public ushort t1_timeouts;
90 public ushort ti_timeouts;
91 public uint reserved1;
92 public ushort free_ncbs;
93 public ushort max_cfg_ncbs;
94 public ushort max_ncbs;
95 public ushort xmit_buf_unavail;
96 public ushort max_dgram_size;
97 public ushort pending_sess;
98 public ushort max_cfg_sess;
99 public ushort max_sess;
100 public ushort max_sess_pkt_size;
101 public ushort name_count;
102 }
103
104 [StructLayout(LayoutKind.Sequential)]
105 public struct NAME_BUFFER
106 {
107 [MarshalAs(UnmanagedType.ByValArray, SizeConst = (int)NCBCONST.NCBNAMSZ)]
108 public byte[] name;
109 public byte name_num;
110 public byte name_flags;
111 }
112
113 [StructLayout(LayoutKind.Sequential)]
114 public struct NCB
115 {
116 public byte ncb_command;
117 public byte ncb_retcode;
118 public byte ncb_lsn;
119 public byte ncb_num;
120 public IntPtr ncb_buffer;
121 public ushort ncb_length;
122 [MarshalAs(UnmanagedType.ByValArray, SizeConst = (int)NCBCONST.NCBNAMSZ)]
123 public byte[] ncb_callname;
124 [MarshalAs(UnmanagedType.ByValArray, SizeConst = (int)NCBCONST.NCBNAMSZ)]
125 public byte[] ncb_name;
126 public byte ncb_rto;
127 public byte ncb_sto;
128 public IntPtr ncb_post;
129 public byte ncb_lana_num;
130 public byte ncb_cmd_cplt;
131 [MarshalAs(UnmanagedType.ByValArray, SizeConst = 10)]
132 public byte[] ncb_reserve;
133 public IntPtr ncb_event;
134 }
135
136 [StructLayout(LayoutKind.Sequential)]
137 public struct LANA_ENUM
138 {
139 public byte length;
140 [MarshalAs(UnmanagedType.ByValArray, SizeConst = (int)NCBCONST.MAX_LANA)]
141 public byte[] lana;
142 }
143
144 [StructLayout(LayoutKind.Auto)]
145 public struct ASTAT
146 {
147 public ADAPTER_STATUS adapt;
148 [MarshalAs(UnmanagedType.ByValArray, SizeConst = (int)NCBCONST.NUM_NAMEBUF)]
149 public NAME_BUFFER[] NameBuff;
150 }
151 public class Win32API
152 {
153 [DllImport("NETAPI32.DLL")]
154 public static extern char Netbios(ref NCB ncb);
155 }
156
157 public string GetMacAddress()
158 {
159 string addr = "";
160 try
161 {
162 int cb;
163 ASTAT adapter;
164 NCB Ncb = new NCB();
165 char uRetCode;
166 LANA_ENUM lenum;
167
168 Ncb.ncb_command = (byte)NCBCONST.NCBENUM;
169 cb = Marshal.SizeOf(typeof(LANA_ENUM));
170 Ncb.ncb_buffer = Marshal.AllocHGlobal(cb);
171 Ncb.ncb_length = (ushort)cb;
172 uRetCode = Win32API.Netbios(ref Ncb);
173 lenum = (LANA_ENUM)Marshal.PtrToStructure(Ncb.ncb_buffer, typeof(LANA_ENUM));
174 Marshal.FreeHGlobal(Ncb.ncb_buffer);
175 if (uRetCode != (short)NCBCONST.NRC_GOODRET)
176 return "";
177
178 for (int i = 0; i < lenum.length; i++)
179 {
180 Ncb.ncb_command = (byte)NCBCONST.NCBRESET;
181 Ncb.ncb_lana_num = lenum.lana[i];
182 uRetCode = Win32API.Netbios(ref Ncb);
183 if (uRetCode != (short)NCBCONST.NRC_GOODRET)
184 return "";
185
186 Ncb.ncb_command = (byte)NCBCONST.NCBASTAT;
187 Ncb.ncb_lana_num = lenum.lana[i];
188 Ncb.ncb_callname[0] = (byte)'*';
189 cb = Marshal.SizeOf(typeof(ADAPTER_STATUS)) + Marshal.SizeOf(typeof(NAME_BUFFER)) * (int)NCBCONST.NUM_NAMEBUF;
190 Ncb.ncb_buffer = Marshal.AllocHGlobal(cb);
191 Ncb.ncb_length = (ushort)cb;
192 uRetCode = Win32API.Netbios(ref Ncb);
193 adapter.adapt = (ADAPTER_STATUS)Marshal.PtrToStructure(Ncb.ncb_buffer, typeof(ADAPTER_STATUS));
194 Marshal.FreeHGlobal(Ncb.ncb_buffer);
195
196 if (uRetCode == (short)NCBCONST.NRC_GOODRET)
197 {
198 if (i > 0)
199 addr += ":";
200 addr = string.Format("{0,2:X}{1,2:X}{2,2:X}{3,2:X}{4,2:X}{5,2:X}",
201 adapter.adapt.adapter_address[0],
202 adapter.adapt.adapter_address[1],
203 adapter.adapt.adapter_address[2],
204 adapter.adapt.adapter_address[3],
205 adapter.adapt.adapter_address[4],
206 adapter.adapt.adapter_address[5]);
207 }
208 }
209 }
210 catch
211 { }
212 return addr.Replace(' ', '0');
213 }
214
215 }
216 }
217
2、然后是主界面程序的
1
private
void
btn_ReadHard_Click(
object
sender, EventArgs e)
2
{
3
//
读机器码
4
string
hardcode
=
""
;
5
Hardware.HardwareInfo hd
=
new
Hardware.HardwareInfo();
6
hardcode
=
hd.GetCpuID();
7
MD5CryptoServiceProvider md5
=
new
MD5CryptoServiceProvider();
8
byte
[] hdcode1
=
System.Text.Encoding.UTF8.GetBytes(hardcode
+
"
new
"
);
9
byte
[] hdcode2
=
md5.ComputeHash(hdcode1);
10
md5.Clear();
11
tb_Hardcode.Text
=
Convert.ToBase64String(hdcode2).Replace(
"
=
"
,
""
);
12
13
}
1
2
private
string
GetRegCode(
string
MacCode)
3
{
4
string
stra
=
MacCode.Substring(
16
);
5
string
[] arstr
=
new
string
[
4
]{MacCode.Substring(
1
,
5
),
6
MacCode.Substring(
6
,
5
),MacCode.Substring(
11
,
5
),MacCode.Substring(
16
,
5
)};
7
string
str5
=
""
;
8
MD5CryptoServiceProvider md5
=
new
MD5CryptoServiceProvider();
9
byte
[] hdcode;
10
byte
[] regcode;
11
for
(
int
i
=
0
; i
<
arstr.Length;i
++
)
12
{
13
hdcode
=
System.Text.Encoding.UTF8.GetBytes(arstr[i]
+
"
string
"
);
14
regcode
=
md5.ComputeHash(hdcode);
15
str5
+=
Convert.ToBase64String(regcode).Replace(
"
=
"
,
""
);
16
}
17
md5.Clear();
18
return
str5;
19
}
1 private void btn_ReadHard_Click(object sender, EventArgs e)
2 {
3 //读机器码
4 string hardcode = "";
5 Hardware.HardwareInfo hd = new Hardware.HardwareInfo();
6 hardcode = hd.GetCpuID();
7 MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
8 byte[] hdcode1 = System.Text.Encoding.UTF8.GetBytes(hardcode +"new");
9 byte[] hdcode2 = md5.ComputeHash(hdcode1);
10 md5.Clear();
11 tb_Hardcode.Text = Convert.ToBase64String(hdcode2).Replace("=","");
12
13 }
请大家参考6楼的回复,一定要进行工作进程模拟,否则任何一台电脑提取到的注册码是都是一样的,因为在取本机CpuID或硬盘序列号的时候,返回值为空.
或者在配置文件中加入身份验证要求,如
<allow users="CHINA-3ABA37ACE\administrator"/>
<deny users="*"/>
关于更多的asp.net身份模拟资料,请参考微软的这篇文档.
http://www.microsoft.com/china/community/program/originalarticles/techdoc/impersonation.mspx