c++ - 奇怪的解密输出

标签 c++ encryption

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

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
}

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

enter image description here

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

最佳答案

试试这个:

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]" 写入您的文件。因此解密步骤将解密“加密:”部分以及原始加密消息。

关于c++ - 奇怪的解密输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15949343/

相关文章:

c++ - 宏输出说明

c++ - 如何使用 QSqlQuery 重用准备好的语句?

c++ - 结构变量包含什么?

java - 使用 RSA 在 Android 中加密/解密

delphi - Delphi-MadCrypt-加密和解密字符串时出现问题

c++ - 如何全局#define预处理器变量?

c++ - 针对 Linux 编译时 Assimp aiString 为空

c# - .NET 中的加密/解密

java - Objective-C 中的等效 Java javax.crypto 库?

java - Paypal Java SDK - 尝试创建加密按钮 - 不起作用,找不到文档