#XOR cypher function, type casting from ASCII help

39 messages · Page 1 of 1 (latest)

hybrid rock
#

My goal is to make the following function:

char* EncryptXOR(char* hexString, char* key)

that takes a string of 32-bit hex and uses xor encryption on it using a 4-letter ASCII key. I'm not sure how to approach it besides converting the two to int, XORing, then converting the result back to ASCII. The last step I'm having trouble with because I would have to un-concatenate the result and convert each to the equivalent character.

I know there must be a more efficient way to turn ascii into an int, do something to it, and turn it back to ascii, I just can't find anything that works.

dusty pelicanBOT
#

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.

pine rain
#

a string of 32-bit hex
a what

hybrid rock
#

sorry, like "0000F000"

pine rain
#

oh i see

#

well

#

you loop over hexString and at the same time over key

#
for (int i = 0; i < strlen(hexString); i++) {
    /* do stuff with */ key[i % strlen(key)]
}
#

the i % len ensures that youre not accessing out of bounds

hybrid rock
#

thats for the encrypting part right

pine rain
#

for both

hybrid rock
#

how should i turn the key into an int tho

pine rain
#

why would you

hybrid rock
#

u can xor an int with a character?

pine rain
#

both hexString and key are chars

#

not ints

hybrid rock
#

oh

#

so u can xor 2 characters together?

pine rain
#

why wouldnt you be able to ?

#

it get implicitly converted to an int if you mean that

hybrid rock
#

so for that operation it would use the ascii values

pine rain
#

and then back to a char once you assign

#
char a = 123;
char b = 111;

char c = a ^ b;
^^^^^^   ^^^^^
char      int 
hybrid rock
#

oooo

pine rain
#

;compile

char a = 123;
char b = 111;
char c = a ^ b;
printf("%zu\n", sizeof(char));
printf("%zu\n", sizeof(int));
printf("%zu\n", sizeof(c));
printf("%zu\n", sizeof(a ^ b));
storm atlasBOT
#
Program Output
1
4
1
4
hybrid rock
#

whats %zu?

pine rain
#

sizeof returns size_t and the correct format specifier for that type is zu

hybrid rock
#

oh ok

#

okok

#

hmm

#
char* EncryptXOR(char* hexString, char* key) {
    char* encryptedString = (char*)malloc(9); 
    encryptedString[8] = '\0';

    for (int i = 0; i < strlen(hexString); i++) {
    encryptedString[i] = hexString[i] ^ key[i % strlen(key)];
    }

    return encryptedString;
}
#

i have this but the output isnt matching the test cases still, is there anything obviously wrong

pine rain
#

dont hardcode sizes that are supposed to be variable

#
size_t len = strlen(hexstring);
char* encryptedString = malloc(len + 1); 
encryptedString[len] = '\0';

for (int i = 0; i < len; i++) {
    ...
}
hybrid rock
#

its still not working but i'll work it out :(

#

thanks for your help

dusty pelicanBOT
#

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