#Encrypting Issue

14 messages · Page 1 of 1 (latest)

near basinBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.

stiff oyster
#

Here is the code:

unsigned char *Crypt(unsigned char *data, int datalength, unsigned int initialValue){

    // error handleing checks
    if(data == nullptr){ // checking data is valid input
        throw invalid_argument("Error: Null pointer for input data");
    }
    if(datalength <0) { // checking datalength negative and greater than 1 byte
        throw out_of_range("Error: datalength out of valid range [0, 8]");
    }
    if(initialValue < 0 ){ // valid input range
        throw out_of_range("Error: initialValue out of valid range [0, 255]");
    }

    unsigned int feedback_value = 0x87654321; // This is the F value 
    unsigned char* key_stream = new unsigned char[datalength];
    unsigned char* output = new unsigned char[datalength] ;

    if(output == nullptr){
        throw bad_alloc(); // memory allocation failure
    }

    // Using the LFSR algo to produce a key stream
    for(int i = 0; i <8; i++){
        if(initialValue %2 == 0){
            initialValue = initialValue >> 1;
        } else{
            initialValue = (initialValue >> 1) ^ feedback_value;
        }
        
    }
    key_stream[0] = initialValue & 0xFF;
    
    // obtaining the keystream with reinterpret_cast is not making it possible, so need to use another method
     // - this interprets the entire unsigned int as an array of unsigned char

    // key_stream = reinterpret_cast<unsigned char*>(&initialValue); // concerned about using this. 
    // so far it is alright, but don't know issues about it

    for(size_t i = 0; i < datalength; i++){ 
        output[i] = data[i] ^ key_stream[i]; // does the XOR with length of bytes 
    }
    delete[] key_stream;
    return output;

}
#

And the main function:

nt main(int argc, char const *argv[])
{
    // Run excample of the LFSR cipher and XOR cipher
    const char* str= "apple";
    unsigned char* data = (unsigned char*) str;
    unsigned int initialValue = 0x12345678; // this is the current state: initial
    int datalength = 5;

    // We need to deal with extreme cases to run algorithmn better
    // like str ="pineapple", datalength = 10  

    unsigned char *lfsr_encryption = Crypt(data,datalength,initialValue);

    // print the encrypted data 
    for(size_t i = 0; i < datalength; ++i){
        cout << "\\x" << setw(2) << setfill('0') << hex << static_cast<int>(lfsr_encryption[i]);
        if(i < datalength - 1){
            cout << "|";
        }
    }

    // initialValue = initialValue >> 1;
    // hex is 0xCD01EFD730
    // for (size_t i = 0; i < datalength; i++) {
    //     cout << hex<< static_cast<int>(lfsr_encryption[i]); // also use printf("%c", data[i]);
    // }

    // Now do the reverse  and get back the message 
    cout << endl;
    data = lfsr_encryption;
    unsigned char *lfsr_decryption = Crypt(data,datalength,initialValue);
    for (int i = 0; lfsr_decryption[i] != '\0'; i++) {
        cout <<  (char)lfsr_decryption[i]; // also use printf("%c", data[i]);
    }
    
    return 0;
}

lavish jacinth
#

@stiff oyster have you tried stepping through the code with a debugger?

stiff oyster
lavish jacinth
#

alright then, have fun!

stiff oyster
#

I was able to step through the code with gdb and got this as the outputs after running the function.

(gdb) print initialValue
$4 = 700264620
(gdb) print key_stream
$5 = (unsigned char *) 0x55555556aeb0 "\254"
(gdb) print output
$6 = (unsigned char *) 0x55555556aed0 "\315pple"
(gdb)

Before the function, I got the initial defined output.

So far, I don't understand the issue as I never really used gdb extensively before.

stiff oyster
#

@lavish jacinth

lavish jacinth
#

well

#

what did you learn from using the debugger?

stiff oyster
#

I think it ha to deal with the key_stream definition as only a single character is being encrypted. Which in return cause the output to be incorrect.

#

I am still a bit confused on how to change it such that the encryption is possible. Even with reinterpret_cast<unsigned char> the issue is still there. Don't get where I am missing the logic.

near basinBOT
#

This question is being automatically marked as stale.
If your question has been answered, type !solved.
If your question is not answered feel free to bump the post or re-ask.
Take a look at !howto ask for tips on improving your question.

near basinBOT
#

Thank you and let us know if you have any more questions!

This thread is now set to auto-hide after an hour of inactivity