Advertisement
asril99

Untitled

Jul 16th, 2016
169
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.07 KB | None | 0 0
  1. Option Strict On
  2.  
  3. Public Class Form1
  4.  
  5. Private kstrRegSubKeyName As String = "Penjualan" 'The name for the sub-key to store registry info
  6. Private mintUsedTrialDays As Integer
  7. Private mintTrialPeriod As Integer = 11 'Days in the trial.
  8. Private mblnInTrial As Boolean = True
  9. Private mblnFullVersion As Boolean = False
  10.  
  11. Private Structure CurrentDate
  12. Dim Day As String
  13. Dim Month As String
  14. Dim Year As String
  15. End Structure
  16.  
  17. #Region " - Generate key - "
  18.  
  19. Private Sub txtGKClientName_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtGKClientName.TextChanged
  20. GenerateKey(txtPassPhrase.Text, txtGKClientName.Text)
  21. End Sub
  22.  
  23. Private Sub txtPassPhrase_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtPassPhrase.TextChanged
  24. GenerateKey(txtPassPhrase.Text, txtGKClientName.Text)
  25. End Sub
  26.  
  27. Private Sub GenerateKey(ByVal pPassPhrase As String, ByVal pClientName As String)
  28. Dim strRegkey As String = Encrypt(pPassPhrase, pClientName.ToUpper)
  29. If strRegkey.Length > 0 Then
  30. strRegkey = strRegkey.Remove(16, (strRegkey.Length - 16))
  31. strRegkey = strRegkey.Insert(4, "-")
  32. strRegkey = strRegkey.Insert(8, "-")
  33. strRegkey = strRegkey.Insert(12, "-")
  34. End If
  35. txtGeneratedKey.Text = strRegkey
  36. End Sub
  37.  
  38. #End Region ' - Generate key -
  39.  
  40. #Region " - Validate key - "
  41.  
  42. Private Sub btnTestKey_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnTestKey.Click
  43. lblVKStatus.Text = ""
  44. Dim strRegCode As String = Me.txtKeyToValidate.Text.ToUpper
  45. Dim strPassPhrase As String = txtPassPhrase.Text
  46. Dim strUserName As String = Encrypt(strPassPhrase, txtVKClientName.Text.ToUpper)
  47. strUserName = strUserName.Remove(16, (strUserName.Length - 16))
  48. strRegCode = strRegCode.Replace("-", "")
  49. If strUserName = strRegCode Then
  50. AuthorizeComputer(strPassPhrase, txtVKClientName.Text.ToUpper, strRegCode)
  51. Else
  52. lblVKStatus.Text = "The name and the key entered appears to be incorrect!"
  53. End If
  54. End Sub
  55.  
  56. Private Function AuthorizeComputer(ByVal pPassPhrase As String, ByVal pUsername As String, ByVal pRegCode As String) As Boolean
  57. Try
  58. Dim strMotherboardID As String = Encrypt(pPassPhrase, cHardware.GetMotherBoardID.Trim)
  59. Dim oReg As Microsoft.Win32.RegistryKey
  60. oReg = Microsoft.Win32.Registry.CurrentUser.OpenSubKey("Software", True)
  61. oReg = oReg.CreateSubKey(kstrRegSubKeyName)
  62. oReg.SetValue("USERID", Encrypt(pPassPhrase, pUsername))
  63. oReg.SetValue("LOCALPATH", Encrypt(pPassPhrase, pRegCode))
  64. oReg.SetValue("Enabled", "")
  65. 'Store these two values, the program will check for matches on each run.
  66. 'Just to be safe. :P
  67. oReg.SetValue("CompID", strMotherboardID)
  68. oReg.Close()
  69. MessageBox.Show("The license of your application is now saved.", "Licensing demo", MessageBoxButtons.OK, MessageBoxIcon.Information)
  70. Return True
  71. Catch ex As Exception
  72. MessageBox.Show("Impossible to save license info", "Licensing demo", MessageBoxButtons.OK, MessageBoxIcon.Error)
  73. Return False
  74. End Try
  75. End Function
  76.  
  77. #End Region ' - Validate key -
  78.  
  79. #Region " - Application status - "
  80.  
  81. Private Sub btnApplicationStatus_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnApplicationStatus.Click
  82. Dim oReg As Microsoft.Win32.RegistryKey
  83. oReg = Microsoft.Win32.Registry.CurrentUser.OpenSubKey("Software", True)
  84. oReg = oReg.CreateSubKey(kstrRegSubKeyName)
  85. oReg = Microsoft.Win32.Registry.CurrentUser.OpenSubKey("Software\\" & kstrRegSubKeyName)
  86. Dim strOldDay As String = oReg.GetValue("UserSettings", "").ToString
  87. Dim strOldMonth As String = oReg.GetValue("operatingsystem", "").ToString
  88. Dim strOldYear As String = oReg.GetValue("GUID", "").ToString
  89. Dim strRegName As String = oReg.GetValue("USERID", "").ToString
  90. Dim strRegCode As String = oReg.GetValue("LOCALPATH", "").ToString
  91. Dim strCompID As String = oReg.GetValue("CompID", "").ToString
  92. Dim strTrialDone As String = oReg.GetValue("Enable", "").ToString
  93. oReg.Close()
  94.  
  95. 'If the keys should automatically be created, then create them.
  96. If strOldDay = "" Then
  97. CreateRegKeys(txtPassPhrase.Text)
  98. End If
  99.  
  100. 'If the keys are encrypted, decrypt them.
  101. 'If EncryptKeys = True Then
  102. strOldDay = Decrypt(txtPassPhrase.Text, strOldDay)
  103. strOldMonth = Decrypt(txtPassPhrase.Text, strOldMonth)
  104. strOldYear = Decrypt(txtPassPhrase.Text, strOldYear)
  105. 'End If
  106.  
  107. 'Define global variables.
  108. mintUsedTrialDays = DiffDate(strOldDay, strOldMonth, strOldYear)
  109.  
  110. 'Fill the progress bar
  111. lblApplicationStatus.Text = DisplayApplicationStatus(DiffDate(strOldDay, strOldMonth, strOldYear), mintTrialPeriod)
  112.  
  113. 'Disable the continue button if the trial is over
  114. If DiffDate(strOldDay, strOldMonth, strOldYear) > mintTrialPeriod Then
  115. 'unregbutton.Enabled = False
  116. mblnInTrial = False
  117. oReg = Microsoft.Win32.Registry.CurrentUser.OpenSubKey("Software", True)
  118. oReg = oReg.CreateSubKey(kstrRegSubKeyName)
  119. oReg.SetValue("Enable", "1")
  120. oReg.Close()
  121. End If
  122.  
  123. 'If the date is earlier than possible, then disable the program.
  124. If strOldMonth = "" Then
  125. Else
  126. Dim dtmOldDate As Date = New Date(Convert.ToInt32(strOldYear), Convert.ToInt32(strOldMonth), Convert.ToInt32(strOldDay))
  127. If Date.Compare(DateTime.Now, dtmOldDate) < 0 Then
  128. 'unregbutton.Enabled = False
  129. mblnInTrial = False
  130. oReg = Microsoft.Win32.Registry.CurrentUser.OpenSubKey("Software", True)
  131. oReg = oReg.CreateSubKey(kstrRegSubKeyName)
  132. oReg.SetValue("Enable", "1")
  133. oReg.Close()
  134. End If
  135. End If
  136.  
  137.  
  138. 'If the trial is done then disable the button
  139. If strTrialDone = "1" Then
  140. 'unregbutton.Enabled = False
  141. mblnInTrial = False
  142. lblApplicationStatus.Text = "The system clock has been manually changed, and the application has been locked out to prevent unauthorized access!"
  143. End If
  144.  
  145. 'See if the user is already registered, if so re-process the info and check if the computer is all okay.
  146. If strRegName = "" Then
  147. Else
  148. Dim strRN As String = Decrypt(txtPassPhrase.Text, strRegName)
  149. Dim strRC As String = Decrypt(txtPassPhrase.Text, strRegCode)
  150. Dim UserName As String = strRegName
  151. UserName = UserName.Remove(16, (UserName.Length - 16))
  152. If UserName = Decrypt(txtPassPhrase.Text, strRegCode) Then
  153. If Encrypt(txtPassPhrase.Text, cHardware.GetMotherBoardID.Trim.ToString) = strCompID Then
  154. mblnInTrial = False
  155. mblnFullVersion = True
  156.  
  157. strRC = strRC.Insert(4, "-")
  158. strRC = strRC.Insert(8, "-")
  159. strRC = strRC.Insert(12, "-") 'Add dashes to make it look cool
  160.  
  161. lblApplicationStatus.Text = "Licensed version to " + strRN + " with the key " + strRC
  162. oReg = Microsoft.Win32.Registry.CurrentUser.OpenSubKey("Software", True)
  163. oReg = oReg.CreateSubKey(kstrRegSubKeyName)
  164. oReg.SetValue("Enable", "")
  165. oReg.Close()
  166. End If
  167.  
  168. End If
  169. End If
  170. End Sub
  171.  
  172. Private Function DisplayApplicationStatus(ByVal pDaysUsed As Integer, ByVal pTotalDays As Integer) As String
  173. 'Check if the author made the mistake of setting the trial period days to less than 0
  174. If pTotalDays < 0 Then
  175. Return "An error has occurred! The author has alloted you a trial period less than zero days, which is impossible. Please contact the author and tell him/her of this error."
  176. End If
  177.  
  178. 'Check if the trial is expired
  179. If pDaysUsed >= pTotalDays Then
  180. Return "Your trial has expired!"
  181. End If
  182.  
  183. 'Draw the bar
  184. Return "You have " + (pTotalDays - pDaysUsed).ToString + " days remaining in your free trial period."
  185. End Function
  186.  
  187. Private Sub CreateRegKeys(ByVal pPassPhrase As String)
  188. Try
  189. Dim Current As CurrentDate
  190. Current.Day = DateTime.Now.Day.ToString
  191. Current.Month = DateTime.Now.Month.ToString
  192. Current.Year = DateTime.Now.Year.ToString
  193. 'If EncryptKeys = True Then
  194. Current.Day = Encrypt(pPassPhrase, Current.Day)
  195. Current.Month = Encrypt(pPassPhrase, Current.Month)
  196. Current.Year = Encrypt(pPassPhrase, Current.Year)
  197. 'End If
  198. Dim oReg As Microsoft.Win32.RegistryKey
  199. oReg = Microsoft.Win32.Registry.CurrentUser.OpenSubKey("Software", True)
  200. oReg = oReg.CreateSubKey(kstrRegSubKeyName)
  201. oReg.SetValue("UserSettings", Current.Day)
  202. oReg.SetValue("operatingsystem", Current.Month)
  203. oReg.SetValue("GUID", Current.Year)
  204. oReg.Close()
  205. Catch
  206. End Try
  207. End Sub
  208.  
  209. Private Function DiffDate(ByVal OrigDay As String, ByVal OrigMonth As String, ByVal OrigYear As String) As Integer
  210. Try
  211. Dim D1 As Date = New Date(Convert.ToInt32(OrigYear), Convert.ToInt32(OrigMonth), Convert.ToInt32(OrigDay))
  212. Return Convert.ToInt32(DateDiff(DateInterval.Day, D1, DateTime.Now))
  213. Catch
  214. Return 0
  215. End Try
  216. End Function
  217.  
  218. #End Region ' - Application status -
  219.  
  220. #Region " - Common methods - "
  221.  
  222. Private Function Encrypt(ByRef pPassPhrase As String, ByVal pTextToEncrypt As String) As String
  223. If pPassPhrase.Length > 16 Then
  224. 'limitation of the encryption mechanism
  225. pPassPhrase = pPassPhrase.Substring(0, 16)
  226. End If
  227.  
  228. If pTextToEncrypt.Trim.Length = 0 Then
  229. 'the Text to encrypt not set!!!
  230. Return String.Empty
  231. End If
  232.  
  233. Dim skey As New Encryption.Data(pPassPhrase)
  234. Dim sym As New Encryption.Symmetric(Encryption.Symmetric.Provider.Rijndael)
  235. Dim objEncryptedData As Encryption.Data
  236. objEncryptedData = sym.Encrypt(New Encryption.Data(pTextToEncrypt), skey)
  237. Return objEncryptedData.ToHex
  238. End Function
  239.  
  240. Private Function Decrypt(ByRef pPassPhrase As String, ByVal pHexStream As String) As String
  241. Try
  242. Dim objSym As New Encryption.Symmetric(Encryption.Symmetric.Provider.Rijndael)
  243. Dim encryptedData As New Encryption.Data
  244. encryptedData.Hex = pHexStream
  245. Dim decryptedData As Encryption.Data
  246. decryptedData = objSym.Decrypt(encryptedData, New Encryption.Data(pPassPhrase))
  247. Return decryptedData.Text
  248. Catch
  249. Return Nothing
  250. End Try
  251. End Function
  252.  
  253. 'Private Function DecryptU(ByVal hexstream As String) As String
  254. ' Try
  255. ' Dim register As New register
  256. ' Dim sym As New Encryption.Symmetric(Encryption.Symmetric.Provider.Rijndael)
  257. ' Dim encryptedData As New Encryption.Data
  258. ' encryptedData.Hex = hexstream
  259. ' Dim decryptedData As Encryption.Data
  260. ' decryptedData = sym.Decrypt(encryptedData, register.skey)
  261. ' Return decryptedData.ToString
  262. ' Catch
  263. ' End Try
  264. 'End Function
  265. #End Region ' - Common methods -
  266.  
  267. Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  268.  
  269. End Sub
  270.  
  271. Private Sub GroupBox2_Enter(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles GroupBox2.Enter
  272.  
  273. End Sub
  274. End Class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement