unsigned key[8] increament problem :(


hi,

i wish to generate all possible key combinations ranging:

HEX: "0F FF FF FF FF FF FF FF" TO HEX: FF FF FF FF FF FF FF FF

i also test each key after incrementing by 1, for test i want the key to be a an unsigned char[8]

key start rang and end range can be initialize/declare in any format.

Problem is if :

unsigned char key[] = {0x0F,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF};

then i can not increment this key by +1 , though if i initialize this key in decimal as:

unsigned long long key = 1152921504606846975;

then i can increment the key in for loop by key++ but then i cant convert it back into unsigned char array :(

i want to achieve something like this soory i am c noob :(

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <stdio.h>
#include <string.h>
#include <stdlib.h>


unsigned char key[] = {0x0F,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF};


int main()
{
    int idx;
    
    for(idx=0;;idx++)
    {

    //key[]++; here i want to add +1 in key for increamenting
    

printf ("Current key: %02X %02X %02X %02X %02X %02X %02X %02X \n",key[0],key[1],key[2],key[3],key[4],key[5],key[6],key[7]); // this way i want to print the final incremented key
	}

}



In my programe i also have function that test each key but key has to be unsigned char so i am kinda stuck :(


Thanks all

Something like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <stdint.h>
#include <stdio.h>

unsigned char* to_char_array(uint64_t in, unsigned char* buf)
{
    for(int i = 0; i < 8; ++i)
        buf[i] = (in >> 8*i) & 0xFFu;
    return buf;
}

void output(unsigned char* key)
{
    printf("Current key: %02X %02X %02X %02X %02X %02X %02X %02X \n",
            key[0],key[1],key[2],key[3],key[4],key[5],key[6],key[7]);
}

int main()
{
    const uint64_t start = 0xFFFFFFFFFFFFFF0Fu;
    const uint64_t end   = 0xFFFFFFFFFFFFFFFFu;
    unsigned char buf[8];
    //Handles overflow gracefully
    for(uint64_t x = start; x != end + 1; ++x)
        output(to_char_array(x, buf));
}
http://ideone.com/YJtaSa
Last edited on
hi,

Thanks for reply i run the program it increments in 1 byte only output as:

10 FF FF FF FF FF FF FF
11 FF FF FF FF FF FF FF
12 FF FF FF FF FF FF FF
13 FF FF FF FF FF FF FF
14 FF FF FF FF FF FF FF
15 FF FF FF FF FF FF FF
16 FF FF FF FF FF FF FF
.
.
.
FF FF FF FF FF FF FF FF << stop here


increamenting each byte is simple byte[0]++; and byte[1]++; but this way all possible combinations cant be generate , i also tried this way but we must take the char array as whole integer.

Last edited on
hi,

sorry i think i mistake in key start range in decimal key range is :

1152921504606846975 <to> 18446744073709551615

conisder these in hex :

unsigned char key_low[] = {0x0F,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF};

to

unsigned char key_high[] = {0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF};




it should increament like:

0F FF FF FF FF FF FF FF
01 00 00 00 00 00 00 00
01 00 00 00 00 00 00 01
01 00 00 00 00 00 00 02
01 00 00 00 00 00 00 03
01 00 00 00 00 00 00 04
and so on ....

or plz consider key start/low rang :

unsigned char key_low[] = {0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00};

hope this avoid confusion ...






Last edited on
hi,

i edit your code and made:

const uint64_t start = 0x0100000000000000u;

now it generate i think all keys i want but from left to right i it decrementing lol

i am not sure it will generate all possible combinations or not plz someone test see if it can generate all combinations also suggest improvement.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <stdint.h>
#include <stdio.h>

unsigned char* to_char_array(uint64_t in, unsigned char* buf)
{
    for(int i = 0; i < 8; ++i)
        buf[i] = (in >> 8*i) & 0xFFu;
    return buf;
}

void output(unsigned char* key)
{
    printf("Current key: %02X %02X %02X %02X %02X %02X %02X %02X \n",
            key[0],key[1],key[2],key[3],key[4],key[5],key[6],key[7]);
}

int main()
{
    //const uint64_t start = 0xFFFFFFFFFFFFFF0Fu;
    const uint64_t start = 0x0100000000000000u;
	const uint64_t end   = 0xFFFFFFFFFFFFFFFFu;
    unsigned char buf[8];
    for(uint64_t x = start; x != end + 1; ++x)
        output(to_char_array(x, buf));
}







newcslover wrote:
0F FF FF FF FF FF FF FF
01 00 00 00 00 00 00 00
It should be actually
0F FF FF FF FF FF FF FF
10 00 00 00 00 00 00 00


Only problem you have now is that values are backward outputted in big endian format.
Change output to:
1
2
3
4
5
void output(unsigned char* key)
{
    printf("Current key: %02X %02X %02X %02X %02X %02X %02X %02X \n",
           key[7],key[6],key[5],key[4],key[3],key[2],key[1],key[0]);
}
http://ideone.com/1c3Jkx

(I intentionally changes start range. Otherwise ideone would not allow program to finish, as output will be too long)
Last edited on
Yes thanks it indeed solve my problem ... i will try improve it further thanks you so much brother
Topic archived. No new replies allowed.