7,765
社区成员
发帖
与我相关
我的任务
分享
Imports System.IO
Public Class Form1
Private Sub RegisterToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles RegisterToolStripMenuItem.Click
If Algorithm.Isval Then
MsgBox("你已经成功注册了本产品,谢谢")
Exit Sub
End If
Dim reg As New RegDialog
reg.ShowDialog()
If Algorithm.Isval Then
Text = Application.ProductName
End If
End Sub
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
Dim un = ""
Dim key = ""
Try
Dim arr = File.ReadAllLines("reg.txt")
un = arr(0)
key = arr(1)
Catch ex As Exception
End Try
Dim al As New Algorithm
If Not al.ValKey(un, key) Then
Text = Application.ProductName & " - 未注册"
RegDialog.ShowDialog()
If al.ValKey(un, key) Then Text = Application.ProductName
Else
Text = Application.ProductName
End If
End Sub
Private Sub NewToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles NewToolStripMenuItem.Click
If Not Algorithm.Isval Then
MsgBox("只有注册用户才能使用这个功能")
Else
MsgBox("打开文件")
End If
End Sub
End Class
Imports System.IO
Imports System.Text.RegularExpressions
Public Class RegDialog
Private Sub RegDialog_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
TextBox2.Text = New Algorithm().GetMachineCode()
End Sub
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
If Len(Trim(TextBox1.Text)) <= 3 Then
MsgBox("用户名需要大于3个字符")
Exit Sub
End If
Dim key As String = UCase(Trim(Replace(TextBox3.Text, "-", "")))
If Not Regex.IsMatch(key, "[A-F0-9]{25}") Then
MsgBox("序列号格式无效,序列号是一个25位长度的字符串,类似XXXXX-XXXXX-XXXXX-XXXXX-XXXXX,请向开发者获取")
Exit Sub
End If
If TextBox2.Text = TextBox3.Text Then
MsgBox("序列号和机器码是不同的,请勿混淆")
Exit Sub
End If
Dim al As New Algorithm()
al.ValKey(TextBox1.Text, TextBox3.Text)
File.WriteAllText("reg.txt", TextBox1.Text + vbCrLf + TextBox3.Text)
If Algorithm.Isval Then
Close()
Else
MsgBox("序列号错误,请重新输入")
End If
End Sub
Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
Close()
End Sub
End Class
Imports System.Text
Imports System.Management
Imports System.Security.Cryptography
Public Class Algorithm
Private Const pkey As String = "263cc70e-f646-4b1a-9959-673f4ca4c47e"
Public Shared Isval As Boolean = False
Public Function FormatSN(sn As String) As String
Dim r As String = Mid(sn, 1, 5)
For i As Integer = 1 To 4
r += "-" & Mid(sn, i * 5 + 1, 5)
Next
Return UCase(r)
End Function
Public Function GetMachineCode() As String
Dim mc As ManagementClass = New ManagementClass("Win32_DiskDrive")
Dim moc As ManagementObjectCollection = mc.GetInstances()
Dim sn As String = "disk_id"
For Each item As ManagementObject In moc
sn += item.Properties("SerialNumber").Value
Next
Dim md5 As New MD5CryptoServiceProvider
Dim bsn() As Byte = md5.ComputeHash(Encoding.Default.GetBytes(pkey & sn)).Concat(md5.ComputeHash(Encoding.Default.GetBytes(sn & pkey))).ToArray()
sn = ""
For i As Integer = 0 To 24
sn += Hex(bsn(i) Mod 16)
Next
Return FormatSN(sn)
End Function
Public Function GetKey(username As String, machinecode As String) As String
Dim sn As String = Trim(UCase(username)) & ";" & Trim(UCase(Replace(machinecode, "-", "")))
Dim md5 As New MD5CryptoServiceProvider
Dim bsn() As Byte = md5.ComputeHash(Encoding.Default.GetBytes(pkey & sn)).Concat(md5.ComputeHash(Encoding.Default.GetBytes(sn & pkey))).ToArray()
sn = ""
For i As Integer = 0 To 24
sn += Hex(bsn(i) Mod 16)
Next
Return FormatSN(sn)
End Function
Public Function ValKey(username As String, key As String) As Boolean
Dim mc As String = GetMachineCode()
Isval = UCase(Replace(GetKey(username, mc), "-", "")) = Trim(UCase(Replace(key, "-", "")))
Return Isval
End Function
End Class
Imports System.Text.RegularExpressions
Module Module1
Sub Main()
Dim al As New Algorithm
Console.Write("输入用户名,不区分大小写,长度大于3个字符:")
Dim username As String = Console.ReadLine()
If Len(username) <= 3 Then
Console.WriteLine("用户名太短")
Exit Sub
End If
Console.WriteLine("当前机器码 " + al.GetMachineCode())
Console.Write("回车使用当前机器码,或者输入新的机器码(不区分大小写,可以不输入横线):")
Dim inputmachinecode As String = Trim(Replace(UCase(Console.ReadLine()), "-", ""))
If inputmachinecode <> "" Then
If Not Regex.IsMatch(inputmachinecode, "^[A-F0-9]{25}$") Then
Console.WriteLine("机器码输入错误")
Exit Sub
End If
End If
Dim machinecode As String = IIf(inputmachinecode = "", al.GetMachineCode(), inputmachinecode)
Console.WriteLine("提供给用户的注册码是:" + al.GetKey(username, machinecode))
End Sub
End Module