Create License Key Application

Creat License Key Mới Nhất 2025,Create License Key Application
Chào mọi người, bài viết này cũng không còn xa lạ gì với một số anh em chuyên code và tạo bản quyền chương trình nữa đúng không.

Tạo License Key Mới Nhất 2025

Hôm nay mình sẽ hướng dẫn mọi người lại về điều trên và tạo ra một chương trình có bản quyền tốt nhất để tránh những anh chị em phá phách bé khóa chương trình.

Việc tạo bản quyền (license key) cho một chương trình viết bằng Visual Basic (VB.NET) thường bao gồm các bước như:
  1. Tạo khóa bản quyền (License Key).
  2. Lưu và xác minh khóa bản quyền khi chạy chương trình.
  3. Bảo vệ mã nguồn tránh bị bẻ khóa dễ dàng.
Dưới đây là một ví dụ cơ bản bằng Visual Basic để tạo và kiểm tra license key dựa trên Serial Number của máy + mã hóa:

🔐 1. Tạo Mã License (dành cho nhà phát hành)

Imports System.Security.Cryptography
Imports System.Text

Module LicenseGenerator
    Function GenerateLicenseKey(machineId As String, secretKey As String) As String
        Dim encoding As New UTF8Encoding()
        Dim hmac As New HMACSHA256(encoding.GetBytes(secretKey))
        Dim hashBytes As Byte() = hmac.ComputeHash(encoding.GetBytes(machineId))
        Return Convert.ToBase64String(hashBytes).Substring(0, 20).ToUpper()
    End Function

    Sub Main()
        Dim machineId As String = "ABC1234567" ' Thay bằng ID thật của máy người dùng
        Dim secretKey As String = "MY_SECRET_KEY"
        Dim licenseKey As String = GenerateLicenseKey(machineId, secretKey)

        Console.WriteLine("License Key: " & licenseKey)
    End Sub
End Module

💻 2. Kiểm Tra License Khi Người Dùng Chạy Ứng Dụng

Imports System.Security.Cryptography
Imports System.Text

Public Class LicenseChecker
    Private secretKey As String = "MY_SECRET_KEY"

    Public Function VerifyLicense(machineId As String, userKey As String) As Boolean
        Dim expectedKey As String = GenerateLicenseKey(machineId, secretKey)
        Return String.Equals(expectedKey, userKey, StringComparison.OrdinalIgnoreCase)
    End Function

    Private Function GenerateLicenseKey(machineId As String, secret As String) As String
        Dim encoding As New UTF8Encoding()
        Dim hmac As New HMACSHA256(encoding.GetBytes(secret))
        Dim hashBytes As Byte() = hmac.ComputeHash(encoding.GetBytes(machineId))
        Return Convert.ToBase64String(hashBytes).Substring(0, 20).ToUpper()
    End Function
End Class

🖥️ 3. Lấy Machine ID (VD: từ ổ đĩa C)

Imports System.Management

Public Function GetMachineId() As String
    Dim searcher As New ManagementObjectSearcher("SELECT * FROM Win32_LogicalDisk WHERE DeviceID = 'C:'")
    For Each disk As ManagementObject In searcher.Get()
        Return disk("VolumeSerialNumber").ToString()
    Next
    Return "UNKNOWN"
End Function

✅ 4. Cách Dùng Trong Form

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    Dim machineId As String = GetMachineId()
    Dim userLicenseKey As String = My.Settings.LicenseKey ' Hoặc nhập từ người dùng
    Dim checker As New LicenseChecker()

    If checker.VerifyLicense(machineId, userLicenseKey) Then
        MessageBox.Show("Đã kích hoạt bản quyền!")
    Else
        MessageBox.Show("Sai mã bản quyền. Vui lòng liên hệ hỗ trợ.")
        Application.Exit()
    End If
End Sub

🔒 Lưu ý Bảo Mật

  • Nên mã hóa chuỗi key hoặc dùng RSA/DSA để tăng độ bảo mật.
  • Kết hợp thêm yếu tố thời gian (hạn dùng) nếu bạn muốn tạo license theo thời hạn.
  • Không hard-code SecretKey trong mã nếu phân phối rộng rãi — nên mã hóa hoặc obfuscate.
Nếu bạn cần hỗ trợ tạo trình tạo key tự động, hệ thống license online, hoặc thêm tính năng như hết hạn, theo dõi key, hãy cho biết — mình có thể giúp bạn mở rộng hệ thống.
<