File decryption help please

Hi guys,
I wrote this program for file encryption and decryption but my decryption command hangs. How do i fix it i am so lost. Thanks!

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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
 #include<iostream>
#include<fstream>
#include <stdlib.h>
#include<string.h>

using namespace std;

int main()
{
    string source;
    string destination;
    int menu;
    while(true)
    {
        cout<<"1. Enter source filename"<<endl
            <<"2. Enter destination filename"<<endl
            <<"3. Encrypt file"<<endl
            <<"4. Decrypt file"<<endl
            <<"0. Exit"<<endl;
        cin>>menu;
        switch (menu)
        {
        case 1:
            {
                cout <<"1: Enter source filename" << endl;
                cin >> source;
            }
            break;
        case 2:
            {
                cout << "2: Enter destination filename" << endl;
                cin >> destination;
            };
            break;
        case 3:
            {
            if((source.empty())||(destination.empty()))
                {
                cout<<"You havenot entered source file"<<endl;
                }
            else
                {
                ifstream file1;
                file1.open(source.c_str());
                ofstream file2;
                file2.open(destination.c_str());
                char c;
                while(!file1.eof())
                {
                file1>>c;
                char newc= int(c)+12;
                file2<<newc<<" ";
                }
                file1.close();
                file2.close();
                }
                }
                break;
        case 4:
            {
            if((destination.empty())||(source.empty()))
                {
                cout<<"You have not entered destination file"<<endl;
                }
            else
                {
                ofstream file1;
                file1.open(source.c_str());
                ifstream file2;
                file2.open(destination.c_str());
                char c;
                while(!file2.eof())
                    {
                    file2>>c;
                    char newc=int(c)-12;
                    file1<<newc;
                    }
                file1.close();
                file2.close();

                }
                }
            break;
        case 0:
            {
            exit (EXIT_SUCCESS);
            }
            break;
            }
    }
}
Last edited on
1
2
char newc= int(c)+12;
 file2<<newc<<" ";


you are adding a space between every char in the output file. When you take it back, this make the original file different...
Topic archived. No new replies allowed.