Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

C++ Decrypt the Enemy Message. I get stuck with a part of my assignment, and nee

ID: 3593375 • Letter: C

Question

C++ Decrypt the Enemy Message.

I get stuck with a part of my assignment, and need some help.

The entire assignment is about "decrypt the enemy message"

The encryption algorithm is to take the original message, treat each group of 4 bytes like an integer, add a secret key to the integer, then copy the resulting number to the encrypted message treating it like four characters.

Here's an example provided by the instruction of the assignment:

For example, if the original string is “HI THERE” and the secret key is the number 2, then the algorithm would:

Take the first four characters, which are the first four bytes, which are “HI T”.

If these four bytes are typecast to a 4 byte int (the size of an int on most machines) then it has the value 1411402056.

Add the secret key of 2 to the value resulting in the value 1411402058

Typecast the 1411402058 back as a 4 character string, resulting in “JI T” (basically it just increases the leftmost character by 2 in the ASCII code) The process is repeated for the next group of 4 characters, “HERE”:

These four bytes are typecast to a 4 byte int which is the value 1163019592

Add the secret key of 2 to the value resulting in the value 1163019594

Typecast 1163019594 back as a 4 character string, resulting in “JERE” The entire encrypted string would be “JI TJERE”

I just can not figure out this part. How to typecast "HI T" and get the number 1411402058?

Thanks for all the help!

Explanation / Answer

//program

#include<iostream>

#include<string>

using namespace std;

char *encryptMessage(char *str, int key);

char *decryptMessage(char *str, int key);

int main()

{

int key = 2;

char *encryptedMsg = encryptMessage("I am testing", key);

char *decryptedMsg = decryptMessage(encryptedMsg, key);

cout << "Encrypted message: " << encryptedMsg << endl;

cout << "Decrypted message: " << decryptedMsg << endl;

}

char *encryptMessage(char *str, int key)

{

char buf[5];

int i = 0,j=0,k=0,m ,len=0;

int number;

char *encryptedMsg;

//allocate memory for encryptedMsg

encryptedMsg = (char*)malloc((strlen(str) + 1)*sizeof(char));

while (str[i] != '')

{

//take four char into buf

if (j < 4)

{

buf[j++] = str[i++];

}

else

{

buf[j] = '';

j = 0;

//now convert 4 char array ie buf to int and add key to it

number = *(int *)buf+key;

//convert number back to char array

buf[3] = (char)(number & (0xFF));

buf[2] = (char)((number >> 8) & 0xFF);

buf[1] = (char)((number >> 16) & 0xFF);

buf[0] = (char)((number >> 24) & 0xFF);

m = i - 4;

k = 3;

while (k >= 0)

{

encryptedMsg[m] = buf[k];

//cout << "msg" << encryptedMsg[m] << " ";

m++;

k--;

len++;

}

}

}

if (strlen(str) != len)

{

//encrypt mesg which was incomlete

buf[j] = '';

j = 0;

//now convert 4 char array ie buf to int and add key to it

number = *(int *)buf + key;

//convert number back to char array

buf[3] = (char)(number & (0xFF));

buf[2] = (char)((number >> 8) & 0xFF);

buf[1] = (char)((number >> 16) & 0xFF);

buf[0] = (char)((number >> 24) & 0xFF);

m = i - 4;

k = 3;

while (k >= 0)

{

encryptedMsg[m] = buf[k];

m++;

k--;

}

}

encryptedMsg[strlen(str)] = '';

//cout << "Encrypted msg " << encryptedMsg << endl;

return encryptedMsg;

}

char *decryptMessage(char *str, int key)

{

char buf[5];

int i = 0, j = 0, k = 0, m, len = 0;

int number;

char *decryptedMsg;

//allocate memory for encryptedMsg

decryptedMsg = (char*)malloc((strlen(str) + 1)*sizeof(char));

while (str[i] != '')

{

//take four char into buf

if (j < 4)

{

buf[j++] = str[i++];

}

else

{

buf[j] = '';

j = 0;

//now convert 4 char array ie buf to int and add key to it

number = *(int *)buf - key;

//convert number back to char array

buf[3] = (char)(number & (0xFF));

buf[2] = (char)((number >> 8) & 0xFF);

buf[1] = (char)((number >> 16) & 0xFF);

buf[0] = (char)((number >> 24) & 0xFF);

m = i - 4;

k = 3;

while (k >= 0)

{

decryptedMsg[m] = buf[k];

//cout << "msg" << decryptedMsg[m] << " ";

m++;

k--;

len++;

}

}

}

if (strlen(str) != len)

{

//encrypt mesg which was incomlete

buf[j] = '';

j = 0;

//now convert 4 char array ie buf to int and add key to it

number = *(int *)buf - key;

//convert number back to char array

buf[3] = (char)(number & (0xFF));

buf[2] = (char)((number >> 8) & 0xFF);

buf[1] = (char)((number >> 16) & 0xFF);

buf[0] = (char)((number >> 24) & 0xFF);

m = i - 4;

k = 3;

while (k >= 0)

{

decryptedMsg[m] = buf[k];

m++;

k--;

}

}

decryptedMsg[strlen(str)] = '';

//cout << "Decrypted msg " << decryptedMsg << endl;

return decryptedMsg;

}

------------------------------------------------------------------------------

//output1

Encrypted message: JI TJERE
Decrypted message: HI THERE

//output2

Encrypted message: K am"tesving
Decrypted message: I am testing