简体   繁体   English

使用补码加密文件

[英]Encrypt a file using one's complement

I have used C++ file streams to encrypt a file. 我已经使用C ++文件流来加密文件。 In this, every character read is one's complemented and written into the output file: 在这种情况下,读取的每个字符都将被补充并写入输出文件:

ifstream input("Normal file");
ofstream output("encrypted file");
char ch;
while (input >> ch)
{
  ch = ~ch;
  output << ch;
}

I used the same program to recover normal file from encrypted file . 我使用同一程序从加密文件中恢复正常 文件 However, I found that the unencrypted file did not have any blank spaces or return marks. 但是,我发现未加密的文件没有任何空格或返回标记。 So, I changed the while part to: 因此,我将while部分更改为:

while (input >> ch)
{
  ch = isspace(ch) ? ch : ~ch;
  output << ch;
}

Till the results are not what I expected. 直到结果不是我所期望的。 Where did I go wrong? 我哪里做错了?

The problem is in how you're reading the file: 问题在于您如何读取文件:

char ch;
while (input >> ch)

By default, the stream extractor skips all whitespace. 默认情况下,流提取器跳过所有空格。 To get it to stop that, you can do something like: 要使其停止,可以执行以下操作:

input >> std::noskipws;

... before you start reading from the stream. ... 您开始从信息流读取之前

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM