File Encryption Bug

Decrypted file has the same message as the original except it adds a few letters
(usually 2). For example "abc123" = "abc12343". So far I couldn't find any reason as to why it does this, any help is appreciated

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
#include <iostream>
#include <fstream>
#include <string>


using namespace std;

void encryptOrDecrypt(string fileloc, string nfileloc, char mode);

int main()
{
	string filelocation;
	string newfilelocation;
	char sh;

	cout << "Original file location:";
	getline(cin, filelocation);
	cout << "New file location:";
	getline(cin, newfilelocation);

	cout << "Mode:";
	cin >> sh;

	encryptOrDecrypt(filelocation, newfilelocation, sh);
	
	cout << "Operation successful" << endl;

	system("PAUSE");
	return 0;
}

void encryptOrDecrypt(string fileloc, string nfileloc, char mode) {
	fstream input(fileloc.c_str(), ios::in);
	fstream output(nfileloc.c_str(), ios::out);

	char get;

	if (mode == 'E' || mode == 'e') {
		while (input.good()) {
			input.get(get);
			get += 1;
			output << get;
		}
	}
	else if (mode == 'D' || mode == 'd') {
		while (input.good()) {
			input.get(get);
			get -= 1;
			output << get;
		}
	}
	input.close();
	output.close();
}
So far I couldn't find any reason as to why it does this, any help is appreciated

Can you explain it in more details? We would like to hear more of the specific bug you are encountering.
Well, as I said, I create a text file, encrypt it, and when I decrypt it I can notice
that there are some letters added to the original text. I can give you some
examples of what letters it adds, but you can try it out for yourself (I compiled the
code in Visual Studio 2015).

abcdefgh12345678 --> abcdefgh1234567898
example --> examplefe
VisualStudio --> VisualStudiopo
A-B_C|D --> A-B_C|DED

It looks like it adds the letter after the last one + last letter.
So if last letter is "e" then it adds "f" and "e" again

I'm going to completely rewrite the code and if I'm successful I will post the new
code
Last edited on
Here is the new one that worked.

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
void encryptOrDecrypt(string fileloc, string nfileloc, char mode) 
{
	fstream input(fileloc.c_str(), ios::in);
	fstream output(nfileloc.c_str(), ios::out);

	char get;

	if (mode == 'E' || mode == 'e') 
	{
		while (input >> get) 
		{
			get += 1;
			output << get;
		}
	}
	else if (mode == 'D' || mode == 'd') 
	{
		while (input >> get) 
		{
			get -= 1;
			output << get;
		}
	}

	input.close();
	output.close();
}


Your old code should be equivalent to this :
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
void encryptOrDecrypt(string fileloc, string nfileloc, char mode) {
	fstream input(fileloc.c_str(), ios::in);
	fstream output(nfileloc.c_str(), ios::out);

	char get;

	if (mode == 'E' || mode == 'e') {
		while (true) {
			input.get(get);

                        if(!input.good()) break;

			get += 1;
			output << get;
		}
	}
	else if (mode == 'D' || mode == 'd') {
		while (true) {
			input.get(get);

                        if(!input.good()) break;

			get -= 1;
			output << get;
		}
	}
	input.close();
	output.close();
}
ty
Topic archived. No new replies allowed.