使用convertToString:
protected String convertToString(byte[] bytes)
{
/*
* The binary data structure, from http://msdn.microsoft.com/en-us/library/cc230371(PROT.10).aspx:
* byte[0] - Revision (1 byte): An 8-bit unsigned integer that specifies the revision level of the SID structure. This value MUST be set to 0x01.
* byte[1] - SubAuthorityCount (1 byte): An 8-bit unsigned integer that specifies the number of elements in the SubAuthority array. The maximum number of elements allowed is 15.
* byte[2-7] - IdentifierAuthority (6 bytes): A SID_IDENTIFIER_AUTHORITY structure that contains information, which indicates the authority under which the SID was created. It describes the entity that created the SID and manages the account.
* Six element arrays of 8-bit unsigned integers that specify the top-level authority
* big-endian!
* and then - SubAuthority (variable): A variable length array of unsigned 32-bit integers that uniquely identifies a principal relative to the IdentifierAuthority. Its length is determined by SubAuthorityCount.
* little-endian!
*/
if (bytes == null || bytes.length < 8)
{
return Messages.getString("InPlaceMsAdObjectSidValueEditor.InvalidSid"); //$NON-NLS-1$
}
char[] hex = Hex.encodeHex(bytes);
StringBuffer sb = new StringBuffer();
// start with 'S'
sb.append('S');
// revision
int revision = Integer.parseInt(new String(hex, 0, 2), 16);
sb.append('-');
sb.append(revision);
// get count
int count = Integer.parseInt(new String(hex, 2, 2), 16);
// check length
if (bytes.length != (8 + count * 4))
{
return Messages.getString("InPlaceMsAdObjectSidValueEditor.InvalidSid"); //$NON-NLS-1$
}
// get authority, big-endian
long authority = Long.parseLong(new String(hex, 4, 12), 16);
sb.append('-');
sb.append(authority);
// sub-authorities, little-endian
for (int i = 0; i < count; i++)
{
StringBuffer rid = new StringBuffer();
for (int k = 3; k >= 0; k--)
{
rid.append(hex[16 + (i * 8) + (k * 2)]);
rid.append(hex[16 + (i * 8) + (k * 2) + 1]);
}
long subAuthority = Long.parseLong(rid.toString(), 16);
sb.append('-');
sb.append(subAuthority);
}
return sb.toString();
}
(從Apache目錄工作室)
完全正確,感謝名單!我以另一種方式做了它,但你對convertSidToStringSid和convertStringSidToSid的提示真的很有幫助!博客也很有幫助。 – mtm