Help with encryption program

I am making an encryption program and i tried adding a uppercase replacement array and it works when you encrypt it but when you go to decrypt it, after you type the name of the file and press enter it just hangs and doesnt do anything, why? i narrowed it down it has something to do with this while statement in the decrypt function. The code encrypts the uppercase letters just fine but it hangs in the decrypt function.

1
2
3
4
 while((pos = store_text.find(upperReplace[i])) != string::npos)
 {
     store_text[pos] = upper[i];
 }



entire code

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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
#include <iostream>
#include <string>
#include <fstream>
#include <sstream>

using namespace std;

void encrypt()
{
    ifstream fIn;
    ifstream passIn;
    string file_name2;
    string store_text2;
    char lower[27] = {'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','\0'};
    char upper[27] = {'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','\0'};
    char replace[28] = {'♎','×','♋','¼','Ѝ','☩','þ','ß','¤','Œ','Ø','★','유','½','å','¹','°','Ä','ª','¡','♯','✖','','♚','¿','™','✝','\0'};
    char upperReplace[27] = {'³','²','µ','÷','û','ê','ì','Ñ','Î','Ó','Ö','É','È','Ë','Á','¦','Ü','Û','Ù','Õ','ã','á','Ã','Â','Å','Æ','\0'};
    char space = ' ';
    int pos = string::npos;

    cin.ignore(100, '\n');

    cout << "what is the exact name of the file for encryption" << endl;
    getline(cin, file_name2);

    fIn.open(file_name2.c_str());

    stringstream ss(stringstream::in | stringstream::app | stringstream::out);

    while(getline(fIn, store_text2))
    {
        ss << store_text2 << endl;
    }

    store_text2 = ss.str();


    //Find lower case alphabet
    for(int i = 0; i < 27; i++)
    {
        while((pos = store_text2.find(lower[i])) != string::npos)
        {
            store_text2[pos] = replace[i];
        }
/*
        while((pos = store_text2.find(upper[i])) != string::npos)
        {
            store_text2[pos] = upperReplace[i];
        }
*/
        while((pos = store_text2.find(space)) != string::npos)
        {
            store_text2[pos] = replace[27];
        }
    }

    ofstream fOut;

    fOut.open(file_name2.c_str());

    fOut << store_text2;

    cout << "File Encrypted\n" << endl;

    cout << store_text2;

    cin.get();
}

void decrypt()
{
    ifstream fIn;
    string file_name;
    char space = ' ';
    string store_text;
    char lower[27] = {'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','\0'};
    char upper[27] = {'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','\0'};
    char replace[28] = {'♎','×','♋','¼','Ѝ','☩','þ','ß','¤','Œ','Ø','★','유','½','å','¹','°','Ä','ª','¡','♯','✖','','♚','¿','™','✝','\0'};
    char upperReplace[27] = {'³','²','µ','÷','û','ê','ì','Ñ','Î','Ó','Ö','É','È','Ë','Á','¦','Ü','Û','Ù','Õ','ã','á','Ã','Â','Å','Æ','\0'};
    int pos = string::npos;

    cin.ignore(100, '\n');

    cout << "What is the exact name of the file for decryption" << endl;
    getline(cin, file_name);

    fIn.open(file_name.c_str());

    stringstream ss(stringstream::in | stringstream::app | stringstream::out);

    while(getline(fIn, store_text))
    {
        ss << store_text << endl;
    }

    store_text = ss.str();


    for(int i = 0; i < 27; i++)
    {
        while((pos = store_text.find(replace[i])) != string::npos)
        {
            store_text[pos] = lower[i];
        }
/*
        while((pos = store_text.find(upperReplace[i])) != string::npos)
        {
            store_text[pos] = upper[i];
        }
*/
        while((pos = store_text.find(replace[27])) != string::npos)
        {
            store_text[pos] = space;
        }

    }

    ofstream fOut2;

    fOut2.open(file_name.c_str());

    fOut2 << store_text;

    cout << "File Decrypted\n" << endl;

    cout << store_text;

    cin.get();
}

void updates()
{
    cout << "Version 1.0.2 Alpha\n" << endl;

    cout << "The code has been cleaned up a bit and I also fixed some" << endl;
    cout << "spelling errors." << endl;


    cout << "Version 1.0.0 Alpha\n" << endl;

    cout << "This version is the first build of the software" << endl;
    cout << "it encrypts and decrypts text in text files so no one" << endl;
    cout << "but the program can read it.\n" << endl;
}

void password()
{
    string pass;
    bool correct;

    cout << "Please enter your password" << endl;
    getline(cin, pass);
}

int main()
{
    int choice;



    cout << "\n";
    cout << "Encryptor Version 1.1.2 Alpha\n" << endl;

    cout << "What do you want to do?" << endl;
    cout << "1) Encrypt file" << endl;
    cout << "2) Decrypt file" << endl;
    cout << "3) View software update history" << endl;
    cin >> choice;

    if(choice == 1)
    {
        encrypt();
    }
    else if(choice == 2)
    {
        decrypt();
    }
    else if(choice == 3)
    {
        updates();
    }
}

