简体   繁体   English

c xor结果与javascript xor不同

[英]c xor result is different than javascript xor

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: 我正在尝试在c中进行某种xor文件加密,并在javascript中进行解密( 以此为基础,现在我遇到了以下问题:

Say for example i want to do 73^122 in C, the result is 57 , but the same operation in javascript yields 51 . 比如说我想在C中做73^122 ,结果是57 ,但是javascript中的相同操作产生51 Why is this happening, and what would be the proper way to fix it? 为什么会发生这种情况,以及解决问题的正确方法是什么?

Here's some C code of the encrypt function 这是加密函数的一些C代码

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. 我真的怀疑你提到的C的结果。 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. 你的右手边有超过8位有点奇怪,通常在C中进行XOR加密你一次只能做一个char ,这实际上意味着8位字节。

Any chance you confused hexadecimal ( 0x73 and 0x122 ) vs decimal ( 73 and 122 ) number literals? 您是否有机会混淆十六进制( 0x730x122 )与十进制( 73122 )数字文字? 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. 你能告诉我们有问题的C代码,我们可以告诉你这个bug。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM