I feel silly for not being able to figure this out, but I am lost. I am trying to XOR two C strings.
我觉得愚蠢无法解决这个问题,但我迷失了。我试图XOR两个C字符串。
#include <stdio.h>
#include <memory.h>
#include <stdlib.h>
int main()
{
char plainone[16];
char plaintwo[16];
char xor[17];
strcpy(plainone, "PlainOne");
strcpy(plaintwo, "PlainTwo");
int i=0;
for(i=0; i<strlen(plainone);i++)
xor[i] ^= (char)(plainone[i] ^ plaintwo[i]);
printf("PlainText One: %s\nPlainText Two: %s\n\none^two: %s\n", plainone, plaintwo, xor);
return 0;
}
My output is:
我的输出是:
$ ./a.out
PlainText One: PlainOne
PlainText Two: PlainTwo
one^two:
Why doesn't the xor array read as anything?
为什么xor数组不能读作什么?
2 个解决方案
#1
12
Once you are dealing with XOR, you are dealing with binary bytes that might not be printable ASCII characters.
处理XOR后,您正在处理可能不是可打印ASCII字符的二进制字节。
And when you XOR the same characters with each other, you get a 0. So 'P' ^ 'P'
will be 0. That's a NUL byte and it terminates the string. If you try to print with printf()
you get nothing; printf()
considers the string to be a terminated length-0 string.
当你将相同的字符相互异或时,你得到一个0.所以'P'^'P'将为0.这是一个NUL字节,它终止字符串。如果你尝试用printf()打印,你什么也得不到; printf()认为该字符串是一个终止的长度为0的字符串。
Also, you should simply assign the XOR result into your target buffer with =
rather than using ^=
as your program did.
此外,您应该使用=而不是使用^ =将XOR结果分配到目标缓冲区中。
Here's my version of your program, and my output:
这是我的程序版本和输出:
#include <stdio.h>
#include <memory.h>
#include <stdlib.h>
#define LENGTH 16
int main()
{
char const plainone[LENGTH] = "PlainOne";
char const plaintwo[LENGTH] = "PlainTwo";
char xor[LENGTH];
int i;
for(i=0; i<LENGTH; ++i)
xor[i] = (char)(plainone[i] ^ plaintwo[i]);
printf("PlainText One: %s\nPlainText Two: %s\n\none^two: ", plainone, plaintwo);
for(i=0; i<LENGTH; ++i)
printf("%02X ", xor[i]);
printf("\n");
return 0;
}
Output:
PlainText One: PlainOne
PlainText Two: PlainTwo
one^two: 00 00 00 00 00 1B 19 0A 00 00 00 00 00 00 00 00
Notice how the first five bytes are all 00
because Plain
is XORed with Plain
.
注意前五个字节都是00,因为Plain与Plain是XORed。
#2
1
Well "Plain" xor "Plain" == 00000, were 0 is the terminator char. C strings print up to the terminator, which means it prints nothing.
好“普通”xor“普通”== 00000,0是终结者字符。 C字符串打印到终止符,这意味着它什么都不打印。
#1
12
Once you are dealing with XOR, you are dealing with binary bytes that might not be printable ASCII characters.
处理XOR后,您正在处理可能不是可打印ASCII字符的二进制字节。
And when you XOR the same characters with each other, you get a 0. So 'P' ^ 'P'
will be 0. That's a NUL byte and it terminates the string. If you try to print with printf()
you get nothing; printf()
considers the string to be a terminated length-0 string.
当你将相同的字符相互异或时,你得到一个0.所以'P'^'P'将为0.这是一个NUL字节,它终止字符串。如果你尝试用printf()打印,你什么也得不到; printf()认为该字符串是一个终止的长度为0的字符串。
Also, you should simply assign the XOR result into your target buffer with =
rather than using ^=
as your program did.
此外,您应该使用=而不是使用^ =将XOR结果分配到目标缓冲区中。
Here's my version of your program, and my output:
这是我的程序版本和输出:
#include <stdio.h>
#include <memory.h>
#include <stdlib.h>
#define LENGTH 16
int main()
{
char const plainone[LENGTH] = "PlainOne";
char const plaintwo[LENGTH] = "PlainTwo";
char xor[LENGTH];
int i;
for(i=0; i<LENGTH; ++i)
xor[i] = (char)(plainone[i] ^ plaintwo[i]);
printf("PlainText One: %s\nPlainText Two: %s\n\none^two: ", plainone, plaintwo);
for(i=0; i<LENGTH; ++i)
printf("%02X ", xor[i]);
printf("\n");
return 0;
}
Output:
PlainText One: PlainOne
PlainText Two: PlainTwo
one^two: 00 00 00 00 00 1B 19 0A 00 00 00 00 00 00 00 00
Notice how the first five bytes are all 00
because Plain
is XORed with Plain
.
注意前五个字节都是00,因为Plain与Plain是XORed。
#2
1
Well "Plain" xor "Plain" == 00000, were 0 is the terminator char. C strings print up to the terminator, which means it prints nothing.
好“普通”xor“普通”== 00000,0是终结者字符。 C字符串打印到终止符,这意味着它什么都不打印。