我在stephan-brumme网站上进行了XOR加密教程(不幸的是我不能包含URL,因为我没有足够的声望)。我想要做的是:阅读example.txt文件的内容并解密它包含的文本。例如,这是example.txt的内容:C++ XOR加密 - 解密问题
\xe7\xfb\xe0\xe0\xe7
这样,当使用密码“password”进行解密时,应返回“hello”。这是我的代码:
#include <string>
#include <iostream>
#include <fstream>
using namespace std;
std::string decode(const std::string& input)
{
const size_t passwordLength = 9;
static const char password[passwordLength] = "password";
std::string result = input;
for (size_t i = 0; i < input.length(); i++)
result[i] ^= ~password[i % passwordLength];
return result;
}
int main(int argc, char* argv[])
{
string line;
ifstream myfile ("example.txt");
if (myfile.is_open())
{
while (getline (myfile,line))
{
cout << decode(line);
}
myfile.close();
}
return 0;
}
这是运行应用程序的结果: click for image
正如你可以看到,解密不成功。现在,如果我做它,所以它不读txt文件,而是直接解密的文字,像这样:
cout << decode("\xe7\xfb\xe0\xe0\xe7");
它完美: click for image
我在做什么错在这里?
非常感谢提前! :)
最可能的来源是example.txt中的内容。记住XOR的输出是二进制格式,这意味着example.txt需要是UTF-8。为了保持一致性,请始终使用相同类型的键和输入[例如:char或widechar(utf-8)] – Nandu
example.txt为UTF-8。感谢您的帮助! – MrWhite
可以包含显示文件内容的图像吗? – Nandu