c - 简单的 XOR 加密给出意想不到的结果

标签 c windows encryption xor

...
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/

相关文章:

c - int 指针不会返回,触发 SIGABRT

c - 使用 newSocket.sin_port = htons(portNumber) 将端口号分配给套接字时出错;

python - Cython:如何将 C 函数分配给 Python/Cython 变量?

c - 从另一个 exe 运行 exe

java - 加密mongodb中的密码字段

android - OPEN GL ES 和 EGL 库之间的混淆

windows - 是否可以为 Windows 7 和/或 8 创建不区分大小写的自定义区域设置?

windows - 无法创建/打开锁文件 :\data\db\mongod. 锁

c++ - 使用字母字符加密/解密 XOR

php - 如何从php中的sha1()获取加密字符的原始字符串?