Recently, a B/S application software was just completed, which was written in vs2003 using VB. NET. A simple software registration function is added to the software at the customer's requirement. The main principle is to first obtain the machine cpu id, then add the unit name into a string, and then encrypt the string with MD5 to obtain the registration code.
The following is the source code in regdll. VB, which implements the functions:
Option strict on
Option explicit on
Imports system
Imports system. String
Imports system. Management
Imports system. Web. Security
Namespace Reg
Public class regdll
Public Function getcode () as string
Dim mcode as string = getcpuid (). tostring
Return mcode
End Function
Public Function getmd5 (byval code as string) as string
Return System. Web. Security. formsauthentication. hashpasswordforstoringinconfigfile (Code, "MD5 ")
End Function
Public Function gethostname () as string 'get the machine name
Return System. net. DNS. gethostname ()
End Function
Public Function getcpuid () as string 'gets the CPU ID
Dim MC as managementclass = new managementclass ("win32_processor ")
Dim MOC as managementobjectcollection
MoC = mc. getinstances ()
Dim strcpuid as string = ""
For each MO as managementobject in MoC
Strcpuid = Mo. properties ("processorid"). value. tostring ()
Exit
Next
Return strcpuid
End Function
Public Function getmainharddiskid () as string 'Get the master hard disk number
Dim searcher as managementobjectsearcher = new managementobjectsearcher ("select * From win32_physicalmedia ")
Dim disk as new managementobject ("win32_logicaldisk.deviceid = 'C :'")
Dim diskproperty as propertydata = disk. properties ("DeviceID ")
Dim strharddiskid as string = diskproperty. Name. tostring (). Trim () + diskproperty. type. tostring () + diskproperty. value. tostring () + diskproperty. qualifiers. tostring ()
Return strharddiskid
End Function
End Class
End namespace
The following is a check function checkreg (). The function first provides the unit name, local machine code, and registration code in the database, and then checks the registration information based on the return value.
The following is the source code:
Public Function checkreg () as integer
'Check Software Registration Information 0: registered correctly; 1: No organization name
'2: There is a machine code, but different from the obtained machine code, it indicates that the system is registered on another machine and the machine code is incorrect.
'3: The registration code stored on the machine is different from the newly generated registration code. The registration code is incorrect.
'4: no registration code
'5: Unknown error
Dim mycommand as new system. Data. oledb. oledbcommand ("select dwmc, mcode, regcode from dwxx", Conn)
Dim getsoft as Boolean
Dim myreader as system. Data. oledb. oledbdatareader
Dim dwmc, mcode, regcode, sqlstr as string
Try
Conn. open ()
Myreader = mycommand. executereader ()
Getsoft = myreader. hasrows
If getsoft then
Myreader. Read ()
Session ("mcode") = myreader ("mcode"). tostring
Session ("regcode") = myreader ("regcode"). tostring
Session ("dwmc") = myreader ("dwmc"). tostring
Else
Session ("mcode") = ""
Session ("regcode") = ""
Session ("dwmc") = ""
End if
Catch e as exception
Session ("mcode") = ""
Session ("regcode") = ""
Session ("dwmc") = ""
Return (5)
End try
Myreader. Close ()
Conn. Close ()
If SESSION ("dwmc"). tostring = "" then "has no unit name. 1 is returned.
Return (1)
End if
If SESSION ("mcode"). tostring = "" then', if no machine code exists, obtain the machine code first.
Session ("mcode") = myreg. getcode (). tostring
Sqlstr = "Update dwxx set mcode = '" + SESSION ("mcode"). tostring + "'"
If not executesql (sqlstr) then
Return (5)
End if
End if
If SESSION ("regcode"). tostring = "" then' no registration code 4
Return (4)
End if
If (Session ("dwmc "). tostring <> "") and (Session ("mcode "). tostring <> "") and (Session ("regcode "). tostring <> "") then
'If the registration information already exists, check whether the registration information is correct.
If SESSION ("mcode"). tostring <> myreg. getcode (). tostring then
Session ("mcode") = myreg. getcode (). tostring
Sqlstr = "Update dwxx set mcode = '" + SESSION ("mcode"). tostring + "'"
If not executesql (sqlstr) then
Return (5)
End if
End if
If (myreg. getmd5 (Session ("dwmc"). tostring + SESSION ("mcode"). tostring) <> (Session ("regcode"). tostring) then
Return (3)
Else
Return (0)
End if
End if
End Function