【发布时间】:2015-12-12 19:22:21
【问题描述】:
我按照 stephan-brumme 网站上的教程进行了 XOR 加密(不幸的是,我不能包含 URL,因为我没有足够的声誉)。我想要做的是:阅读 example.txt 文件的内容并解密它包含的文本。例如,这是example.txt的内容:
\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)]
-
example.txt 是 UTF-8。感谢您的帮助!
-
你能附上一张显示文件内容的图片吗?
-
当然,这里是:i.imgur.com/dzc9nBh.png
-
从文件内容中删除“\x”,并将文件读取为ascii。
标签: c++ encryption cryptography xor