ชุมชนคนรักภาษาเบสิค - Visual Basic Community

 ลืมรหัสผ่าน
 ลงทะเบียน
ค้นหา
ดู: 4418|ตอบกลับ: 5

[VB.NET] วิธีการสร้างชุดลงทะเบียนโปรแกรม (Registration)

[คัดลอกลิงก์]

311

กระทู้

502

โพสต์

6052

เครดิต

ผู้ดูแลระบบ

ทองก้อน ทับทิมกรอบ

Rank: 9Rank: 9Rank: 9

เครดิต
6052



สำหรับบทความนี้จะเป็นวิธีการง่ายๆในการทำชุดลงทะเบียนโปรแกรม ซึ่งแอดมินได้สร้างโปรเจค 2 ชุดไว้ในโซลูชั่นเดียวกัน ชุดแรกก็จะเป็นเหมือนกับโปรแกรมให้ลูกค้าใช้งาน อีกส่วนคือการสร้างรหัสลงทะเบียน (เป็นโปรเจคหลัก ต้องเข้าไปเปิด Solution ในโฟลเดอร์ RegisterProgram) เวลานำไปใช้งานจริงเราต้องแยกออกจากกันน่ะครับ ...

ขั้นตอนวิธีการคิดในการเข้ารหัส ...
[1] อ่านซีเรียลนัมเบอร์ของดิสต์ อันนี้เพื่อให้ได้โค้ดง่ายๆเข้าใจไม่ยาก แอดมินให้อ่านซีเรียลนัมเบอร์แบบ Logical Disk หรือ Volume Disk ซึ่งค่านี้จะเปลี่ยนใหม่ทุกครั้งที่มีการฟอร์แมต และที่สำคัญคือสามารถเปลี่ยนค่าได้ ... ดังนั้นในการนำไปใช้งานจริงจึงไม่ปลอดภัย แอดมินแนะนำให้อ่านซีเรียลนัมเบอร์จาก Physical Disk แทน ซึ่งโค้ดในการอ่านก็อยู่ในเว็บบอร์ดแห่งนี้แหละขอรับกระผม

[2] สร้างชุดกุญแจขึ้นมา 1 ชุด

[3] นำ 1 กับ 2 มาทำการเข้ารหัส แน่นอนว่าแอดมินใช้วิธีการเข้ารหัสแบบง่ายๆ ท่านต้องไปศึกษาค้นคว้าเพิ่มเติมเองล่ะกันครับผม

สำหรับชุดโปรแกรมที่ให้ลูกค้า หรือยูสเซอร์ใช้งาน จะต้องทำการอ่านสถานะการลงทะเบียนทุกๆครั้งที่เปิดโปรแกรมขึ้นมา โดยอ่านค่าตัวแรกคือซีเรียลนัมเบอร์ของดิสต์ (ป้องกันการเปลี่ยนแปลงค่าจากผู้ใช้) จากนั้นไปอ่านค่า Product Key หรือรหัสลงทะเบียนที่ได้ทำการ Registry เอาไว้ใน RegEdit หากไม่เจอค่าใดๆ แสดงว่ายังไม่มีการลงทะเบียน ก็ระบุ blnDemo = True หรือยังเป็นชุดทดลองอยู่นั่นเอง แต่หากมีค่า Product Key ก็เอามาทำการเข้ารหัส (เหมือนข้อ 3 ด้านบน) ด้วยซีเรียลนัมเบอร์ฮาร์ดดิสต์กับกุญแจที่กำหนดไว้ แล้วนำมาเปรียบเทียบค่า Product Key ที่ได้ก็เป็นอันจบ ...

การเลือกโปรเจคที่จะให้ทำงานเริ่มต้น (StartUp Project)


การ Registry ค่าต่างๆ การที่แอดมินนำไปไว้ที่ HKEY_CURRENT_USER ก็เพราะว่า Windows 10 มันมีการป้องกันสูง หากนำไปไว้ที่ LOCAL MACHINE มักมีปัญหาในการเขียนค่าลงไป


