Converting a SID in Array of bytes to String version in VB.Net

This is a program that converts SID’s with 1 – 5 sub Authorities  , from Array of bytes to the String Format

In my last post I was looking for a way to convert the array of bytes ,

(SID: 1,2,0,0,0,0,0,5,32,0,0,0,32,2,0,0,)

To the normal string format , (SIDString : S-1-5-32-544)

Most of the code I found on the net requires you to to be on the machine that has the SID you want to convert. This Program is a Stand Alone program and can convert any SID in array of bytes to the String Format with out the need to access the system for the information. (only tested on Vista 64 bit at the moment)

I went back to the selfadsi.org  site and looked at  the conversion code they have listed there .

After I stopped trying to figure out why it worked and just started trying to figure out how to get it to work for me, things started going a little smoother. The code does work, you just have to apply it to what you want.

Below is the finished program: This is how it looks when you first open it.

SidConverter1

This Next screen shot shows the SID In Array of bytes inserted in the text box.

It then clears the smaller text boxes with an on change event.

SidConverter2

After Running it you get this:

SIDConverter3

This SID is Actually

(Group Account : SQLServer2005MSSQLServerADHelperUser$VISTA-PC)

When you first insert the Array of bytes into the textbox it Checks the Sub ID Count and then Verifies the amount of bytes in the array, if the number of bytes  does not work out to what it should be then and message box pops up and lets you know. (See the Code It will be clearer)

If you have a trailing comma then it will strip that out first just to avoid any problems.

Lest say though you have an account that only has 2 Sub Authority’s then what.

This program handles that too. See Below.

SIDConverter4

This SID  Here is for the built in Guest account. As you can see it only has 2 Sub ID’s.

When I started building this program I started by making it just for what I needed it for, to convert a SID with 5 subs. But as I was going thru it I decided why not make it possible to convert from 1 – 5 sub ID’s. I’ve actually only seen  1,2,4,5 in my research using the tool I built in the last Post.

The Code:

