Windows CD-Key auslesen

    • Allgemein

    Es gibt 18 Antworten in diesem Thema. Der letzte Beitrag () ist von vb-checker.

      Windows CD-Key auslesen

      Da ich von nem Freund um Hilfe bei ner art Produktaktivierung gefragt wurde, hab ich schnell die folgenden Code-Schnipsel ergooglet.

      ACHTUNG! Man sollte den Key auf keinen Fall einfach mal so übers Internet übertragen, sondern unbedingt vorher hashen. (MD5, etc)
      Dafür muss man den Key eigentlich auch nicht von den Zahlen in die Zeichen übersetzen.
      ACHTUNG! Es muss in den AGB / der Lizenz / Privacy-Statement stehen, dass zu irgendwelchen Zwecken eine Kennung des Computers / der Windows-Installation übertragen wird.

      Der Code funktioniert auch problemlos, ohne Admin-Rechte, unter Windows Vista. (Dank an Dennis Alexander)


      VB.net-Code
      Folgenden Code in eine leere VB-Datei kopieren und dann mit Win32.WindowsCDKey bzw. Win32.WindowsCDKeyParts aufrufen.

      VB.NET-Quellcode

      1. Imports Microsoft.Win32
      2. Imports System
      3. Public Class Win32
      4. #Region "Properties"
      5. ''' <summary>
      6. ''' Reads the Windows CD key from the registry and returns it as string separated by '-' chars.
      7. ''' </summary>
      8. Public Shared ReadOnly Property WindowsCDKey() As String
      9. Get
      10. Dim rKey As RegistryKey = Registry.LocalMachine.OpenSubKey("SOFTWARE\Microsoft\Windows NT\CurrentVersion")
      11. Dim rpk As Byte() = rKey.GetValue("DigitalProductId", 0)
      12. Dim strKey As String = ""
      13. Const iRPKOffset As Integer = 52
      14. Const strPossibleChars As String = "BCDFGHJKMPQRTVWXY2346789"
      15. Dim i As Integer = 28
      16. Do
      17. Dim lAccu As Long = 0
      18. Dim j As Integer = 14
      19. Do
      20. lAccu *= 256
      21. lAccu += Convert.ToInt64(rpk(iRPKOffset + j))
      22. rpk(iRPKOffset + j) = Convert.ToByte(Convert.ToInt64(Math.Floor(CSng(lAccu) / 24F)) And Convert.ToInt64(255))
      23. lAccu = lAccu Mod 24
      24. j -= 1
      25. Loop While j >= 0
      26. i -= 1
      27. strKey = strPossibleChars(CInt(lAccu)).ToString() + strKey
      28. If (0 = ((29 - i) Mod 6)) AndAlso (-1 <> i) Then
      29. i -= 1
      30. strKey = "-" + strKey
      31. End If
      32. Loop While i >= 0
      33. Return strKey
      34. End Get
      35. End Property
      36. ''' <summary>
      37. ''' Reads the Windows CD key from the registry and returns it as string array.
      38. ''' </summary>
      39. Public Shared ReadOnly Property WindowsCDKeyParts() As String()
      40. Get
      41. Dim strKeyParts As String() = WindowsCDKey.Split("-"C)
      42. Return strKeyParts
      43. End Get
      44. End Property
      45. #End Region
      46. End Class
      Original (C#): dotnet-snippets.de


      VB6-Code
      Folgenden Code in ein Modul kopieren und dann eine der beiden Funktionen aufrufen.

      VB.NET-Quellcode

      1. Option Explicit
      2. 'APIs
      3. Private Declare Function RegOpenKeyEx Lib "advapi32.dll" Alias "RegOpenKeyExA" _
      4. (ByVal hKey As Long, ByVal lpSubKey As String, ByVal ulOptions As Long, _
      5. ByVal samDesired As Long, phkResult As Long) As Long
      6. Private Declare Function RegQueryValueEx Lib "advapi32.dll" Alias "RegQueryValueExA" _
      7. (ByVal hKey As Long, ByVal lpValueName As String, ByVal lpReserved As Long, _
      8. lpType As Long, lpData As Any, lpcbData As Long) As Long
      9. ' Note that if you declare the lpData parameter as String,
      10. ' you must pass it By Value.
      11. Private Declare Function RegCloseKey Lib "advapi32.dll" (ByVal hKey As Long) As Long
      12. '--
      13. 'Consts
      14. Const ERROR_SUCCESS As Long = 0&
      15. Const HKEY_LOCAL_MACHINE As Long = &H80000002
      16. Const NT_CURRENT As String = "SOFTWARE\MICROSOFT\Windows NT\CurrentVersion"
      17. Const REG_OPTION_NON_VOLATILE = &H0
      18. Const KEY_QUERY_VALUE = &H1
      19. Const KEY_SET_VALUE = &H2
      20. Const KEY_CREATE_SUB_KEY = &H4
      21. Const KEY_ENUMERATE_SUB_KEYS = &H8
      22. Const KEY_NOTIFY = &H10
      23. Const KEY_CREATE_LINK = &H20
      24. Const KEY_ALL_ACCESS = KEY_QUERY_VALUE Or KEY_SET_VALUE Or KEY_CREATE_SUB_KEY _
      25. Or KEY_ENUMERATE_SUB_KEYS Or KEY_NOTIFY Or KEY_CREATE_LINK
      26. '--
      27. 'Functions
      28. 'Reads the Windows CD key from the registry and returns it as string separated by '-' chars.
      29. Public Function WindowsCDKey() As String
      30. Dim hRegistryKey As Long
      31. If RegOpenKeyEx(HKEY_LOCAL_MACHINE, NT_CURRENT, REG_OPTION_NON_VOLATILE, _
      32. KEY_ALL_ACCESS, hRegistryKey) = ERROR_SUCCESS Then
      33. Dim rpk(163) As Byte
      34. Dim DataLength As Long: DataLength = 164
      35. If RegQueryValueEx(hRegistryKey, "DigitalProductId", _
      36. 0, 0, ByVal VarPtr(rpk(0)), DataLength) = ERROR_SUCCESS Then
      37. Dim strKey As String
      38. Const iRPKOffset As Integer = 52
      39. Dim strPossibleChars As String: strPossibleChars = "BCDFGHJKMPQRTVWXY2346789"
      40. Dim i As Integer: i = 28
      41. Do
      42. Dim lAccu As Long: lAccu = 0
      43. Dim j As Integer: j = 14
      44. Do
      45. lAccu = lAccu * 256
      46. lAccu = lAccu + CLng(rpk(iRPKOffset + j))
      47. rpk(iRPKOffset + j) = CByte(Int(CSng(lAccu) / 24#) And CLng(255))
      48. lAccu = lAccu Mod 24
      49. j = j - 1
      50. Loop While j >= 0
      51. i = i - 1
      52. strKey = Mid$(strPossibleChars, lAccu + 1, 1) & strKey
      53. If (0 = ((29 - i) Mod 6)) And (-1 <> i) Then
      54. i = i - 1
      55. strKey = "-" + strKey
      56. End If
      57. Loop While i >= 0
      58. WindowsCDKey = strKey
      59. End If
      60. RegCloseKey hRegistryKey
      61. End If
      62. End Function
      63. 'Reads the Windows CD key from the registry and returns it as string array.
      64. Public Function WindowsCDKeyParts() As String()
      65. Dim strKeyParts() As String
      66. strKeyParts = Split(WindowsCDKey, "-")
      67. WindowsCDKeyParts = strKeyParts
      68. End Function
      69. '--
      Original: s.o. + codeproject.com



      Keywords: Visual Basic, VB6, VB.net, C#, C++, Windows CD-Key, Registry, Produktaktivierung

      Dieser Beitrag wurde bereits 6 mal editiert, zuletzt von „Mad Andy“ ()

      Zieh dir einen Button auf die Form und eine TextBox, füge den CD-Key-Code ein und dann schreibe in Button1_Click:

      VB.NET-Quellcode

      1. TextBox1.Text = WindowsCDKey


      und benenne aber die Klasse Win32 um in Form1
      Und wenn du nun auf den Button klickst, erscheint der CD-Key in der TextBox

      %P% :thumbup:
      Hello World

      huttERic schrieb:

      superhans21 schrieb:

      Wäre toll, wenns auch jemand, der Windows 7 hat testen könnte.

      Mfg superhans21
      Okay, da du den Thread schon ausgegraben hast: Grade getestet, es geht ;)

      Bei mir geht es nicht.

      Mach ich da was falsch? Habe neue Klasse hinzugefügt, Win32.vb benannt, den ersten Quelltext reinkopiert.
      Dann in Form1 so aufgerufen:

      VB.NET-Quellcode

      1. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
      2. TextBox1.Text = Win32.WindowsCDKey
      3. End Sub


      System.InvalidCastException: Das Objekt des Typs "System.Int32" kann nicht in Typ "System.Byte[]" umgewandelt werden.

      Markiert wird diese Zeile:

      VB.NET-Quellcode

      1. Dim rpk As Byte() = rKey.GetValue("DigitalProductId", 0)
      Moin,

      habe immer unter vb 2010 folgende Fehlermeldung:

      Das Objekt des Typs "System.Int32" kann nicht in Typ "System.Byte[]" umgewandelt werden.

      Aber eine bestimmte Zeile markiert er mir nicht. Es kommt auch nur, wenn ich Try Catch hinzufüge, sonst passiert nicht. Unter vb 2008 funktioniert es aber wunderbar.
      Ich bekomme auch diesen Fehler: Das Objekt des Typs "System.Int32" kann nicht in Typ "System.Byte[]" umgewandelt werden.

      Ich benutze Win 7 x64.

      Egel ob Option STrict on oder off und ob mit CType oder nicht

      Skybird schrieb:

      Das sind ja Ubisoftmethoden hier !

      Es lag wirklich an der Ziel-CPU. Es muss für AnyCPU kompiliert werden. Mit x86 läuft es nicht. Danke

      @Mad Andy: Ändere diese Zeile doch bitte, damit das ganze auch Option Strict On validiert ist.

      VB.NET-Quellcode

      1. Dim rpk As Byte() = CType(rKey.GetValue("DigitalProductId", 0), Byte())

      Skybird schrieb:

      Das sind ja Ubisoftmethoden hier !