Any way to display raw REG_BINARY data as text?

Discussion in 'Mixed Languages' started by QuantumBug, Apr 25, 2012.

Tags:
  1. QuantumBug

    QuantumBug MDL Developer

    Mar 7, 2012
    1,485
    1,321
    60
    I'm tring to take everything from

    HKLM\Software\Microsoft\Windows NT\CurrentVersion, DigitalProductId

    and display it in a textbox, now I don't want to convert the HEX as I've already written a process to do that when decoding a product key, I literally want to display the raw HEX in a textbox unformatted and make a new line where there should be, so it looks just like below

    A4 00 00 00 00 00 00 00
    C2 00 00 01 EE 00 00 00
    00 00 00 00 00 00 00 00
    EE EE EE EE E2 E2 33 00 > and so forth

    I've been using different methods with System.Text.Encoding.ASCII.GetString() but none of them have worked

    I'm using VB.NET for this, thanks in advance.
     
  2. stevemk14ebr

    stevemk14ebr MDL Senior Member

    Jun 23, 2010
    267
    48
    10
    i have been trying to solve this for about an hr and its really stumped me, can u post ur process of converting to hex so i can have more to work with
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  3. Alphawaves

    Alphawaves Super Moderator/Developer
    Staff Member

    Aug 11, 2008
    5,886
    20,283
    180
    #3 Alphawaves, Apr 26, 2012
    Last edited by a moderator: Apr 20, 2017
    Hi, this should work:
    Code:
     Public Function GetRD(ByVal Path As String, ByVal Value As String) As String
            Dim str As String = String.Empty
            Dim Format As String = String.Empty
            Dim Data As Object = Registry.GetValue(Path, Value, Nothing)
            If Data Is Nothing Then Return "N/A"
            For j As Integer = LBound(Data) To UBound(Data)
                str = str & Hex(Data(j))
            Next
            str = BitConverter.ToString(Data).Replace("-", "")
            str = Regex.Replace(str, "(.{2})", "$0 ")
            For i As Integer = 0 To str.Length - 1 Step 1
                If i > 0 And i Mod 24 = 0 Then
                    Format &= Environment.NewLine
                End If
                Format &= str(i).ToString
            Next
            Return Format
        End Function
    Usage:
    Code:
    TextBox1.Text = GetRD("HKEY_LOCAL_MACHINE\Software\Microsoft\Windows NT\CurrentVersion", "DigitalProductId")
    ;)
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  4. QuantumBug

    QuantumBug MDL Developer

    Mar 7, 2012
    1,485
    1,321
    60
    Just testing it now, will let you know.
     
  5. QuantumBug

    QuantumBug MDL Developer

    Mar 7, 2012
    1,485
    1,321
    60
    Yes it worked perfectly first time!

    :worthy: Alphawaves
     
  6. Alphawaves

    Alphawaves Super Moderator/Developer
    Staff Member

    Aug 11, 2008
    5,886
    20,283
    180
    #6 Alphawaves, Apr 26, 2012
    Last edited by a moderator: Apr 20, 2017
    Ok didn't really need the values, this new function has values removed..

    Code:
     Public Function GetRD(ByVal Path As String, ByVal Value As String) As String
            Dim str As String = String.Empty
            Dim Format As String = String.Empty
            Dim Data As Object = Registry.GetValue(Path, Value, Nothing)
            If Data Is Nothing Then Return "N/A"
            For j As Integer = LBound(Data) To UBound(Data)
                str = str & Hex(Data(j))
            Next
            str = BitConverter.ToString(Data).Replace("-", "")
            str = Regex.Replace(str, "(.{2})", "$0 ")
            For i As Integer = 0 To str.Length - 1 Step 1
                If i > 0 And i Mod 24 = 0 Then
                    Format &= Environment.NewLine
                End If
                Format &= str(i).ToString
            Next
            Return Format
        End Function
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  7. QuantumBug

    QuantumBug MDL Developer

    Mar 7, 2012
    1,485
    1,321
    60
    Thanks a bunch man, just have to draw the letter spacing now so it looks tidy :)
     
  8. Alphawaves

    Alphawaves Super Moderator/Developer
    Staff Member

    Aug 11, 2008
    5,886
    20,283
    180
    Well im always here if you need help.;)
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  9. QuantumBug

    QuantumBug MDL Developer

    Mar 7, 2012
    1,485
    1,321
    60
    Good to know, you've always been a great help and I've learned a heck of a lot, since being on MDL I've done so much development it's unreal.

    As for the drawing of letter spacing, I used the cheap way and went with the Consolas font, another textbox is going next to it to display the wierdly formatted ProductID, it'll basically look like it does in the registry apart from the left hand side offsets.
     
  10. Alphawaves

    Alphawaves Super Moderator/Developer
    Staff Member

    Aug 11, 2008
    5,886
    20,283
    180
    If you want the right side data let me know..:D;)
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  11. QuantumBug

    QuantumBug MDL Developer

    Mar 7, 2012
    1,485
    1,321
    60
    I've had it working, now it's not.

    It was a simple HEX to ANSI. You decode the product key then view hex would enable allowing you to view it in a seperate form, and in the seperate from load event it would convert the HEX from RichTextBox1 to ANSI in RichTextBox2.

    If you have decent process to do it seperately for me to learn from that would be great.
     
  12. Alphawaves

    Alphawaves Super Moderator/Developer
    Staff Member

    Aug 11, 2008
    5,886
    20,283
    180
    #12 Alphawaves, Apr 27, 2012
    Last edited by a moderator: Apr 20, 2017
    Not had time today to look at this, i do however have a vbscript to display it in messagebox (should be simple enough to convert).
    Code:
    Const HKLM = &H80000002 
    Set oReg=GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\default:StdRegProv")
    sKeyPath = "SOFTWARE\Microsoft\Windows NT\CurrentVersion\"
    sValueName = "DigitalProductId"
    oReg.GetBinaryValue HKLM, sKeyPath, sValueName, sValue
    sContents = ""
    For i = 0 To UBound(sValue)
      vByte = sValue(i)
      If vByte <> 0 And vByte <> "" Then
        sContents = sContents & Chr(vByte)
      End If
    Next
    WScript.Echo sContents
    Ill have a look in a bit, have to go im real busy today..
    :eek: ;)
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  13. QuantumBug

    QuantumBug MDL Developer

    Mar 7, 2012
    1,485
    1,321
    60
    Haha I'll wait for you on this one, I've given up. Best I've got is HEX to Base64.
     
  14. Alphawaves

    Alphawaves Super Moderator/Developer
    Staff Member

    Aug 11, 2008
    5,886
    20,283
    180
    #14 Alphawaves, Apr 29, 2012
    Last edited: Apr 29, 2012
    lol i got the correct function here but only diplaying first char..:eek: :laie:

    encoding should be utf7 or default, all others display incorrectly

    Ill aslo have a look at converting the vbscript.

    Ill try again when im soba..:rofl:
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  15. QuantumBug

    QuantumBug MDL Developer

    Mar 7, 2012
    1,485
    1,321
    60
    omgwth that's what mine was doing also, ok that's fine :D I'm sitting down having a drink also, how I love my weekends.
     
  16. Alphawaves

    Alphawaves Super Moderator/Developer
    Staff Member

    Aug 11, 2008
    5,886
    20,283
    180
    #16 Alphawaves, Apr 29, 2012
    Last edited by a moderator: Apr 20, 2017
    Ok here is code :
    Code:
     Public Function PID()
            Dim Reg As Object = Registry.LocalMachine.OpenSubKey("SOFTWARE\Microsoft\Windows NT\CurrentVersion", False)
            Dim Data As Byte()
            Dim str As String = String.Empty
            Data = Reg.GetValue("DigitalProductId")
            Reg.Close()
            For j As Integer = 0 To UBound(Data)
                Dim byt As Byte = Data(j)
                If byt = 0 Then
                    byt = &H2E
                End If
                str = str & Chr(byt)
            Next
            str = Regex.Replace(str, "(.{1})", "$0 ")
            Return str
        End Function
    Usage:
    Code:
    TextBox1.Text = PID()
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  17. QuantumBug

    QuantumBug MDL Developer

    Mar 7, 2012
    1,485
    1,321
    60
    #17 QuantumBug, Apr 29, 2012
    Last edited by a moderator: Apr 20, 2017
    (OP)
    Jheez it's finally working, Alphawaves I don't know what I'd do without you, you're a star.

    For whoever uses this code, here is a quick edit to manually give the DigitalPID location as a string.

    Code:
        Public Function PID(ByVal Path As String, ByVal Value As String) As String
            Dim str As String = String.Empty
            Dim Data As Object = Registry.GetValue(Path, Value, Nothing)
            If Data Is Nothing Then Return "N/A"
            For j As Integer = 0 To UBound(Data)
                Dim byt As Byte = Data(j)
                If byt = 0 Then
                    byt = &H2E
                End If
                str = str & Chr(byt)
            Next
            str = Regex.Replace(str, "(.{1})", "$0 ")
            Return str
        End Function
    
    Usage:

    Code:
    Textbox1.Text = PID(keylocation.Text, "DigitalProductId")
    
    Or

    Code:
    Textbox1.Text = PID("HKEY_LOCAL_MACHINE\Software\Microsoft\Windows NT\CurrentVersion\", "DigitalProductId")
    
    keylocation.Text in my case is a string path to the Subkey DigitalProductId. As I'm constantly looking for DigitalProductId I added that myself, and the path string would read, for example: "HKEY_LOCAL_MACHINE\Software\Microsoft\Windows NT\CurrentVersion\, then DigitalProductId would be automatically appended onto the end.

    Big thanks to Alphawaves for helping me!
     
  18. Alphawaves

    Alphawaves Super Moderator/Developer
    Staff Member

    Aug 11, 2008
    5,886
    20,283
    180
    #18 Alphawaves, Apr 29, 2012
    Last edited by a moderator: Apr 20, 2017
    Cool dave, you dont need this line:
    Code:
    Dim Format As String = String.Empty
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  19. QuantumBug

    QuantumBug MDL Developer

    Mar 7, 2012
    1,485
    1,321
    60
    #19 QuantumBug, Apr 29, 2012
    Last edited by a moderator: Apr 20, 2017
    (OP)
    Didn't think I did as it doesn't get used anywhere. Already removed it in my app.

    Thanks again :D Hope you enjoyed your night also!