I'm not sure I understand the code, but the for loop is trying each of the characters in the decode string, correct? Since the while loop is just checking for the index 'i', would the 'while' statement be better replaced with an 'if' statement?

Also, you might want to run a debugger in the loop (or add print statements) to see what is happening in the while loop. It might give you a clue as to what is happening.

EDIT:
I copied your code, uncommented the while loop in the decrypt function, built and ran the code using Code Blocks and it decrypted successfully on simple input. In particular I ran it on the text "Once upon a time."
What is the simplest input that decrypt is failing on for you?

EDIT 2:
I just found out that you aren't closing the file at the end of the encryption function, so in my case the encrypted code was never saved.
After I added 'fOut.close();' to the end of encrypt, the encrypted data is saved to the original file and I *am* seeing the hang issue.


-Mike
Last edited on
Hi Mike, thanks for replying, i actually found and fixed the issue, I was trying to use the null terminating character in the upperReplace array and thats why it was hanging. But i fixed it now. Also it wasnt saving because you have to press enter and then it saves it, i noticed that too, and i noticed theat after i encrypt, if i close the program by hitting the red X it doesnt save. Now i am having a different problem I am trying to create a password function but i am having problems, it just wont work. Try it out and youll see what i mean, I want it to check and see if the user has an existing password, and if not then take them to create one, and when they get on again then check to see if it exists and if it does then ask them for it but it doesnt do that it keeps asking for a new passord and then if it asks for you to enter your password and if i enter it wront it takes me to the main program which it isnt supposed to do, its just supposed to quit.

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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
#include <iostream>
#include <string>
#include <fstream>
#include <sstream>

using namespace std;

void encrypt()
{
    ifstream fIn;
    ifstream passIn;
    string file_name2;
    string store_text2;
    char lower[27] = {'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','\0'};
    char upper[27] = {'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','\0'};
    char replace[28] = {'♎','×','♋','¼','Ѝ','☩','þ','ß','¤','Œ','Ø','★','유','½','å','¹','°','Ä','@','¡','♯','^','','♚','*','™','✝','\0'};
    char upperReplace[28] = {'³','²','µ','÷','û','ê','ì','Ñ','Д','Ó','Ö','É','п','⅞','Á','¦','Ü','Û','Ù','Õ','я','Ю','Ã','Ш','₧','Æ','✝','\0'};
    char space = ' ';
    int pos = string::npos;

    cin.ignore(100, '\n');

    cout << "What is the exact name of the file for encryption" << endl;
    getline(cin, file_name2);

    fIn.open(file_name2.c_str());

    stringstream ss(stringstream::in | stringstream::app | stringstream::out);

    while(getline(fIn, store_text2))
    {
        ss << store_text2 << endl;
    }

    store_text2 = ss.str();


    //Find lower case alphabet
    for(int i = 0; i < 27; i++)
    {
        while((pos = store_text2.find(lower[i])) != string::npos)
        {
            store_text2[pos] = replace[i];
        }

        while((pos = store_text2.find(upper[i])) != string::npos)
        {
            store_text2[pos] = upperReplace[i];
        }

        while((pos = store_text2.find(space)) != string::npos)
        {
            store_text2[pos] = replace[27];
        }
    }

    ofstream fOut;

    fOut.open(file_name2.c_str());

    fOut << store_text2;

    cout << "File Encrypted\n" << endl;

    cout << store_text2;

    fOut.close();

    cin.get();
}

void decrypt()
{
    ifstream fIn;
    string file_name;
    char space = ' ';
    string store_text;
    char lower[27] = {'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','\0'};
    char upper[27] = {'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','\0'};
    char replace[28] = {'♎','×','♋','¼','Ѝ','☩','þ','ß','¤','Œ','Ø','★','유','½','å','¹','°','Ä','@','¡','♯','^','','♚','*','™','✝','\0'};
    char upperReplace[28] = {'³','²','µ','÷','û','ê','ì','Ñ','Д','Ó','Ö','É','п','⅞','Á','¦','Ü','Û','Ù','Õ','я','Ю','Ã','Ш','₧','Æ','✝','\0'};
    int pos = string::npos;

    cin.ignore(100, '\n');

    cout << "What is the exact name of the file for decryption" << endl;
    getline(cin, file_name);

    fIn.open(file_name.c_str());

    stringstream ss(stringstream::in | stringstream::app | stringstream::out);

    while(getline(fIn, store_text))
    {
        ss << store_text << endl;
    }

    store_text = ss.str();


    for(int i = 0; i < 27; i++)
    {
        while((pos = store_text.find(replace[i])) != string::npos)
        {
            store_text[pos] = lower[i];
        }

        while((pos = store_text.find(upperReplace[i])) != string::npos)
        {
            store_text[pos] = upper[i];
        }

        while((pos = store_text.find(replace[27])) != string::npos)
        {
            store_text[pos] = space;
        }

    }

    ofstream fOut2;

    fOut2.open(file_name.c_str());

    fOut2 << store_text;

    cout << "File Decrypted\n" << endl;

    cout << store_text;

    fOut2.close();

    cin.get();
}

