[英]c xor result is different than javascript xor
我正在嘗試在c中進行某種xor文件加密,並在javascript中進行解密( 以此為基礎,現在我遇到了以下問題:
比如說我想在C中做73^122
,結果是57
,但是javascript中的相同操作產生51
。 為什么會發生這種情況,以及解決問題的正確方法是什么?
這是加密函數的一些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;
}
}
我真的懷疑你提到的C的結果。 你應該展示一些代碼。
你的右手邊有超過8位有點奇怪,通常在C中進行XOR加密你一次只能做一個char
,這實際上意味着8位字節。
您是否有機會混淆十六進制( 0x73
和0x122
)與十進制( 73
和122
)數字文字? 再次,當你沒有顯示你的代碼時,很難提供幫助。
當我跑:
#include <stdio.h>
int main() {
printf("%d\n", 73^122);
}
我明白了:
51
你能告訴我們有問題的C代碼,我們可以告訴你這個bug。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.