【问题标题】:Strange Decryption Output奇怪的解密输出
【发布时间】:2013-04-11 12:55:18
【问题描述】:

我只是在尝试加密和解密,并制作了一个函数来加密、解密和写入文件......

void encrypt(const std::string& r){

std::string new_r = "Encrypted:\n";
for(int i = 0; i < r.length(); i++)
{
    new_r += ~r[i];
}

wtofe("/home/programming/Desktop/latin.txt", new_r); // Writes it to a file
decrypt(new_r);
}

void decrypt(const std::string& r){

std::string new_r = "Decrypted:\n";
for(int i = 0; i < r.length(); i++)
{
    new_r += ~(r[i]);
}

wtofd("/home/programming/Desktop/latin.txt", new_r); //Writes it to a file
}

写入文件并加密有效。它也解密了它,但看看这个奇怪的输出:

我作为输入写的是 Davlog,正如您所见,它已添加到解密的末尾。但为什么?我做错了什么?

【问题讨论】:

  • 显示 wtofd 函数...你也在描述 new_r,它是字符串 "Encrypted:\n" + 加密字符串
  • wtofd 函数不是问题,它只是一个写入文件的 fstream

标签: c++ encryption


【解决方案1】:

试试这个:

void encrypt(const std::string& r){
  std::string new_r;
  for(int i = 0; i < r.length(); i++)
  {
      new_r += ~r[i];
  }

  wtofe("/home/programming/Desktop/latin.txt", new_r); // Writes it to a file
  decrypt(new_r);
}

void decrypt(const std::string& r){

  std::string new_r;
  for(int i = 0; i < r.length(); i++)
  {
      new_r += ~(r[i]);
  }

  wtofd("/home/programming/Desktop/latin.txt", new_r); //Writes it to a file
}

在您的原始代码中,您将"Encrypted:[your encrypted msg]" 而不仅仅是"[your encrypted msg]" 写入您的文件。因此,解密步骤将解密“Encrypted:”部分以及原始加密消息。

【讨论】:

    猜你喜欢
    • 2020-07-24
    • 1970-01-01
    • 2013-10-04
    • 1970-01-01
    • 2017-04-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多