void updates()
{
    cout << "Version 1.1.2 Alpha\n" << endl;

    cout << "I added uppercase letter support but still no support" << endl;
    cout << "for numbers or symbols, thos will be added at a later time" << endl;

    cout << "Version 1.0.2 Alpha\n" << endl;

    cout << "The code has been cleaned up a bit and I also fixed some" << endl;
    cout << "spelling errors.\n" << endl;


    cout << "Version 1.0.0 Alpha\n" << endl;

    cout << "This version is the first build of the software" << endl;
    cout << "it encrypts and decrypts text in text files so no one" << endl;
    cout << "but the program can read it.\n" << endl;
}

int password()
{
    string NewPword;
    string pass;
    bool correct;
    bool newPass = true;

    ofstream passOut;

    passOut.open("PassKey.txt");

    ifstream passIn;

    passIn.open("PassKey.txt");
    passOut << newPass << endl;
    passIn >> newPass;

    if(newPass == true)
    {
        cout << "Please create a password" << endl;
        getline(cin, NewPword);

        newPass = false;

        passOut << newPass << endl;
        passOut << NewPword << endl;
    }
    if(newPass == false)
    {
        cout << "Please enter your password" << endl;
        getline(cin, pass);

            passIn >> NewPword;

            if(NewPword == pass)
            {
                cout << "Proceed" << endl;
            }
            else if(NewPword != pass)
            {
                cout << "Error!! now closing" << endl;
            }
    }
}

int main()
{
    password();

    int choice;

    cout << "\n";
    cout << "Encryptor Version 1.1.2 Alpha\n" << endl;

    cout << "What do you want to do?" << endl;
    cout << "1) Encrypt file" << endl;
    cout << "2) Decrypt file" << endl;
    cout << "3) View software update history" << endl;
    cin >> choice;

    if(choice == 1)
    {
        encrypt();
    }
    else if(choice == 2)
    {
        decrypt();
    }
    else if(choice == 3)
    {
        updates();
    }
}
Hi Ch1156,

I did the following:
1. Added fOut.close(); to the end of the encrypt function.
2. Uncommented the commented while loop in the decrypt function.
3. Changed all 3 while statements in decryption into if statements.

The good news is that your program no longer hangs.
However, the decryption failed halfway through.
In other words, if the input is "Once upon a time." comes back similar to "Once up$&#()-^-"
I wasn't able to copy the output so the encrypted characters listed here are not the characters you output, but you get the idea.
Hi,

Is line 195 the line that you are expecting to end the program?
It's just a print statement, you need to add the exit() function to exit the program.
By the way. I'm a C programmer, new to C++.
Let me check if C++ uses something other than exit().
Dont worry about that i fixed it, its the password function im having real trouble with :(
Hi Ch1156,

But line 195 is in the password function, right?
Maybe you're replying to an earlier post?
Oh sorry yes it must have been an earlier post, everything in the password function does not work.
Bump please help.
ok i just rebuilt the entire password function with good success except when im told to enter the password after i created it, it just tells me that the password is incorrect even though its right. also when it says its wrong its supposed to close the program but it continues on, how do i fix these? here is the password function

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
int password()
{
    string newPassword;
    string existingPassword;
    bool passwordCreated;
    ifstream passIn;
    ofstream passOut;

    passIn.open("PassKey.txt");

    passIn >> passwordCreated;

    if(passwordCreated == 1)
    {
        passIn >> passwordCreated;
        passIn >> newPassword;

        cout << "Please enter your password" << endl;
        getline(cin, existingPassword);

            if(newPassword != existingPassword)
            {
                cout << "Incorrect password" << endl;
                return 0;
            }
            else if(newPassword == existingPassword)
            {
                cout << "Correct password, Proceed" << endl;
            }
    }

    passOut.open("PassKey.txt");
    passOut << passwordCreated << endl;

    //If password is not set up yet, create one

    if(passwordCreated == 0)
    {
        passOut.close();
        passOut.open("PassKey.txt");
        cout << "Please create a password" << endl;

        do
        {
            getline(cin, newPassword);

            if(newPassword.length() <= 4)
            {
                cout << "Your password must be more than 4 characters long!!\n" << endl;
            }
        }
        while(newPassword.length() <= 4);

        passOut << true << endl;
        passOut << newPassword << endl;
        cout << "Your password is: " << newPassword << endl;

        passOut.close();
    }
}
bump
Ok i fixed the password problem, now the only problem i have is that it keeps going back to main even if i put return 0; atht eh end of the function. why?
Topic archived. No new replies allowed.