.txt 文件中的 Ascii 控制字符问题、XOR 加密、C++
Issues with Ascii Control Characters in .txt files, XOR encryption, C++
C++,我希望能够使用密钥为的逐字符 XOR 来“加密”和解密 .txt 文件
"1".
我能够成功加密和解密“Hello World”,但较大的文本文件似乎有一些 XOR 映射到 Ascii 控制字符,导致解密无法完全解密整个文件。
Hello.txt 保存“Hello World”,output.txt 为空。
我如何异或:
std::ifstream infile("Hello.txt");
std::ofstream outfile("output.txt");
std::string key = "1";
std::string contents((std::istreambuf_iterator<char>(infile)),
std::istreambuf_iterator<char>()); //copies file data to contents
infile.close();
int k = 0;
for(std::size_t i = 0; i<contents.length();i++) //character by character
{
contents[i] = contents[i] ^ key[k]; //"encrypt"
k = (k+1) % key.length(); //iterate through key (for longer keys)
outfile <<contents[i]; //write to output.txt
}
outfile.close();
使用相同的代码解密 output.txt,但需要对 infile 和 outfile 进行硬编码更改。例如。 infile("output.txt"), outfile("text.txt");
如果文件包含字符“+”,密钥为“1”,“+”将加密为“”(→),解密将截断“+”后的所有内容,包括在内。
我认为某些值与 Ascii 控制字符异或 .txt 文件无法处理 (Windows 10) 是否正确?
是否可以通过我使用.txt 文件保存密文的方法正确地“加密”和解密?
如果可能,如何将 Ascii 控制字符存储到 .txt 文件?
我应该更改什么以更好地处理密文中的 Ascii 控制字符?
当您创建一个 std::ofstream
或 std::istream
对象时,您使用 2 个参数 ctor,但第二个参数具有默认值(std::ios::in
和 std::ios::out
respectevely)并且打开默认情况下处于“文本”模式的文件,Windows 将启用控制字符处理和映射 \r
到“\r\n”,反之亦然。
所以显式传递第二个参数:
std::ofstream outfile("output.txt", std::ios::out | std::ios::binary);
这将以“二进制”模式打开它们,所有数据都将不加更改地通过。
以二进制方式打开文件解决了问题。
std::ifstream infile("Hello.txt",std::ios::binary); //<---- here
std::ofstream outfile("output.txt",std::ios::binary); //<----- here
std::string key = "1";
std::string contents((std::istreambuf_iterator<char>(infile)),
std::istreambuf_iterator<char>()); //copies file data to contents
infile.close();
int k = 0;
for(std::size_t i = 0; i<contents.length();i++) //character by character
{
contents[i] = contents[i] ^ key[k]; //"encrypt"
k = (k+1) % key.length(); //iterate through key (for longer keys)
outfile <<contents[i]; //write to output.txt
}
outfile.close();
C++,我希望能够使用密钥为的逐字符 XOR 来“加密”和解密 .txt 文件 "1".
我能够成功加密和解密“Hello World”,但较大的文本文件似乎有一些 XOR 映射到 Ascii 控制字符,导致解密无法完全解密整个文件。
Hello.txt 保存“Hello World”,output.txt 为空。
我如何异或:
std::ifstream infile("Hello.txt");
std::ofstream outfile("output.txt");
std::string key = "1";
std::string contents((std::istreambuf_iterator<char>(infile)),
std::istreambuf_iterator<char>()); //copies file data to contents
infile.close();
int k = 0;
for(std::size_t i = 0; i<contents.length();i++) //character by character
{
contents[i] = contents[i] ^ key[k]; //"encrypt"
k = (k+1) % key.length(); //iterate through key (for longer keys)
outfile <<contents[i]; //write to output.txt
}
outfile.close();
使用相同的代码解密 output.txt,但需要对 infile 和 outfile 进行硬编码更改。例如。 infile("output.txt"), outfile("text.txt");
如果文件包含字符“+”,密钥为“1”,“+”将加密为“”(→),解密将截断“+”后的所有内容,包括在内。
我认为某些值与 Ascii 控制字符异或 .txt 文件无法处理 (Windows 10) 是否正确?
是否可以通过我使用.txt 文件保存密文的方法正确地“加密”和解密?
如果可能,如何将 Ascii 控制字符存储到 .txt 文件?
我应该更改什么以更好地处理密文中的 Ascii 控制字符?
当您创建一个 std::ofstream
或 std::istream
对象时,您使用 2 个参数 ctor,但第二个参数具有默认值(std::ios::in
和 std::ios::out
respectevely)并且打开默认情况下处于“文本”模式的文件,Windows 将启用控制字符处理和映射 \r
到“\r\n”,反之亦然。
所以显式传递第二个参数:
std::ofstream outfile("output.txt", std::ios::out | std::ios::binary);
这将以“二进制”模式打开它们,所有数据都将不加更改地通过。
以二进制方式打开文件解决了问题。
std::ifstream infile("Hello.txt",std::ios::binary); //<---- here
std::ofstream outfile("output.txt",std::ios::binary); //<----- here
std::string key = "1";
std::string contents((std::istreambuf_iterator<char>(infile)),
std::istreambuf_iterator<char>()); //copies file data to contents
infile.close();
int k = 0;
for(std::size_t i = 0; i<contents.length();i++) //character by character
{
contents[i] = contents[i] ^ key[k]; //"encrypt"
k = (k+1) % key.length(); //iterate through key (for longer keys)
outfile <<contents[i]; //write to output.txt
}
outfile.close();