I'm trying to do some sort of xor file encryption in c, and decryption in javascript (using this as a basis, and for now I'm stuck at the following issue:
Say for example i want to do 73^122
in C, the result is 57
, but the same operation in javascript yields 51
. Why is this happening, and what would be the proper way to fix it?
Here's some C code of the encrypt function
void encrypt_data(FILE* input_file, FILE* output_file, char* key)
{
int key_count = 0; //Used to restart key if strlen(key) < strlen(encrypt)
int encrypt_byte;
while( (encrypt_byte = fgetc(input_file)) != EOF) //Loop through each byte of file until EOF
{
//XOR the data and write it to a file
fputc(encrypt_byte ^ key[key_count], output_file);
printf("original %d\n", encrypt_byte); //yields 73
printf("xoring with %d\n", key[key_count]); // yields 122
printf("xored %d\n", encrypt_byte ^ key[key_count]); // yields 57
break; //breaking just for example purpose
//Increment key_count and start over if necessary
key_count++;
if(key_count == strlen(key))
key_count = 0;
}
}
I really doubt the result for C that you're mentioning. You should show some code.
That your right-hand side has more than 8 bits is a bit strange, normally for XOR encryption in C you'd do it one char
at a time, which in practice means with 8-bit bytes.
Any chance you confused hexadecimal ( 0x73
and 0x122
) vs decimal ( 73
and 122
) number literals? Again, very hard to help when you're not showing your code.
When I run:
#include <stdio.h>
int main() {
printf("%d\n", 73^122);
}
I get:
51
can you please show us the C code in question and we can show you the bug.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.