มาดูโค้ดในส่วนของการสร้างรหัสลงทะเบียน ... (RegisterProgram) ...
  1. Public Class frmGenKeyMaker
  2.     '// เลขชุดเหมือนกุญแจที่ต้องนำมาเข้ารหัสต้องตั้งค่าให้ตรงกัน
  3.     Dim ProgramID As UInt32 = 1234567890

  4.     Private Sub btnGenKey_Click(sender As System.Object, e As System.EventArgs) Handles btnGenKey.Click
  5.         Try
  6.             '// ค่านี้ต้องได้รับมาจากลูกค้า
  7.             Dim ProductNumber As UInt32 = UInt32.Parse(txtProductNumber.Text)
  8.             txtProductKey.Text = Encrypt(ProgramID, ProductNumber)
  9.         Catch ex As Exception
  10.             txtProductKey.Clear()
  11.             MessageBox.Show(ex.Message)
  12.         End Try
  13.     End Sub

  14.     '/ Simple encryption and decryption.
  15.     Private Function Encrypt(ByVal seed As UInt32, ByVal value As UInt32) As UInt32
  16.         Dim rand As New Random(CInt(seed \ 2))
  17.         Return (value Xor CUInt(UInt32.MaxValue * rand.NextDouble()))
  18.     End Function

  19.     Private Sub frmGenKeyMaker_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
  20.         txtProductID.Text = ProgramID
  21.         txtProductNumber.Text = GetDriveVolumeSerialNumber()
  22.     End Sub

  23.     Private Function GetDriveVolumeSerialNumber() As String
  24.         Dim DriveSerial As Long
  25.         Dim FSO As Object, Drv As Object
  26.         '/ Create a FileSystemObject object
  27.         FSO = CreateObject("Scripting.FileSystemObject")
  28.         Drv = FSO.GetDrive(FSO.GetDriveName(AppDomain.CurrentDomain.BaseDirectory))
  29.         With Drv
  30.             If .IsReady Then
  31.                 DriveSerial = .SerialNumber
  32.             Else    '"Drive Not Ready!"
  33.                 DriveSerial = -1
  34.             End If
  35.         End With
  36.         '/ Clean up
  37.         Drv = Nothing
  38.         FSO = Nothing
  39.         GetDriveVolumeSerialNumber = Math.Abs(DriveSerial) 'Hex(Math.Abs(DriveSerial))
  40.     End Function


  41. End Class
คัดลอกไปที่คลิปบอร์ด