Imports System.Text Public Class Form1 Private Sub btnConvert_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnConvert.Click Try Dim b1, b2 As String Dim RawSid As String RawSid = tbInputString.Text Dim SidStrCol As New Collection Dim TrmdRawSID As String = RawSid.TrimEnd(,) Dim stary() As String = Split(TrmdRawSID, ,, 1) For Each strByte In stary SidStrCol.Add(strByte) Next b1 = SidStrCol(1) Revision Number 1 byte b2 = SidStrCol(2) Sub ID Count 1 byte If b2 = 1 Then sCount1() ElseIf b2 = 2 Then sCount2() ElseIf b2 = 3 Then sCount3() ElseIf b2 = 4 Then sCount4() ElseIf b2 = 5 Then sCount5() ElseIf b2 > 5 Then MsgBox(This program only works for SID’s with 1-5 Sub Authority’s & vbNewLine & _ Your Sub ID = & b2.ToString, MsgBoxStyle.Information, Sub ID Count To High) End If Catch ex As Exception If tbInputString.Text = Nothing Then MsgBox(Please insert the SID to Convert, MsgBoxStyle.Information, Error, Missing SID) Else MsgBox(ex.Message, MsgBoxStyle.Information, Some Kind Of Error Happened) End If End Try End Sub Private Sub sCount1() Try Dim b1, b2, b3, b4, b5, b6, b7, b8, b9, b10, b11, b12 As String Dim RawSid As String RawSid = tbInputString.Text Dim SidStrCol As New Collection Dim TrmdRawSID As String = RawSid.TrimEnd(,) Dim stary() As String = Split(TrmdRawSID, ,, 1) For Each strByte In stary SidStrCol.Add(strByte) Next b1 = SidStrCol(1) Revision Number 1 byte b2 = SidStrCol(2) Sub ID Count 1 byte Satrt Auth. b3 = SidStrCol(3) SID_IDENTIFIER_AUTHORITY 6 bytes b4 = SidStrCol(4) 2 b5 = SidStrCol(5) 3 b6 = SidStrCol(6) 4 b7 = SidStrCol(7) 5 b8 = SidStrCol(8) 6 Start Sub 1 b9 = SidStrCol(9) Sub Authority 1 4 bytes b10 = SidStrCol(10) 2 b11 = SidStrCol(11) 3 b12 = SidStrCol(12) 4 Everything before here is the minimum that will go in Will need to check the sub id count after this. tbRevision.Text = b1 tbSubIDCount.Text = b2 Text boxes SID_IDENTIFIER_AUTHORITY tbIA1.Text = b3 tbIA2.Text = b4 tbIA3.Text = b5 tbIA4.Text = b6 tbIA5.Text = b7 tbIA6.Text = b8 Text boxes sub 1 tb1ID1.Text = b9 tb1ID2.Text = b10 tb1ID3.Text = b11 tb1ID4.Text = b12 Text boxes Sub 2 tb2ID1.Text = x tb2ID2.Text = x tb2ID3.Text = x tb2ID4.Text = x Text boxes Sub 3 tb3ID1.Text = x tb3ID2.Text = x tb3ID3.Text = x tb3ID4.Text = x Text boxes Sub 4 tb4ID1.Text = x tb4ID2.Text = x tb4ID3.Text = x tb4ID4.Text = x Tex boxes Sub 5 tb5ID1.Text = x tb5ID2.Text = x tb5ID3.Text = x tb5ID4.Text = x IA Math Dim strIAMath As String strIAMath = b3 strIAMath = strIAMath * 256 + b4 strIAMath = strIAMath * 256 + b5 strIAMath = strIAMath * 256 + b6 strIAMath = strIAMath * 256 + b7 strIAMath = strIAMath * 256 + b8 Dim Sub1Math As String Sub1Math = b12 Starts From The Right Sub1Math = (Sub1Math * 256) + b11 Sub1Math = (Sub1Math * 256) + b10 Sub1Math = (Sub1Math * 256) + b9 Dim OutputString As String OutputString = (S- & b1 & & strIAMath & & Sub1Math) tbOutputString.Text = OutputString Catch ex As Exception MsgBox(ex.Message, MsgBoxStyle.Information, Some Kind Of Error Happened) End Try End Sub Private Sub sCount2() Try Dim b1, b2, b3, b4, b5, b6, b7, b8, b9, b10, b11, b12, b13, b14, b15, b16 As String Dim RawSid As String RawSid = tbInputString.Text Dim SidStrCol As New Collection Dim TrmdRawSID As String = RawSid.TrimEnd(,) Dim stary() As String = Split(TrmdRawSID, ,, 1) For Each strByte In stary SidStrCol.Add(strByte) Next b1 = SidStrCol(1) Revision Number 1 byte b2 = SidStrCol(2) Sub ID Count 1 byte Satrt Auth. b3 = SidStrCol(3) SID_IDENTIFIER_AUTHORITY 6 bytes b4 = SidStrCol(4) 2 b5 = SidStrCol(5) 3 b6 = SidStrCol(6) 4 b7 = SidStrCol(7) 5 b8 = SidStrCol(8) 6 Start Sub 1 b9 = SidStrCol(9) Sub Authority 1 4 bytes b10 = SidStrCol(10) 2 b11 = SidStrCol(11) 3 b12 = SidStrCol(12) 4 Everything before here is the minimum that will go in Will need to check the sub id count after this. b13 = SidStrCol(13) Sub Authority 2 4 bytes b14 = SidStrCol(14) 2 b15 = SidStrCol(15) 3 b16 = SidStrCol(16) 4 Sub ID count = 2 All above tbRevision.Text = b1 tbSubIDCount.Text = b2 Text boxes SID_IDENTIFIER_AUTHORITY tbIA1.Text = b3 tbIA2.Text = b4 tbIA3.Text = b5 tbIA4.Text = b6 tbIA5.Text = b7 tbIA6.Text = b8 Text boxes sub 1 tb1ID1.Text = b9 tb1ID2.Text = b10 tb1ID3.Text = b11 tb1ID4.Text = b12 Text boxes Sub 2 tb2ID1.Text = b13 tb2ID2.Text = b14 tb2ID3.Text = b15 tb2ID4.Text = b16 Text boxes Sub 3 tb3ID1.Text = x tb3ID2.Text = x tb3ID3.Text = x tb3ID4.Text = x Text boxes Sub 4 tb4ID1.Text = x tb4ID2.Text = x tb4ID3.Text = x tb4ID4.Text = x Tex boxes Sub 5 tb5ID1.Text = x tb5ID2.Text = x tb5ID3.Text = x tb5ID4.Text = x IA Math Dim strIAMath As String strIAMath = b3 strIAMath = strIAMath * 256 + b4 strIAMath = strIAMath * 256 + b5 strIAMath = strIAMath * 256 + b6 strIAMath = strIAMath * 256 + b7 strIAMath = strIAMath * 256 + b8 Dim Sub1Math As String Sub1Math = b12 Starts From The Right Sub1Math = (Sub1Math * 256) + b11 Sub1Math = (Sub1Math * 256) + b10 Sub1Math = (Sub1Math * 256) + b9 Dim Sub2Math As String Sub2Math = b16 Starts From The Right Sub2Math = (Sub2Math * 256) + b15 Sub2Math = (Sub2Math * 256) + b14 Sub2Math = (Sub2Math * 256) + b13 Dim OutputString As String OutputString = (S- & b1 & & strIAMath & & Sub1Math & & Sub2Math) tbOutputString.Text = OutputString Catch ex As Exception MsgBox(ex.Message, MsgBoxStyle.Information, Some Kind Of Error Happened) End Try End Sub Private Sub sCount3() Try Dim b1, b2, b3, b4, b5, b6, b7, b8, b9, b10, b11, b12, b13, b14, b15, b16, b17, b18, b19, b20 As String Dim RawSid As String RawSid = tbInputString.Text Dim SidStrCol As New Collection Dim TrmdRawSID As String = RawSid.TrimEnd(,) Dim stary() As String = Split(TrmdRawSID, ,, 1) For Each strByte In stary SidStrCol.Add(strByte) Next b1 = SidStrCol(1) Revision Number 1 byte b2 = SidStrCol(2) Sub ID Count 1 byte Satrt Auth. b3 = SidStrCol(3) SID_IDENTIFIER_AUTHORITY 6 bytes b4 = SidStrCol(4) 2 b5 = SidStrCol(5) 3 b6 = SidStrCol(6) 4 b7 = SidStrCol(7) 5 b8 = SidStrCol(8) 6 Start Sub 1 b9 = SidStrCol(9) Sub Authority 1 4 bytes b10 = SidStrCol(10) 2 b11 = SidStrCol(11) 3 b12 = SidStrCol(12) 4 Everything before here is the minimum that will go in Will need to check the sub id count after this. b13 = SidStrCol(13) Sub Authority 2 4 bytes b14 = SidStrCol(14) 2 b15 = SidStrCol(15) 3 b16 = SidStrCol(16) 4 Sub ID count = 2 All above b17 = SidStrCol(17) Sub Authority 3 4 bytes b18 = SidStrCol(18) 2 b19 = SidStrCol(19) 3 b20 = SidStrCol(20) 4 Sub ID Count = 3 All Above tbRevision.Text = b1 tbSubIDCount.Text = b2 Text boxes SID_IDENTIFIER_AUTHORITY tbIA1.Text = b3 tbIA2.Text = b4 tbIA3.Text = b5 tbIA4.Text = b6 tbIA5.Text = b7 tbIA6.Text = b8 Text boxes sub 1 tb1ID1.Text = b9 tb1ID2.Text = b10 tb1ID3.Text = b11 tb1ID4.Text = b12 Text boxes Sub 2 tb2ID1.Text = b13 tb2ID2.Text = b14 tb2ID3.Text = b15 tb2ID4.Text = b16 Text boxes Sub 3 tb3ID1.Text = b17 tb3ID2.Text = b18 tb3ID3.Text = b19 tb3ID4.Text = b20 Text boxes Sub 4 tb4ID1.Text = x tb4ID2.Text = x tb4ID3.Text = x tb4ID4.Text = x Tex boxes Sub 5 tb5ID1.Text = x tb5ID2.Text = x tb5ID3.Text = x tb5ID4.Text = x IA Math Dim strIAMath As String strIAMath = b3 strIAMath = strIAMath * 256 + b4 strIAMath = strIAMath * 256 + b5 strIAMath = strIAMath * 256 + b6 strIAMath = strIAMath * 256 + b7 strIAMath = strIAMath * 256 + b8 Dim Sub1Math As String Sub1Math = b12 Starts From The Right Sub1Math = (Sub1Math * 256) + b11 Sub1Math = (Sub1Math * 256) + b10 Sub1Math = (Sub1Math * 256) + b9 Dim Sub2Math As String Sub2Math = b16 Starts From The Right Sub2Math = (Sub2Math * 256) + b15 Sub2Math = (Sub2Math * 256) + b14 Sub2Math = (Sub2Math * 256) + b13 Dim Sub3Math As String Sub3Math = b20 Starts From The Right Sub3Math = Sub3Math * 256 + b19 Sub3Math = Sub3Math * 256 + b18 Sub3Math = Sub3Math * 256 + b17 Dim OutputString As String OutputString = (S- & b1 & & strIAMath & & Sub1Math & & Sub2Math & & Sub3Math) tbOutputString.Text = OutputString Catch ex As Exception MsgBox(ex.Message, MsgBoxStyle.Information, Some Kind Of Error Happened) End Try End Sub Private Sub sCount4() Try Dim b1, b2, b3, b4, b5, b6, b7, b8, b9, b10, b11, b12, b13, b14, b15, b16, b17, b18, b19, b20, b21, b22, b23, b24 As String Dim RawSid As String RawSid = tbInputString.Text Dim SidStrCol As New Collection Dim TrmdRawSID As String = RawSid.TrimEnd(,) Dim stary() As String = Split(TrmdRawSID, ,, 1) For Each strByte In stary SidStrCol.Add(strByte) Next b1 = SidStrCol(1) Revision Number 1 byte b2 = SidStrCol(2) Sub ID Count 1 byte Satrt Auth. b3 = SidStrCol(3) SID_IDENTIFIER_AUTHORITY 6 bytes b4 = SidStrCol(4) 2 b5 = SidStrCol(5) 3 b6 = SidStrCol(6) 4 b7 = SidStrCol(7) 5 b8 = SidStrCol(8) 6 Start Sub 1 b9 = SidStrCol(9) Sub Authority 1 4 bytes b10 = SidStrCol(10) 2 b11 = SidStrCol(11) 3 b12 = SidStrCol(12) 4 Everything before here is the minimum that will go in Will need to check the sub id count after this. b13 = SidStrCol(13) Sub Authority 2 4 bytes b14 = SidStrCol(14) 2 b15 = SidStrCol(15) 3 b16 = SidStrCol(16) 4 Sub ID count = 2 All above b17 = SidStrCol(17) Sub Authority 3 4 bytes b18 = SidStrCol(18) 2 b19 = SidStrCol(19) 3 b20 = SidStrCol(20) 4 Sub ID Count = 3 All Above b21 = SidStrCol(21) Sub Authority 4 4 bytes b22 = SidStrCol(22) 2 b23 = SidStrCol(23) 3 b24 = SidStrCol(24) 4 Sub ID Count = 4 All Above tbRevision.Text = b1 tbSubIDCount.Text = b2 Text boxes SID_IDENTIFIER_AUTHORITY tbIA1.Text = b3 tbIA2.Text = b4 tbIA3.Text = b5 tbIA4.Text = b6 tbIA5.Text = b7 tbIA6.Text = b8 Text boxes sub 1 tb1ID1.Text = b9 tb1ID2.Text = b10 tb1ID3.Text = b11 tb1ID4.Text = b12 Text boxes Sub 2 tb2ID1.Text = b13 tb2ID2.Text = b14 tb2ID3.Text = b15 tb2ID4.Text = b16 Text boxes Sub 3 tb3ID1.Text = b17 tb3ID2.Text = b18 tb3ID3.Text = b19 tb3ID4.Text = b20 Text boxes Sub 4 tb4ID1.Text = b21 tb4ID2.Text = b22 tb4ID3.Text = b23 tb4ID4.Text = b24 Tex boxes Sub 5 tb5ID1.Text = x tb5ID2.Text = x tb5ID3.Text = x tb5ID4.Text = x IA Math Dim strIAMath As String strIAMath = b3 strIAMath = strIAMath * 256 + b4 strIAMath = strIAMath * 256 + b5 strIAMath = strIAMath * 256 + b6 strIAMath = strIAMath * 256 + b7 strIAMath = strIAMath * 256 + b8 Dim Sub1Math As String Sub1Math = b12 Starts From The Right Sub1Math = (Sub1Math * 256) + b11 Sub1Math = (Sub1Math * 256) + b10 Sub1Math = (Sub1Math * 256) + b9 Dim Sub2Math As String Sub2Math = b16 Starts From The Right Sub2Math = (Sub2Math * 256) + b15 Sub2Math = (Sub2Math * 256) + b14 Sub2Math = (Sub2Math * 256) + b13 Dim Sub3Math As String Sub3Math = b20 Starts From The Right Sub3Math = Sub3Math * 256 + b19 Sub3Math = Sub3Math * 256 + b18 Sub3Math = Sub3Math * 256 + b17 Dim Sub4Math As String Sub4Math = b24 Starts From The Right Sub4Math = Sub4Math * 256 + b23 Sub4Math = Sub4Math * 256 + b22 Sub4Math = Sub4Math * 256 + b21 Dim OutputString As String OutputString = (S- & b1 & & strIAMath & & Sub1Math & & Sub2Math & & Sub3Math & & Sub4Math) tbOutputString.Text = OutputString Catch ex As Exception MsgBox(ex.Message, MsgBoxStyle.Information, Some Kind Of Error Happened) End Try End Sub Private Sub sCount5() Try Dim b1, b2, b3, b4, b5, b6, b7, b8, b9, b10, b11, b12, b13, b14, b15, b16, b17, b18, b19, b20, b21, b22, b23, b24, b25, b26, b27, b28 As String Dim RawSid As String RawSid = tbInputString.Text Dim SidStrCol As New Collection Dim TrmdRawSID As String = RawSid.TrimEnd(,) Dim stary() As String = Split(TrmdRawSID, ,, 1) For Each strByte In stary SidStrCol.Add(strByte) Next b1 = SidStrCol(1) Revision Number 1 byte b2 = SidStrCol(2) Sub ID Count 1 byte Satrt Auth. b3 = SidStrCol(3) SID_IDENTIFIER_AUTHORITY 6 bytes b4 = SidStrCol(4) 2 b5 = SidStrCol(5) 3 b6 = SidStrCol(6) 4 b7 = SidStrCol(7) 5 b8 = SidStrCol(8) 6 Start Sub 1 b9 = SidStrCol(9) Sub Authority 1 4 bytes b10 = SidStrCol(10) 2 b11 = SidStrCol(11) 3 b12 = SidStrCol(12) 4 Everything before here is the minimum that will go in Will need to check the sub id count after this. b13 = SidStrCol(13) Sub Authority 2 4 bytes b14 = SidStrCol(14) 2 b15 = SidStrCol(15) 3 b16 = SidStrCol(16) 4 Sub ID count = 2 All above b17 = SidStrCol(17) Sub Authority 3 4 bytes b18 = SidStrCol(18) 2 b19 = SidStrCol(19) 3 b20 = SidStrCol(20) 4 Sub ID Count = 3 All Above b21 = SidStrCol(21) Sub Authority 4 4 bytes b22 = SidStrCol(22) 2 b23 = SidStrCol(23) 3 b24 = SidStrCol(24) 4 Sub ID Count = 4 All Above b25 = SidStrCol(25) Sub Authority 5 (RID) 4 bytes b26 = SidStrCol(26) 2 b27 = SidStrCol(27) 3 b28 = SidStrCol(28) 4 (Last byte in the array / Collection) Sub ID Count = 5 then All 28 Bytes will be used. tbRevision.Text = b1 tbSubIDCount.Text = b2 Text boxes SID_IDENTIFIER_AUTHORITY tbIA1.Text = b3 tbIA2.Text = b4 tbIA3.Text = b5 tbIA4.Text = b6 tbIA5.Text = b7 tbIA6.Text = b8 Text boxes sub 1 tb1ID1.Text = b9 tb1ID2.Text = b10 tb1ID3.Text = b11 tb1ID4.Text = b12 Text boxes Sub 2 tb2ID1.Text = b13 tb2ID2.Text = b14 tb2ID3.Text = b15 tb2ID4.Text = b16 Text boxes Sub 3 tb3ID1.Text = b17 tb3ID2.Text = b18 tb3ID3.Text = b19 tb3ID4.Text = b20 Text boxes Sub 4 tb4ID1.Text = b21 tb4ID2.Text = b22 tb4ID3.Text = b23 tb4ID4.Text = b24 Tex boxes Sub 5 tb5ID1.Text = b25 tb5ID2.Text = b26 tb5ID3.Text = b27 tb5ID4.Text = b28 IA Math Dim strIAMath As String strIAMath = b3 strIAMath = strIAMath * 256 + b4 strIAMath = strIAMath * 256 + b5 strIAMath = strIAMath * 256 + b6 strIAMath = strIAMath * 256 + b7 strIAMath = strIAMath * 256 + b8 Dim Sub1Math As String Sub1Math = b12 Starts From The Right Sub1Math = (Sub1Math * 256) + b11 Sub1Math = (Sub1Math * 256) + b10 Sub1Math = (Sub1Math * 256) + b9 Dim Sub2Math As String Sub2Math = b16 Starts From The Right Sub2Math = (Sub2Math * 256) + b15 Sub2Math = (Sub2Math * 256) + b14 Sub2Math = (Sub2Math * 256) + b13 Dim Sub3Math As String Sub3Math = b20 Starts From The Right Sub3Math = Sub3Math * 256 + b19 Sub3Math = Sub3Math * 256 + b18 Sub3Math = Sub3Math * 256 + b17 Dim Sub4Math As String Sub4Math = b24 Starts From The Right Sub4Math = Sub4Math * 256 + b23 Sub4Math = Sub4Math * 256 + b22 Sub4Math = Sub4Math * 256 + b21 Dim sub5Math As String sub5Math = b28 Starts From The Right sub5Math = sub5Math * 256 + b27 sub5Math = sub5Math * 256 + b26 sub5Math = sub5Math * 256 + b25 Dim OutputString As String OutputString = (S- & b1 & & strIAMath & & Sub1Math & & Sub2Math & & Sub3Math & & Sub4Math & & sub5Math) tbOutputString.Text = OutputString Catch ex As Exception MsgBox(ex.Message, MsgBoxStyle.Information, Some Kind Of Error Happened) End Try End Sub Private Sub lblAbout_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lblAbout.Click AboutBox1.Show() End Sub Private Sub lnklblSelfAdsiorg_LinkClicked(ByVal sender As System.Object, ByVal e As System.Windows.Forms.LinkLabelLinkClickedEventArgs) Handles lnklblSelfAdsiorg.LinkClicked Process.Start(Iexplore, http://www.selfadsi.org/deep-inside/microsoft-sid-attributes.htm) End Sub Private Sub tbInputString_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tbInputString.TextChanged Try tbOutputString.Clear() tbRevision.Clear() tbSubIDCount.Clear() tbIA1.Clear() tbIA2.Clear() tbIA3.Clear() tbIA4.Clear() tbIA5.Clear() tbIA6.Clear() Text boxes sub 1 tb1ID1.Clear() tb1ID2.Clear() tb1ID3.Clear() tb1ID4.Clear() Text boxes Sub 2 tb2ID1.Clear() tb2ID2.Clear() tb2ID3.Clear() tb2ID4.Clear() Text boxes Sub 3 tb3ID1.Clear() tb3ID2.Clear() tb3ID3.Clear() tb3ID4.Clear() Text boxes Sub 4 tb4ID1.Clear() tb4ID2.Clear() tb4ID3.Clear() tb4ID4.Clear() Tex boxes Sub 5 tb5ID1.Clear() tb5ID2.Clear() tb5ID3.Clear() tb5ID4.Clear() Dim b1, b2 As String Dim RawSid As String RawSid = tbInputString.Text Dim SidStrCol As New Collection Dim TrmdRawSID As String = RawSid.TrimEnd(,) Dim stary() As String = Split(TrmdRawSID, ,, 1) For Each strByte In stary SidStrCol.Add(strByte) Next Dim colcnt As Integer colcnt = SidStrCol.Count b1 = SidStrCol(1) Revision Number 1 byte b2 = SidStrCol(2) Sub ID Count 1 byte lblArrayCount.Text = SID Length = & colcnt.ToString Below Checks the the proper amount of bytes are present for the given SID Sub Count If b2 = 1 And colcnt <> 12 Then MsgBox(Please Double Check your Input String Length & vbNewLine & Sub Id Count Should be 1 and Byte count should be 12, MsgBoxStyle.Information, Input Verification Error 1) ElseIf b2 = 2 And colcnt <> 16 Then MsgBox(Please Double Check your Input String Length & vbNewLine & Sub Id Count Should be 2 and Byte count should be 16, MsgBoxStyle.Information, Input Verification Error 2) ElseIf b2 = 3 And colcnt <> 20 Then MsgBox(Please Double Check your Input String Length & vbNewLine & Sub Id Count Should be 3 and Byte count should be 20, MsgBoxStyle.Information, Input Verification Error 3) ElseIf b2 = 4 And colcnt <> 24 Then MsgBox(Please Double Check your Input String Length & vbNewLine & Sub Id Count Should be 4 and Byte count should be 24, MsgBoxStyle.Information, Input Verification Error 4) ElseIf b2 = 5 And colcnt <> 28 Then MsgBox(Please Double Check your Input String Length & vbNewLine & Sub Id Count Should be 5 and Byte count should be 28, MsgBoxStyle.Information, Input Verification Error 5) ElseIf colcnt > 28 Then MsgBox(Please Double Check your Input String Length & vbNewLine & Sub Id Count Should be 5 or less and Byte count should be 28 or less, MsgBoxStyle.Information, Input Verification Error To Many Bytes) End If Catch ex As Exception MsgBox(ex.Message, MsgBoxStyle.Information, Some Error happened while Checking SID Length) End Try End Sub End Class

In the First part of the button click. If there is a comma on the end then it gets trimmed right from the start. Then we split the trimmed result and add it to a collection, it was actually easier than an array, also an article said there may be a performance gain using the collection rather than the array. Once we have the collection we set the b1, and b2 as string then make b1 = to the first byte in the collection and make b2 = to the second byte in the collection.Then we check what b2 actually equals,  If it  Equals 1-5 then we move thru the ElseIf Statement to get to the “SUB” that is equal to the b2  byte. Anything else than 1-5 Should be handled with the text box validation . I like using an ElseIf  others may like the select case statement.

When I started this project I just had code to handle all 5 sub authority ID’s in my button click event, then moved it to sub 5 later. I had to start working backwards from all 5 to 1 . Basically what I did was to create 5 “Subs” to handle just the code for that Sub Number 1-5. Then working backwards stripped out the code that didn’t need to be there to  handle just that version and to put x’s into the unused boxes on the form. I don’t know if there is a fancy way to do this  with less code but this is pretty straight forward and easy to debug as is. Viewing the code should explain it better than I can write it out, I tried to comment it as much as possible .

As I build an application I try to think of every way a user can break it and handle any possible errors. I’m pretty sure this one has that covered. Also as I’m building I like to verify the information I’m pulling out, some times I leave the the boxes in place and make them a part of the finished program as I did here. (The small Boxes that each byte goes into)

Download:

This Program will be available for download from my SkyDrive . Since They don’t do the Direct Download Link any more you have to go to the folder and select the file or files you want. Everything is in zip format.File name is (ConvertSIDByteArayToSIDStirng.zip)

Minimum requirements for all of my programs , that I know of, is just dot Net framework 3.5 (and Lower ).

You can also View Information on the other files in the SkyDrive folder

As the usual way thing goes, these program are free and as is with no warranty. I just ask that anyone download directly from my Site. I do update them from time to time. Also if anyone finds a bug please let me know at. pcsxcetra@consolidated.net so I can fix them and post the updated version.

Hope this was informative and the tool(s) useful, in a good way.

Edit: Removed links to site I no longer Have. and changed contact email.

Advertisements

About pcsxcetrasupport3

My part time Business, I mainly do system building and system repair. Over the last several years I have been building system utility's in vb script , HTA applications and VB.Net to be able to better find the information I need to better understand the systems problems in order to get the systems repaired and back to my customers quicker.
This entry was posted in CodeProject, System Tools, VB.net and tagged , , . Bookmark the permalink.

One Response to Converting a SID in Array of bytes to String version in VB.Net

  1. Richard says:

    The System.Security.Principal.SecurityIdentifier class will translate between a byte array and SDDL-form SID for you. It will even let you translate betweens SIDs and account names.

    // Convert byte array to SDDL:
    var bytes = new byte[] { 1, 2, 0, 0, 0, 0, 0, 5, 32, 0, 0, 0, 32, 2, 0, 0 };
    var sid = new SecurityIdentifier(bytes, 0);
    Console.WriteLine(sid.Value); // S-1-5-32-544

    // Convert SDDL to byte array:
    var sid2 = new SecurityIdentifier(“S-1-5-32-544”);
    var bytes2 = new byte[sid2.BinaryLength];
    sid2.GetBinaryForm(bytes2, 0);

    foreach (byte b in bytes2)
    {
    Console.Write(“{0}, “, b);
    }
    Console.WriteLine();
    // 1, 2, 0, 0, 0, 0, 0, 5, 32, 0, 0, 0, 32, 2, 0, 0,

    // Translate SID to account:
    var account = sid.Translate(typeof(NTAccount));
    Console.WriteLine(account.Value); // BUILTIN\Administrators

Comments are closed.