Decryption!

Write a program to decrypt a file named “encrypted text.txt” by using the key found
in a file named “key.txt”. Write the decrypted text to a file called “decrypted text.txt”. The
key file contains two letters per line: the first is the character in the encrypted text, and the
second is the corresponding decrypted character.


#include <iostream>
#include <fstream>
#include <string>

using namespace std;
void decipher(char input, char output, char key);
int main(){

ifstream ifs;
ifs.open("encrypted_text.txt");

if( ! ifs.is_open()){
cout << "The input file could not be opened!" << endl;
return 0;
}

ofstream ofs;
ofs.open("decrypted_text.txt");

if(!ofs.is_open()){
cout << "The output file failed to open!" << endl;
}


string line;

while(getline (ifs,line)){
line.push_back(line);

}
return 0;

}
This is as far as I got...not where to go from here
Reported those idiots... anyway..

file and example input files encrypted.txt , key.txt can be seen and tested at https://repl.it/repls/UntriedFeminineRar

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include <map>

using namespace std;

int main()
{
    string key_file_name("key.txt");
    ifstream ifs(key_file_name);
    if (!ifs)
    {
        cout << "Error: unable to open \""<<key_file_name<<"\".\n";
        return -1;
    }
    
    char a,b;
    map<char,char> decoder;
    while (ifs >> a >> b)
    {
        decoder[a] = b;
    }
    ifs.close();

    string encrypted_file_name("encrypted.txt");
    ifs.open(encrypted_file_name);
    if (!ifs)
    {
        cout << "Error: unable to open \""<<encrypted_file_name<<"\".\n";
        return -2;
    }
    
    // Imitate output file
    ostringstream oss;
    
    char c;
    while (ifs >> noskipws >> c)
    {
        c = tolower(c);
        if (decoder.count(c))
            oss << decoder[c];
        else
            oss << c;
    }
    
    cout << "Decoded:" << endl;
    cout << oss.str();

    return 0;
Topic archived. No new replies allowed.