0

I am trying to implement a LFSR to encrypt a string, but the catch is is that only on the 8th step of the LFSR is when the lowest byte is collected.

My current code encrypts correctly (I believe), but not in the format I would like.

I have to use the initial value provided, and the feedback value provided.

Any help would be appreciated.

My Code:

unsigned char *Crypt(unsigned char *data, int dataLength, unsigned int initialValue){
unsigned int a = 0x87654321; //Feedback Value

unsigned int b = initialValue;
int leastSigBit, count;

for (int i = 0; i < dataLength; i++)
{
    count = 0;
    data[i] = (data[i]) ^ ((b & 0x0000000F)); //Extracting the lowest byte
    do
    {
        count++;
        leastSigBit = b & 1; //Make leastSigBit the output bit
        b >>= 1;             //Shift Register
        if (leastSigBit)
            b ^= a; //If leastSigBit is 1, then XOR feedback product of the 32-bit shift register

    } while (count % 8 != 0); //On the 8th step it will stop loop and extract lowest byte
}

return data;}


int main(){

unsigned int initialVal = 0x12345678;
unsigned char *data;
int dataLength = 5;
int i;

data[0] = 'a';
data[1] = 'p';
data[2] = 'p';
data[3] = 'l';
data[4] = 'e';

cout << endl;
cout << "Original Message: "; //Print Original Data
cout << data << endl;

cout << "Encrypted Message: ";
Crypt(data, dataLength, initialVal);
for (int i = 0; i < dataLength; i++)
    cout << "\\x" << hex << (int)data[i]; //Print Encrypted Data
cout << endl;

cout << "Recovered Message: ";
Crypt(data, dataLength, initialVal);
cout << data << endl; //Printing Recovered Message

cout << endl;

return 0;}
  • Always provide sample inputs, actual outputs, and if they differ, expected outputs. – tadman Mar 11 '21 at 21:07
  • ⟼Remember, it's always important, *especially* when learning and asking questions on Stack Overflow, to keep your code as organized as possible. [Consistent indentation](https://en.wikipedia.org/wiki/Indentation_style) helps communicate structure and, importantly, intent, which helps us navigate quickly to the root of the problem without spending a lot of time trying to decode what's going on. – tadman Mar 11 '21 at 21:14
  • 1
    Your code is broken in several places. For example `b & 0x0F` gives you the lowest 4 bits of `b`, not the lowest byte. I would be amazed if your LFSR implementation produced a usable pseudorandom sequence. Looping while `count % 8 != 0` makes little sense. Why not just use a simple `for()` loop? – r3mainer Mar 11 '21 at 21:23
  • @r3mainer that was my initial thought... but I’d that even the right implementation to solve my issue? I feel like I am missing something paramount here – Cameron Calderon Mar 13 '21 at 03:28
  • Any suggestions to improve this code? I removed the loop and changed it to extract the lowest byte. But I am unsure how to step through the LFSR 8 times THEN take that value. @r3mainer – Cameron Calderon Mar 13 '21 at 19:23

0 Answers0