มาดูโค้ดในส่วนของผู้ใช้งานในการลงทะเบียน ... (CustomerProgram) ...
  1. Public Class frmCustomerProgram
  2.     '// กำหนดชื่อ Section
  3.     Dim SectionName As String = "SampleProgram"
  4.     '// ตัวอย่างชุดตัวเลขเพื่อทำการเข้ารหัส (เหมือนกุญแจ)
  5.     Dim ProgramID As UInt32 = 1234567890
  6.     '// รหัสลงทะเบียนโปรแกรม
  7.     Dim ProductKey As String
  8.     '// Volume Disk
  9.     Dim ProductNumber As String

  10.     Private Sub frmCustomerProgram_FormClosed(sender As Object, e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
  11.         Me.Dispose()
  12.         GC.SuppressFinalize(Me)
  13.         Application.Exit()
  14.     End Sub

  15.     Private Sub frmCustomerProgram_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
  16.         Call Register()
  17.     End Sub

  18.     Sub Register()
  19.         '// อ่านค่า Volume Disk ใหม่ เผื่อมีการแก้ไขเปลี่ยนแปลงค่าใน Registry
  20.         ProductNumber = GetDriveVolumeSerialNumber()
  21.         '// อ่านรหัสปลดล็อคโปรแกรมจาก Registry
  22.         ProductKey = ReadAppRegistry(SectionName, "ProductKey", "")
  23.         '// หากไม่มีค่า ProductKey แสดงว่ายังไม่เคยลงทะเบียนโปรแกรม
  24.         If IsNothing(ProductKey) Or Len(ProductKey) = 0 Then
  25.             '// กำหนดให้เป็น DEMO อยู่
  26.             blnDemo = True
  27.             '// กรณีที่เกิดการแก้ไขค่า Volumn Disk ต้องใช้ค่าที่เราอ่านได้เอง
  28.             Call WriteAppRegistry(SectionName, "ProductNumber", ProductNumber)
  29.             '// ไม่มีการลงทะเบียน
  30.             Call WriteAppRegistry(SectionName, "ProductKey", "")
  31.             Me.Text = Application.ProductName & " [Demo Version]"
  32.             Exit Sub

  33.             '// แสดงว่ามี ProductKey
  34.         Else
  35.             '// เข้ารหัสค่า ProductKey ที่มาจาก Registry โดยมี ProgramID (กุญแจ)
  36.             Dim ActivateKey As UInt32 = Encrypt(ProgramID, CUInt(ProductNumber))
  37.             '// หากค่าเท่ากัน แสดงว่าลงทะเบียนโปรแกรมถูกต้อง ไม่ต้องทำการเขียนข้อมูลลง Registry
  38.             '/ ---------------------------------------------------------------
  39.             If ActivateKey = ProductKey Then
  40.                 blnDemo = False
  41.                 Me.Text = Application.ProductName & " [Version: " & Application.ProductVersion.Substring(0, 4) & "]"
  42.                 '// อาจจะมีการเปลี่ยนแปลงค่าใน Registry ก็เลยทำให้ค่าที่ได้ไม่เท่ากัน
  43.             Else
  44.                 blnDemo = True
  45.                 '// เขียนข้อมูล Volume Disk กลับลงไปใน Registry อีกครั้ง
  46.                 Call WriteAppRegistry(SectionName, "ProductNumber", ProductNumber)
  47.                 '// ให้ค่ารหัสลงทะเบียนเป็นค่าว่างเปล่า
  48.                 Call WriteAppRegistry(SectionName, "ProductKey", "")
  49.                 Me.Text = Application.ProductName & " [Demo Version]"
  50.             End If
  51.         End If
  52.     End Sub

  53.     Private Sub btnRegister_Click(sender As System.Object, e As System.EventArgs) Handles btnRegister.Click
  54.         frmRegister.ShowDialog()
  55.     End Sub
  56. End Class
คัดลอกไปที่คลิปบอร์ด

ฟอร์มในการลงทะเบียนโปรแกรม ...
  1. Public Class frmRegister

  2.     '// กำหนดชื่อ Section
  3.     Dim SectionName As String = "SampleProgram"
  4.     '// ตัวอย่างชุดตัวเลขเพื่อทำการเข้ารหัส (เหมือนกุญแจ)
  5.     Dim ProgramID As UInt32 = 1234567890
  6.     '// รหัสลงทะเบียนโปรแกรม
  7.     Dim ProductKey As String
  8.     '// Volume Disk
  9.     Dim ProductNumber As String

  10.     Private Sub frmRegister_FormClosed(sender As Object, e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
  11.         Me.Dispose()
  12.         GC.SuppressFinalize(Me)
  13.     End Sub

  14.     Private Sub frmRegister_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
  15.         '// อ่านค่า Volume Disk อีกรอบ
  16.         txtProductNumber.Text = GetDriveVolumeSerialNumber()
  17.         '// โหลดค่ารหัสลงทะเบียนมาเก็บไว้
  18.         txtProductKey.Text = ReadAppRegistry(SectionName, "ProductKey", "")
  19.         '// blnDemo จะถูกโหลดตั้งแต่ฟอร์มหลักที่เรียกมา (frmCustomerProgram.vb)
  20.         If Not blnDemo Then
  21.             txtProductKey.Enabled = False
  22.             btnOk.Enabled = False
  23.             btnCancel.Text = "Close"
  24.         End If
  25.     End Sub

  26.     Private Sub btnCancel_Click(sender As System.Object, e As System.EventArgs) Handles btnCancel.Click
  27.         Me.Close()
  28.     End Sub

  29.     Sub Register()
  30.         ProductNumber = txtProductNumber.Text
  31.         '// อ่านค่า ProductKey จาก Registry
  32.         ProductKey = ReadAppRegistry(SectionName, "ProductKey", "")
  33.         '// ยังไม่ได้ลงทะเบียน
  34.         If Len(ProductKey) = 0 And txtProductKey.Text.Trim.Length = 0 Then
  35.             blnDemo = True
  36.             frmCustomerProgram.Text = Application.ProductName & " [Demo Version]"
  37.             txtProductKey.Focus()

  38.             '// ตรวจสอบค่าที่ป้อนเข้ามาใน ProductKey
  39.         ElseIf txtProductKey.Text.Trim.Length <> 0 Then
  40.             '// เข้ารหัส ProgramID (กุญแจ) ร่วมกับ ProductNumber (Volume Disk)
  41.             Dim ActivateKey As UInt32 = Encrypt(ProgramID, CUInt(ProductNumber))
  42.             '// เปรียบเทียบค่าที่ได้จากการเข้ารหัสใหม่ (ActivateKey) และค่าที่ผู้ใช้ป้อนเข้ามา
  43.             If ActivateKey = CUInt(txtProductKey.Text) Then
  44.                 '// ถูกต้อง ...
  45.                 blnDemo = False
  46.                 Call WriteAppRegistry(SectionName, "ProductNumber", ProductNumber)
  47.                 Call WriteAppRegistry(SectionName, "ProductKey", txtProductKey.Text.Trim)
  48.                 MessageBox.Show("Registration Complete.", "Report Status", MessageBoxButtons.OK, MessageBoxIcon.Information)
  49.                 frmCustomerProgram.Text = Application.ProductName & " [Version: " & Application.ProductVersion.Substring(0, 4) & "]"
  50.                 Me.Close()
  51.             Else
  52.                 '// ลงทะเบียนมั่ว
  53.                 blnDemo = True
  54.                 MessageBox.Show("Product Key is not correct.", "Report Status", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
  55.                 frmCustomerProgram.Text = Application.ProductName & " [Demo Version]"
  56.                 txtProductKey.Focus()
  57.             End If
  58.         End If
  59.     End Sub

  60.     Private Sub btnOk_Click(sender As System.Object, e As System.EventArgs) Handles btnOk.Click
  61.         Call Register()
  62.     End Sub

  63. End Class
คัดลอกไปที่คลิปบอร์ด

โมดูลในส่วนของฟังค์ชั่นต่างๆ ...
  1. Module modFunction
  2.     '// กำหนดให้เป็นตัวแปรแบบ Public สามารถมองเห็นได้ทั้งโปรเจค
  3.     '// เพื่อระบุว่าเป็นการลงทะเบียนหรือไม่
  4.     '// blnDemo = False ลงทะเบียนเรียบร้อย (ไม่ใช่ชุดทดลอง)
  5.     '// blnDemo = True ยังไม่ได้ลงทะเบียน (ยังเป็นชุดทดลองอยู่)
  6.     Public blnDemo As Boolean = True

  7.     ' / -----------------------------------------------------------------------------------------------
  8.     ' / Registry with VB.NET function
  9.     ' / Function read original font style from Registry and return its.
  10.     Public Function ReadAppRegistry(SectionName As String, _
  11.         KeyName As String, _
  12.         KeyValue As String _
  13.     ) As String

  14.         ' Application Title ...
  15.         Dim AppTitle As String = My.Application.Info.Title
  16.         '// Check exist KeyName, If not have to create new value by default.
  17.         If GetSetting(AppTitle, SectionName, KeyName) = "" Then _
  18.             Call SaveSetting(AppTitle, SectionName, KeyName, KeyValue)

  19.         ' Return Value
  20.         ReadAppRegistry = GetSetting(AppTitle, SectionName, KeyName)

  21.     End Function

  22.     ' / -----------------------------------------------------------------------------------------------
  23.     ' / Save all font style into Registry, No return The use of sub program.
  24.     ' / Registry with VB.NET function
  25.     Public Sub WriteAppRegistry(SectionName As String, _
  26.         KeyName As String, _
  27.         KeyValue As String _
  28.     )
  29.         ' Application Title ...
  30.         Dim AppTitle As String = My.Application.Info.Title
  31.         Call SaveSetting(AppTitle, SectionName, KeyName, KeyValue)
  32.     End Sub

  33.     '/ Simple encryption and decryption.
  34.     Public Function Encrypt(ByVal seed As UInt32, ByVal value As UInt32) As UInt32
  35.         Dim rand As New Random(CInt(seed \ 2))
  36.         Return (value Xor CUInt(UInt32.MaxValue * rand.NextDouble()))
  37.     End Function

  38.     Public Function GetDriveVolumeSerialNumber() As String
  39.         Dim DriveSerial As Long
  40.         Dim FSO As Object, Drv As Object
  41.         '/ Create a FileSystemObject object
  42.         FSO = CreateObject("Scripting.FileSystemObject")
  43.         Drv = FSO.GetDrive(FSO.GetDriveName(AppDomain.CurrentDomain.BaseDirectory))
  44.         With Drv
  45.             If .IsReady Then
  46.                 DriveSerial = .SerialNumber
  47.             Else    '"Drive Not Ready!"
  48.                 DriveSerial = -1
  49.             End If
  50.         End With
  51.         '/ Clean up
  52.         Drv = Nothing
  53.         FSO = Nothing
  54.         GetDriveVolumeSerialNumber = Math.Abs(DriveSerial) 'Hex(Math.Abs(DriveSerial))
  55.     End Function
  56. End Module
คัดลอกไปที่คลิปบอร์ด

ดาวน์โหลดโค้ดต้นฉบับแบบเต็ม VB.NET (2010) ได้ที่นี่ ...

ขออภัย! โพสต์นี้มีไฟล์แนบหรือรูปภาพที่ไม่ได้รับอนุญาตให้คุณเข้าถึง

คุณจำเป็นต้อง ลงชื่อเข้าใช้ เพื่อดาวน์โหลดหรือดูไฟล์แนบนี้ คุณยังไม่มีบัญชีใช่ไหม? ลงทะเบียน

x
สิ่งที่ดีกว่าการให้ คือการให้แบบไม่มีที่สิ้นสุด

1

กระทู้

11

โพสต์

81

เครดิต

Member

Rank: 2

เครดิต
81
โพสต์ 2020-6-8 11:02:33 | ดูโพสต์ทั้งหมด

ขอบคุณคะอาจารย์ ร่ำรวยเงินทองและสุขภาพที่ดีนะคะ

5

กระทู้

21

โพสต์

165

เครดิต

Member

Rank: 2

เครดิต
165
โพสต์ 2020-9-28 19:58:35 | ดูโพสต์ทั้งหมด

ขอบคุณครับ อาจารย์

0

กระทู้

2

โพสต์

20

เครดิต

Newbie

Rank: 1

เครดิต
20
โพสต์ 2022-3-11 13:07:01 | ดูโพสต์ทั้งหมด

  ขอบคุณครับ

0

กระทู้

2

โพสต์

20

เครดิต

Newbie

Rank: 1

เครดิต
20
โพสต์ 2022-3-20 23:18:18 | ดูโพสต์ทั้งหมด

แก้ไขครั้งสุดท้ายโดย 0x1souce เมื่อ 2022-3-21 16:41

สอบถามหน่อยครับ กรณีที่ใส่ เลขมั่ว 7777777777  แบบนี้   หลังจากรันแล้ว project error ครับ ต้องแก้ยังไงคับ
แก้ได้แล้วครับ เปลี่ยนจาก CInt เป็น CLng

11

กระทู้

31

โพสต์

433

เครดิต

ผู้ดูแลบอร์ด

Rank: 7Rank: 7Rank: 7

เครดิต
433
โพสต์ 2022-10-18 16:44:10 | ดูโพสต์ทั้งหมด

แก้ไขครั้งสุดท้ายโดย puklit เมื่อ 2022-10-22 11:42

ก่อนอื่น ผมต้องขอขอบพระคุณอาจารย์เป็นอย่างสูงที่ได้ถ่ายทอดความรู้ให้แบบฟรี ๆ ไม่มีกั๊ก
สำหรับโปรแกรมด้านล่างนี้ผมได้ศึกษาจากอาจารย์แล้วนำมาต่อยอดในการลงทะเบียนดังนี้ครับ

1. คีย์ Product ID
2. คีย์1
3. คีย2
4. จะเป็น Active LicenseKey ที่ผู้ซื้อโปรแกรมจะต้องส่งข้อมูล Product ID , คีย์1 และ คีย2 กลับมาให้ผู้พัฒนา
    แล้วทางผู้พัฒนาโปรแกรมก็จะส่ง Active LicenseKey กลับไปให้ผู้ซื้อทำการลงทะเบียนต่อไปครับ

รูปตัวอย่าง การลงทะเบียน Active LicenseKey ที่ถูกต้อง


รูปตัวอย่างการลงทะเบียน Active LicenseKey ไม่ถูกต้อง


สำหรับด้านล้างนี้เป็นข้อมูลที่บันทึกการลงทะเบียนโปรแกรมในระบบ Registry ครับ




กรณียังเป็นเวอร์ชั่นทดลองใช้งาน จะแจ้งเตือนวันคงเหลือ ก่อนเข้าสู่หน้าหลัก


กรณีที่หมดวันทดลองใช้งาน ระบบจะแจ้งเตือน ข้อความหลังเข้าสู่ระบบ และจะไม่สามารถเข้าสู่หน้าหลักของโปรแกรมได้


สำหรับรูปด้านล่างนี้เป็นการถอดรหัสจากคีย์ลูกค้า เพื่อสร้าง License key ส่งกลับไปเพื่อให้ลูกค้าลงทะเบียนครับ
(เวอร์ชั่นล่าสุดผมปรับให้เหลือ 2 คีย์ เท่านั้น)





ขออภัย! โพสต์นี้มีไฟล์แนบหรือรูปภาพที่ไม่ได้รับอนุญาตให้คุณเข้าถึง

คุณจำเป็นต้อง ลงชื่อเข้าใช้ เพื่อดาวน์โหลดหรือดูไฟล์แนบนี้ คุณยังไม่มีบัญชีใช่ไหม? ลงทะเบียน

x
ขออภัย! คุณไม่ได้รับสิทธิ์ในการดำเนินการในส่วนนี้ กรุณาเลือกอย่างใดอย่างหนึ่ง ลงชื่อเข้าใช้ | ลงทะเบียน

รายละเอียดเครดิต

ข้อความล้วน|อุปกรณ์พกพา|ประวัติการแบน|G2GNet.com  

GMT+7, 2024-4-26 15:28 , Processed in 0.396433 second(s), 4 queries , File On.

Powered by Discuz! X3.4, Rev.62

Copyright © 2001-2020 Tencent Cloud.

ตอบกระทู้ ขึ้นไปด้านบน ไปที่หน้ารายการกระทู้