用最简单的方法来为自己的软件产生序列号.

思路是:用户提交网卡和CPU的字符串(先进行一下MD5这样不容易知道这个字符串是由什么构造出来的.)

程序在进行判断时,把网卡和CPU组成的KEY,进行自己设定的次数据进行MD5与注册码进行判断,相等注册成功,不相等注册失败.够简单吧.

1.首先需要获取网卡的MAC和CPU的ID,需要引用System.Management库

1)获取MAC

   Public Function GetMac() As String
        Dim query As System.Management.ManagementObjectSearcher = New System.Management.ManagementObjectSearcher("SELECT * FROM Win32_NetworkAdapterConfiguration")
        Dim queryCollection As System.Management.ManagementObjectCollection = query.Get()
        For Each obj As System.Management.ManagementObject In queryCollection
            If obj.Item("IPEnabled").ToString = "True" Then
                Return obj("MacAddress").ToString.Replace(":", "")
            End If
        Next
    End Function
2)获取CPUID
    Public Function GetCpuID() As String
        Dim mc As System.Management.ManagementClass = New System.Management.ManagementClass("Win32_Processor")
        For Each obj As System.Management.ManagementObject In mc.GetInstances
            Return obj("ProcessorId").ToString
        Next
    End Function
3)MD5算法
    Public Function MD5(ByVal key As String) As String
        Dim a As New System.Security.Cryptography.MD5CryptoServiceProvider
        Dim bySource As Byte() = System.Text.Encoding.ASCII.GetBytes(key)

        Dim byTarget As Byte()
        byTarget = a.ComputeHash(bySource)

        Dim stringbuff As New System.Text.StringBuilder
        For Each b As Byte In byTarget
            stringbuff.Append(b.ToString("x2"))
        Next

        Return stringbuff.ToString
    End Function
 
4)用户的机器码
 
    Public Function GetNumber() As String
        Return MD5(Me.GetCpuID & Me.GetMac).ToUpper
    End Function
5)注册窗口
    Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim key As New KeyHelper
        Me.txtkey.Text = key.GetNumber  '获取用户机器码
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Try
           '写入注册表
            Microsoft.VisualBasic.SaveSetting("at", "atsoft", "key", Me.txtcode.Text)
            Me.Close()
        Catch ex As Exception

        End Try
    End Sub
 
6)注册码验证
 Dim reg As String = Microsoft.VisualBasic.GetSetting("at", "atsoft", "key", "")
                    If reg Is Nothing OrElse reg.Length = 0 Then
'当前用户还没有注册
                    Else
'获取机器码
                        Dim chars As String = key.GetNumber()
'5次MD5在与注册表比较,也就是说用户发来了机器码后,注册机,也需要运行五次MD5
                        For i As Integer = 1 To 5
                            chars = key.MD5(chars).ToUpper
                        Next
                        If Not chars.Equals(reg) Then
'注册成功
                            MessageBox.Show("OK", "", MessageBoxButtons.OK, MessageBoxIcon.Information)
                        End If
                    End If
 
就这样,一个简单的注册码验证的软件就完成了.
 

posted on 2008-10-27 13:07  zqonline  阅读(905)  评论(0编辑  收藏  举报

导航