Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/129.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ C++;XOR加密-解密问题_C++_Encryption_Cryptography_Xor - Fatal编程技术网

C++ C++;XOR加密-解密问题

C++ C++;XOR加密-解密问题,c++,encryption,cryptography,xor,C++,Encryption,Cryptography,Xor,我遵循stephan brumme网站上关于XOR加密的教程(不幸的是,我不能包括URL,因为我没有足够的声誉)。我想做的是:读取example.txt文件的内容并解密其中包含的文本。例如,这是example.txt的内容: \xe7\xfb\xe0\xe0\xe7 当使用密码“password”解密时,应返回“hello”。这是我得到的代码: #include <string> #include <iostream> #include <fstream>

我遵循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;
}
#包括
#包括
#包括
使用名称空间std;
标准::字符串解码(常量标准::字符串和输入)
{
const size\u t passwordLength=9;
静态常量字符密码[passwordLength]=“password”;
std::字符串结果=输入;
对于(size_t i=0;icout我打赌example.txt包含“\”、“x”、“e”、“7”等字符。您必须读取这些字符,处理所有反斜杠转义,然后将其输入解码


\xe7是一种用十六进制值E7表示单个字符的常用方法。(根据您的字符集,很可能是单个字符“ç”)。如果您想存储(加密)可读文本,我建议删除\x,并让文件包含像“E7FBE0E7”这样的行。然后 -把每一行读成一个字符串。 -将每对字符从十六进制数转换为整数,并将结果存储在字符中。 -将该字符存储在字符串中。 -然后xor解密字符串

或者,请确保该文件包含所需的实际二进制字符


还要注意,您正在使用密码的终止nul字节进行异或运算。您是有意这样做的吗?

相同字符的异或为零,因此结果可能包括零。
std::string
不喜欢这样,因为零终止字符串

您还可以使用
std::vector
代替
std::string
进行实际编码/解码。您必须更改
decode
函数以处理
vector

并以二进制形式读/写文件

编辑:仅使用
std::string
std::string解码(const std::string&input)

intmain()
{
std::string line=“hello”;
{
行=解码(行);
std::ofstreammyfile(“example.txt”,std::ios::binary);
write(line.data(),line.size());
//编辑2*************

//std::cout最有可能的源是example.txt的内容。请记住,XOR的输出是二进制格式的,这意味着example.txt需要是UTF-8。为了一致性,请始终使用相同类型的键和输入[Ex:char或widechar(UTF-8)]example.txt是UTF-8。感谢您的帮助!您可以包含一个显示文件内容的图像吗?当然可以,这里是:删除“\x”从文件内容中读取ascii格式的文件。我完全支持对错误答案进行向下投票,但请帮助我改进;告诉我我的答案有什么不好。是的,它确实包含了这些字符。对不起,我不明白你所说的“处理所有反斜杠转义,然后将其提供给解码”是什么意思。您能说得更具体一些吗?非常感谢您的帮助!\xe7是一种用十六进制值E7表示单个字符的常用方法。(根据您的字符集,很可能是单个字符“ç”)。如果您想存储(加密)可读文本,我建议删除\x,让文件包含像“E7FBE0E7”这样的行,然后,在我阅读文件后,我可以添加斜杠,它应该可以工作?斜杠不存在。它们不是字符串中的字符。写为“\xe7\xe8”的字符串有两个字符长。正如“\r\n”是两个字符一样。另一方面“/xe7/xe8”是八个字符长……为了得到一个C++程序来显示这8个带有反斜杠的字符,你必须编写CUT<代码> STD::String 可以保存NUL字符(它的NC字符串有问题),所以不需要使用<代码>矢量< /代码>。问题(你解决)是因为
getline
无法处理NUL,而且如果新行被加密,
getline
将无法找到它们,因此您需要使用二进制读取。这工作正常,但我需要“hello”“以可读的方式加密。我的意思是,我需要它像\xe7\xfb\xe0\xe0\xe7那样,而不是那样:这可能吗?非常感谢!@chrisdd您是对的。
std::string
有时会终止,例如在看到“abc\0123”时”,当涉及到零时,我通常会远离它。但在这种情况下,它应该是好的。我将编辑答案以显示
std::string
这是我遵循的教程:是否可以使“hello”成为将按照本教程中的格式进行加密,并且不会使用不可读的符号?谢谢!@MrWhite,只需格式化输出。请参阅更新的答案。
cout << decode("\xe7\xfb\xe0\xe0\xe7");
int main()
{
    std::string line = "hello";

    {
        line = decode(line);
        std::ofstream myfile("example.txt", std::ios::binary);
        myfile.write(line.data(), line.size());

        //Edit 2 *************
        //std::cout << std::hex;
        //for (char c : line)
        //  std::cout << "\\x" << (0xff & c);
        //*************
        //This will make sure width is always 2
        //For example, it will print "\x01\x02" instead of "\x1\x2"
        std::cout << std::hex << std::setfill('0');
        for (char c : line)
            std::cout << "\\x" << std::setw(2) << (0xff & c);
        std::cout << std::endl;
    }

    {
        std::ifstream myfile("example.txt", std::ios::binary | std::ios::ate);
        int filesize = (int)myfile.tellg();
        line.resize(filesize);
        myfile.seekg(0);
        myfile.read(&line[0], filesize);
        line = decode(line);
        std::cout << line << std::endl;
    }

    return 0;
}