...
char text[256];
fgets(text, 255, stdin);
xorEncrypt(text, 'a');
...
void xorEncrypt(char *string, char key)
{
int i, string_length = strlen(string);
for(i=0; i<string_length; i++)
{
string[i]=string[i] ^ key;
}
printf("%s", string);
}
//user enters "test"
//result is: §♦↕§k (correct)
//user enters "abcdefg"
//result is empty
//user enters "testbca"
//result is "§♦↕§♥☻" (incorrect) and there is a beep sound
当我使用“a”、“b”、“c”等字符作为键时,它会给出空结果或一些随机符号,但使用“A”、“I”等其他字母... ,工作正常,可以解密。
最佳答案
代码看起来不错。也许您想要打印结果字符串,但异或可能已将其部分内容转换为不可打印的字符。另请注意,字符串中现在可能存在空字符(例如 'a ^ 'a' = 0),因此结尾不再由终止空字符确定。
关于c - 简单的 XOR 加密给出意想不到的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32297258/