views:

73

answers:

2

Hello experts,

How can I convert a PSID type into a byte array that contains the byte value of the SID? Something like:

PSID pSid;
byte sidBytes[68];//Max. length of SID in bytes is 68
 if(GetAccountSid(
        NULL,       // default lookup logic
        AccountName,// account to obtain SID
        &pSid       // buffer to allocate to contain resultant SID
        )

{

ConvertPSIDToByteArray(pSid, sidBytes);

}

--how should I write the function ConvertPSIDToByteArray?

Thank you!

A: 

I think the function you might be looking for is ConvertSidToStringSid. The general idea is to convert the PSID struct to a LPTSTR which is in fact of type wchar_t. You can then convert this using standard functions to a multi-byte char array using wcstombs which will then give you the SID in bytes. Alternatively, you can operate on the wchar_t type directly and just write that out - there are functions for handling that. In either case, the result will be UTF-16 LE encoded and if you need to change from that you'll have to do a conversion.

Ninefingers
Looking here:http://www.secnewsgroups.net/group/microsoft.public.dotnet.security/topic10882.aspxI understood that SID string's length is not the byte array's length:(68)-- pure binary(136) -- (68*2) = hexString(184) -- SID StringSo I want the shortest form...
rursw1
+1  A: 

Use the GetLengthSid() to get the number of bytes you'll need. Then memcpy() from the PSID.

Hans Passant