I don't have any idea what a pbox might be but it looks like you want to do some kind of decoding of possibly a large amount of data. In that case I would precalculate the bitmasks to safe a lot of shift operations.
Code:
#include <stdio.h>
unsigned char in_masks[8] = { 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80 };
unsigned char bit_masks[8];
unsigned char pbox1[8] = { 7, 1, 2, 3, 4, 5, 6, 0 };
unsigned char pbox2[8] = { 1, 7, 5, 2, 0, 4, 6, 3 };
void setup_mask( unsigned char * pbox ) {
int i;
for ( i = 0; i < 8; ++i )
bit_masks[i] = 1 << pbox[i];
}
unsigned char chg_order( unsigned char a ) {
unsigned char result = 0;
int i = 0;
for ( ; i < 8; ++i )
if ( a & in_masks[i] )
result |= bit_masks[i];
return result;
}
int main() {
int i;
unsigned char data[] = { 0xFE, 0xD3, 0x3d, 0x77, 0x8F, 0xaa, 0x01 };
setup_mask( pbox1 );
for ( i = 0; i < sizeof(data)/sizeof(data[0]); ++i )
printf("%02X -> %02X\n", (int)data[i], (int)chg_order(data[i]) );
puts( "changing decoding order");
setup_mask( pbox2 );
for ( i = 0; i < sizeof(data)/sizeof(data[0]); ++i )
printf("%02X -> %02X\n", (int)data[i], (int)chg_order(data[i]) );
return 0;
}
Kurt