Results 1 to 9 of 9

Thread: Seemingly illogical bitwise AND using C++

  1. #1

    Seemingly illogical bitwise AND using C++

    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?

  2. #2
    <script>alert(0)</script> disavowed's Avatar
    Join Date
    Apr 2002
    Posts
    1,281
    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 &.

  3. #3
    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.

  4. #4
    Quote Originally Posted by 5aLIVE View Post
    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?
    Last edited by squidge; June 30th, 2009 at 13:45.

  5. #5
    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.

  6. #6
    Quote Originally Posted by squidge View Post
    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).
    Last edited by 5aLIVE; June 30th, 2009 at 15:02.

  7. #7
    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)

  8. #8
    <script>alert(0)</script> disavowed's Avatar
    Join Date
    Apr 2002
    Posts
    1,281
    Quote Originally Posted by 5aLIVE View Post
    ... 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.

  9. #9
    Thankyou both for your help.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •