Encryption arrays in C -
i having trouble encryption function in c. char data[] holds 4 characters (i.e. byte) trying encrypt using ciphermap[] found using random number generator. basically, nonsense[] encrypted array , it's index equal ciphermap's value...given this, not sure how set encrypted value byte of particular spot original char data[].
i know question may confusing or vague that's because confused...and new @ such low-level programming. here code:
int encrypt (char data[]) // plain text in char { int nonsense[28]; // cipher text int i; (i = 0; < 28; i++) { nonsense[ciphermap[i]] = data[i / 7] & (1 << (i % 7)); printf("%d", nonsense[ciphermap[i]]); } return 1; }
the problem expression data[i / 7] & (1 << (i % 7))
yields single bit, leaves bit @ it's original location. result of expression power of 2 (1, 2, 4, etc). here's different way extract bits.
void encrypt (char data[]) // plain text in char { int nonsense[28]; // cipher text int i, mask, index; index = 0; ( = 0; < 4; i++ ) { ( mask = 1; mask < 128; mask <<= 1 ) { if ( data[i] & mask ) nonsense[ciphermap[index]] = 1; else nonsense[ciphermap[index]] = 0; index++; } } }
in code above, i
array index plain text, , mask
one-bit value that's used check bits in data
. mask starts value of 1 (lsb first) , shifted left 1 bit each time through loop. when mask
reaches msb, have value of 128, terminates inner loop , allows outer loop move on next input byte. 7 least significant bits of each byte encoded.
Comments
Post a Comment