C++ 使用映射的简单加密/解密程序

C++ 使用映射的简单加密/解密程序,c++,encryption,iterator,maps,C++,Encryption,Iterator,Maps,我应该写一个简单的程序,使用两个映射来加密/解密字符串“Hello World”。加密映射的数据通过命令行参数给出。我已经成功地将字典文件中的数据放入一个映射,并且我已经准备好了解密映射。但是,我不知道如何让程序正确地进行加密。我的输出应该是“Fuggy,typegq”,因为我的代码后面显示了加密字典。格式是将第一个明文字符映射到第二个密文字符。我得到的输出是Hello World中的“h”的空白,后面是“iBVDVBC”。我确信空白是因为我的字典只包含小写字母。如何改进代码以获得正确的输出 #

我应该写一个简单的程序,使用两个映射来加密/解密字符串“Hello World”。加密映射的数据通过命令行参数给出。我已经成功地将字典文件中的数据放入一个映射,并且我已经准备好了解密映射。但是,我不知道如何让程序正确地进行加密。我的输出应该是“Fuggy,typegq”,因为我的代码后面显示了加密字典。格式是将第一个明文字符映射到第二个密文字符。我得到的输出是Hello World中的“h”的空白,后面是“iBVDVBC”。我确信空白是因为我的字典只包含小写字母。如何改进代码以获得正确的输出

#include<iostream>
#include<fstream>
#include<map>
#include<cctype>
#include <string>
using namespace std;

int main(int argc, char *argv[]){

map<char, char> encryptDictionary;
map<char, char> decryptDictionary;
string dictionaryString;
string encryptThis = "Hello world";
fstream file;
int size = encryptThis.size();

file.open(argv[1]);

//Populates encryption dictionary map
while (file >> dictionaryString){
   encryptDictionary[dictionaryString[0]] = dictionaryString[1];
}

//Populates decryption dictionary map
 for(auto itr = encryptDictionary.begin(); itr !=encryptDictionary.end(); itr++){
       decryptDictionary [ itr -> second] = itr -> first;
       }


for (int i = 0; i < size; i++){
for (auto itr = encryptDictionary.begin(); itr !=encryptDictionary.end(); itr++) {

   encryptThis[i] = (encryptDictionary.find(encryptThis[i])->second);

}
}

  cout << encryptThis;
  file.close();
return 0;
}

不要在映射上迭代,因为同一项被多次替换(“h”变成“f”,然后变成“k”,等等),所以删除内部循环。您可以使用
tolower()
将输入字符串转换为小写。不要在映射上迭代,因为同一项会被替换多次(“h”变为“f”,然后是“k”,等等),所以请删除内部循环。您可以使用
tolower()
将输入字符串转换为小写。
an
bx
cs
dq
eu
fk
gj
hf
iz
jm
ko
lg
mw
ni
oy
pl
qa
rp
sr
tb
ud
vh
wt
xe
yv
zc