Log in

View Full Version : Seemingly illogical bitwise AND using C++


5aLIVE
June 30th, 2009, 12:09
I've been taking my first steps in learning C++ and writing little programs as I go to further my understanding.

I've written this simple console program below :

Code:

#include <iostream>
#include <iomanip>
using namespace std;
using std::cout;
using std::endl;
using std:: setfill;
using std::setw;

int main(){

unsigned char* pchar;
unsigned char hexchar = 0x41;
int i = 0;

pchar =&hexchar;

cout << std::hex;
cout << setfill('0');


cout << "The value of the hex variable is ";
cout << "\nHex value of hexchar is " << setw(4) << *pchar;
cout << "\nThe size of hexchar variable is :" << sizeof(*pchar) << "bytes(s)"
<< endl;
cout << hex << *pchar << endl;

for(i=0;i<10;i++)
{
cout << "\nThe hex value is currently set at : " << *pchar;
if((*pchar & 0x42) == 0x42)
cout << "\nYou have found the 'key'";
(*pchar)++;
}
cout << endl;

return 0;
}


The above if statement is displaying the 'found key' message when *pchar is equal to 0x42, 0x43, 0x47, 0x48, 0x4A.
Surely the message showed only be displayed when the pointer to the unsigned character is 0x42 (ASCII character 'B').

All of the above hex values have bit positon two set to '1' which seems to be influencing this although it shouldn't.

What could the problem be?

disavowed
June 30th, 2009, 12:47
If you want to see if it equals 'B' and only 'B', then the if-statement should be: if(*pchar == 0x42)
Or more simply: if(*pchar == 'B')

I'm not sure why you're doing a bitwise &.

5aLIVE
June 30th, 2009, 13:36
My eventual aim is to develop a little program that that will perform an exhaustive key search (40 bits) on an encrypted byte sequence. The above program is experimental to help me understand a few things as I progress.

My aim is to make the program stop and display the key used (in hex) if the first 8 ASCII characters of the decrypted cipher text matches a known sequence.

My reason for using the bitwise AND is because I will eventually be searching through the entire ASCII character set which will include unprintable codes for example.

squidge
June 30th, 2009, 13:40
Quote:
[Originally Posted by 5aLIVE;81405]The above if statement is displaying the 'found key' message when *pchar is equal to 0x42, 0x43, 0x47, 0x48, 0x4A.
Surely the message showed only be displayed when the pointer to the unsigned character is 0x42 (ASCII character 'B').

0x43 & 0x42 == 0x42
0x47 & 0x42 == 0x42
try it in Calculator

I fail to see the illogical part.

Me thinks you mean something like:
if (!memcmp("ABCDEFGH",decrypt(buffer,"AAAAA"))
printf ("I found the key!";

Where AAAAA is your key your testing, and decrypt, well, decrypts the data with the current key. If it matches the expected output (ABCDEFGH in this example), then it outputs a string. Yes?

5aLIVE
June 30th, 2009, 13:51
You are quite right, I checked this on a calculator.
I've got my logic wrong here, what I am trying to do is create a bit mask so that the message is only shown when the value of *pchar is 0x42 only.

5aLIVE
June 30th, 2009, 14:01
Quote:
[Originally Posted by squidge;81410]
Me thinks you mean something like:
if (!memcmp("ABCDEFGH",decrypt(buffer,"AAAAA"))
printf ("I found the key!";

Where AAAAA is your key your testing, and decrypt, well, decrypts the data with the current key. If it matches the expected output (ABCDEFGH in this example), then it outputs a string. Yes?


Yes, that's the kind of thing I am aiming for. Can the same result be achieved using a bit mask?
Thinking about this some more, I don't think it can be done.

memcmp looks absolutely ideal, no mention of it in the indexes of my C++ or C text books which is odd.
As far as I can gather its from the C standard library cstring (string.h).

squidge
June 30th, 2009, 17:45
sure you can use a bitmask, if you dont care about some of the bits in the data your comparing, but if you do, a bitmask is kinda pointless. You'd also have to check each byte seperately (or upto the size of the target system)

yes, memcmp is a c library function. I'm oldskool :-) (I program small microcontrollers for a living, c++ typically isn't available/possible when you have such small memory spaces)

disavowed
June 30th, 2009, 18:21
Quote:
[Originally Posted by 5aLIVE;81411]... so that the message is only shown when the value of *pchar is 0x42 only.


No, you wouldn't use a bitmask for this. You'd use "==" or a memcmp(...)-style function.

5aLIVE
July 1st, 2009, 12:45
Thankyou both for your help.