0

I've been testing everything I find for a couple of days to correctly decode the objectGUID. I try to obtain an Object later converting it to a Byte [], I am not sure if it is the correct process to decode it and obtain the value as it is shown in the Active Directory since I tested but at the time of decoding it shows a different value to the which appears in its respective user in AD. If someone could guide me to have the right process by which I will get the right value or can briefly explain what I am doing wrong, I am open to any solution.

SearchControls searchControls = new SearchControls();
        searchControls.setSearchScope(SearchControls.SUBTREE_SCOPE);
        NamingEnumeration<SearchResult> results = ctx.search(ldapSearchBase, searchFilter, searchControls);
//atributos []  contains the names of the attributes that I need to consult

        while(results.hasMoreElements()){
            SearchResult searchResult = (SearchResult) results.next();
            Attributes attributes = searchResult.getAttributes();            
            for (int i = 0;i<=atributos.length;i++){
                if(i<atributos.length){
                    if(atributos[i].equals("objectGUID")){
                        try {
                           Object c = attributes.get("objectGUID").get();
                           decodeEntryUUID(c);                           
                           content.put(attributes.get(atributos[i]).getID(),decodeEntryUUID(c));
                         }catch (NullPointerException e){
                              System.err.println("Problem listing attributes: " + e);
                         }
public String decodeEntryUUID(Object entryUUID) throws IOException {
        String ret = "x";    
        ret=decodeGuid(serializeObject(entryUUID));        
        return ret;
    }

    public static String decodeGuid(byte[] guid) {
        byte[] withBigEndian = new byte[] { guid[3], guid[2], guid[1], guid[0],
            guid[5], guid[4],
            guid[7], guid[6],
            guid[8], guid[9], guid[10], guid[11], guid[12], guid[13], guid[14], guid[15]
        };
        return convertToDashedString(withBigEndian);
    }

    public static String decodeObjectGUID(byte[] objectGUID) {
        StringBuilder displayStr = new StringBuilder();

        displayStr.append(convertToDashedString(objectGUID));
        System.out.println(displayStr.toString()+" 3");
        return displayStr.toString();
    }
     private static String convertToDashedString(byte[] objectGUID) {
        StringBuilder displayStr = new StringBuilder();

        displayStr.append(prefixZeros((int) objectGUID[3] & 0xFF));
        displayStr.append(prefixZeros((int) objectGUID[2] & 0xFF));
        displayStr.append(prefixZeros((int) objectGUID[1] & 0xFF));
        displayStr.append(prefixZeros((int) objectGUID[0] & 0xFF));
        displayStr.append("-");
        displayStr.append(prefixZeros((int) objectGUID[5] & 0xFF));
        displayStr.append(prefixZeros((int) objectGUID[4] & 0xFF));
        displayStr.append("-");
        displayStr.append(prefixZeros((int) objectGUID[7] & 0xFF));
        displayStr.append(prefixZeros((int) objectGUID[6] & 0xFF));
        displayStr.append("-");
        displayStr.append(prefixZeros((int) objectGUID[8] & 0xFF));
        displayStr.append(prefixZeros((int) objectGUID[9] & 0xFF));
        displayStr.append("-");
        displayStr.append(prefixZeros((int) objectGUID[10] & 0xFF));
        displayStr.append(prefixZeros((int) objectGUID[11] & 0xFF));
        displayStr.append(prefixZeros((int) objectGUID[12] & 0xFF));
        displayStr.append(prefixZeros((int) objectGUID[13] & 0xFF));
        displayStr.append(prefixZeros((int) objectGUID[14] & 0xFF));
        displayStr.append(prefixZeros((int) objectGUID[15] & 0xFF));
        return displayStr.toString();
    }
     private static String prefixZeros(int value) {
        if (value <= 0xF) {
            StringBuilder sb = new StringBuilder("0");
            sb.append(Integer.toHexString(value));
            return sb.toString();
        } else {
            return Integer.toHexString(value);
        }
    }
  • The function here might do the trick: [Convert ByteArray to UUID java](https://stackoverflow.com/questions/24408984/convert-bytearray-to-uuid-java) – Gabriel Luci Jan 29 '20 at 00:35
  • It still returns a different value to the objectGUID that appears in the Activ Directory. That's why I asked what the process is – Alejandro Esquieros Jan 29 '20 at 21:05
  • You already tried the `getGuidFromByteArray` function in that answer I linked to? – Gabriel Luci Jan 29 '20 at 22:37
  • I solved it using the SearchResultEntry class : member.setObjectGUID(Utiles.bytesToUUID( result.getAttribute(attribute).getValueByteArray()).toString()); thanks for your time to answer me!! – Alejandro Esquieros Jan 31 '20 at 22:17

0 Answers0