-
Help Please...bits
I am trying to do an exhaustive search to find a key. That key is 32bits, but I have reduced this search by finding out which key bits are effective (ie: effecting the output).
I have therefore eliminated the a good deal of bytes from this exhaustive search, leaving me with 12 bits of possibilites for the remaining bits. 2^12 is much less than 2^32.
I now need some way to cycle through the possibilities for the remaining 12 bits.
Here's an example:
from left to right: bit0 -> bit31
key={XXXXXXXX XX000000 XX000000 XXXXXXXX}
"bits set to X is a don't care"
I need a way to get all possible values for the above key.
key={XXXXXXXX XX000000 XX000001 XXXXXXXX}
key={XXXXXXXX XX000000 XX000010 XXXXXXXX}
key={XXXXXXXX XX000000 XX000011 XXXXXXXX}
key={XXXXXXXX XX000000 XX000101 XXXXXXXX}
all the way to
key={XXXXXXXX XX111111 XX111111 XXXXXXXX}
Does anybody have any ideas as I've been trying things which are not working......
thank you!!!!
-
you can use bitwise and & to test bits.
char a, c=123;
a= c&1; // state of least significant bit
a=c&2;// state of next bit
a=c&4; // etc
a=c&8;
a=c&16; // and so on.
You can set bits by bitwise orring | with true value in the bit you want to turn on.
-
is there any easier way I could cycle through all values ...
-
of course there is. just test lsb and then right shift.then test again.then right shift.etc.
work on a copy of your key rather than the key itself.
-
i'm kinda of a newbie at C ...
Could you get me started with a snipet of code please....
-
Here's my attempt but it's returning over 10,000 possible values for these key bits...when there should only be 2 to the power of 12=4096....
for ( x = 0 ; x <= 0x000f0f00 ; x += 0x100 )
{
for ( y = 0 ; y <= 0x03f ; y++ )
{
for ( z = 0 ; z <= 0x03f ; z++ )
{
key= x | y | z;
counter++;
printf("\nSomething found ::%d", key);
printf("\nNumber ::%d", counter);
}
}
}
-
-
yeah..that was me too but the bits have changed again and I dont understand the logic...
i tried that code with three for loops but it's returning over 10,000 keys...
any ideas???
-
Use Stoned_Coder's idea but instead of using 0's and 1's just use their hex or decimal equivalent. For instance to find every possible bit combo in a 16-bit usigned int just do a for (unsigned int i=0;i<0xFFFF;i++).
It just counts to 0xFFFF but it will cover every single bit combo that is possible in those 16-bits for the unsigned int. Also you could code an arithmetic shift right and left - remember to preserve the bit that is shifted out of the number so you can properly rotate the bits. But, this will not give you every combo for the bits.
-
> key={XXXXXXXX XX000000 XX000000 XXXXXXXX}
Written as
key={XXXXXXXX XXAAAAAA XXBBBBBB XXXXXXXX}
You need a loop variable for each contiguous block of bits (one for AAAAAA and one for BBBBBB
Then you look at the least significant bit of each of them, and this tells you by how much to increment that counter by
So
for BBBBBB, the increment is 0x100
for AAAAAA, the increment is 0x10000
The limit of each is all those bits set
So
for BBBBBB, the limit is 0x3F00
for AAAAAA, the limit is 0x3F0000
-
>key={XXXXXXXX XX000000 XX000000 XXXXXXXX}
Byte 0 and byte 3 are don't cares. Only bytes 1 and 2 are relevant. The first two bits of both bytes are don't cares. Am I correct?
An inefficient way would be: take bytes 1 and 2 and then count from 0x0000 to 0xFFFF. It's inefficient since you're taking the first two bits of both bytes into account. But it's a way.
-
Yeah, I didn't say it was the best way. Looks like Salem comes through again - looks like my cookies aren't logging me in