XOR with MAC and windows registry

void outputMAC(unsigned char MAC[])
   printf("Your MAC Address Is: %02X-%02X-%02X-%02X-%02X-%02X", MAC[0], MAC[1], MAC[2], MAC[3], MAC[4], MAC[5]);

// Read Mac
      IP_ADAPTER_INFO AdapterInfo[2];
      DWORD dwBufSize = sizeof(AdapterInfo);
      DWORD dwStatus = GetAdaptersInfo(AdapterInfo, &dwBufSize);
      PIP_ADAPTER_INFO pAdapterInfo = AdapterInfo;
        pAdapterInfo = pAdapterInfo->Next;
    // end read mac
//read windows registry
       if( RegOpenKeyEx(HKEY_LOCAL_MACHINE,"SOFTWARE\\mycompany\\mysoft",0,KEY_QUERY_VALUE, &keyHandle) == ERROR_SUCCESS)
                RegQueryValueEx( keyHandle, "ProgramHasRunBefore", NULL, &Type, (LPBYTE)rgValue,&size1);


     return CKBR_OK;

HI I want to perform the following tasks:

1  Read the local computer MAC address
2  Read a windows registry
3. Perform the XOR operation of 1 and 2 and get the final 3 digits


MAC: 0004758226EA
reg. 164D04A6278D

output: 164971240167
First 9 digits= 164971240
final 3 digits = 167
now in my code, i can read teh MAC address properly and I can read teh registry as well

but I don't know how to modify my routine so that we can achieve the 3 operations.

anyone can help?
Who is Participating?
DanRollinsConnect With a Mentor Commented:
The MAC address is 6 bytes long.  In the first line, you convert it to a string of hex digits in a normal manner.

To XOR some OTHER 6-byte value against the MAC, first prepare that other value (let's say that is the key) as an array of 6 bytes, anKey[0] through anKey[5].

Now loop through the six bytes, outputting to a third array, anResult[0]...[5]

BYTE anKey[6];   // fill with binary data that was stored in the registry
BYTE anMAC[6];   // fill with binary data from IP_ADAPTER_INFO.Address
BYTE anResult[6];

for (int j=0; j<6; j++ ) {
       anResult[j] = anMAC[j] ^ anKey[j];

Now you can convert the results value to a string and grab the final three characters...

   char szEncrypted[13];
   sprintf(szEncrypted, "%02X%02X%02X%02X%02X%02X",
               anResult[0], anResult[1], anResult[2], anResult[3], anResult[4], anResult[5]

   char szfinalThreeChars[4];
   strcpy( szfinalThreeChars, &szEncrypted[9] );
   printf("The final 3 chars after XOR are: %s\r\n", szfinalThreeChars );

-- Dan
you could do something like:

int XorValue = MAC ^ Reg;

XorValue &= 0x0000FFF

the & operator will preserve the values of the last 3 bytes and set the rest to 0
gorettiAuthor Commented:
the concept is ok
but how can I modify the code based on my example?
gorettiAuthor Commented:
The problem  now is MAC[0] store 2 digits and rgValue[0] store 1 digit

i want to form a string from MAC and so do the XOR one by one with rgValue
Knut HunstadCommented:
This is also answered in http://www.experts-exchange.com/Programming/Programming_Languages/Dot_Net/VisualC_PLUS_PLUS_DOT_NET/Q_21854805.html:


sprintf(TheResult, "%01X%02X", (MAC[4] & 0xF)^rgValue[9], MAC[5]^(rgValue[10] << 4 + rgValue[11]));

(Not tested, BTW)
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.