使用地图的简单加密/解密程序

时间:2019-10-02 02:43:22

标签: c++ encryption iterator maps

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

#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;
}

这是提供的字典

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

0 个答案:

没有答案
相关问题