0

I got an array

int key[128] = {1,0,1,0,0,1, .........., 0,1,0}

I want to convert this array to

unsigned char key1[16] = {0x__,0x__,...,0x__} 

for example if

from key[0] to key[7] is 10100011, then key1[0] = 0xa3

I need use this new array for AES encryption

FrankeyYuan
  • 43
  • 1
  • 4

2 Answers2

0

This assumes that the lsb is key[0], and the msb is key[7]

int key[128] = {1,0,1,0,0,1, .........., 0,1,0}
unsigned char key1[16];

int (* better_key)[8] = (int (*)[8])key;

int i;
int j;

for (i=0;i<sizeof(key1); i++){
     key1[i]=0;
     for(j=0;j<8;j++){
       key1[i]|=better_key[i][j]<<j;
     }
}

If the msb is key[0], and the lsb is key[7], just replace

better_key[i][j]

with

better_key[i][7-j]

xvan
  • 4,554
  • 1
  • 22
  • 37
0

Go though each element of key[] adding (or'ing) in its bit-position value to the corresponding element of key1[].

#define N 128
#define B8 8

int key[N] = {1,0,1,0,0,1, .........., 0,1,0}
unsigned char key1[N/B8] = { 0 };

for (unsigned i = 0; i<N; i++) {
  key1[i/B8] |= key[i] << (B8 - 1 - (i%B8));
}
chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256