C++ Encryption / Decryption Issues

Ladies and Gentlemen;

I'm very new to programming and have encountered an issue that is over my head. I've fought with this assignment up to the deadline and have received no help from my current instructor on the issue, so I'm begging fro assistance. Again I'm new, so please don't flame me too bad for my formatting.

The program is supposed to take a txt file, extract the contents, encrypt it with a user input key, and write it's contents to another txt file. I'm still fighting the encryption side, I'm sure the decryption is even worse at this point but I'm not going to mess with it until I get the encryption side finished up.

*PROBLEM*
-Encryption side only (right now, I know decryption is bad aswell)
It reads the file as intended, but then everything goes out the window and it outputs a very odd string of "=======" on the screen, but a ascii jumble in the file itself.

Any assistance would be greatly appreciated.

Here is what I have so far (again please take it easy on me I know it is far from perfect).

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

using namespace std;

const int highLimit = 129;
char menuChoice;
char key[highLimit];
string inTxtLoc, outTxtLoc, inMsg, outMsg;
fstream inTxtFile;
fstream outTxtFile;
int countMax;


char getMenuChoice (char);
char getKey (char [], int);
void getEncrypt (char [], int, string, string, string, string);
void runDecrypt (char [], int, string, string, string, string); 

int main ()
{

	do
	{
	cout << "|----------------------------------------------------------------------|\n";
	cout << "|                    Secure Message Delivery System                    |\n";
	cout << "|----------------------------------------------------------------------|\n";
	cout << "|      This program will ENCRYPT and DECRYPT the given message.        |\n";
	cout << "|----------------------------------------------------------------------|\n";
	cout << "|                    Press E to ENCRYPT message.                       |\n";
	cout << "|                    Press D to DECRYPT message.                       |\n";
	cout << "|                  Or press Q to exit the program.                     |\n";
	cout << "|----------------------------------------------------------------------|\n\n\n";;
	cout << "Please enter your choice (E, D, or Q) now :\n\n";

	menuChoice = getMenuChoice(menuChoice);
	if (menuChoice == 'e' || menuChoice == 'E')
		{
			getKey(key, countMax);
			getEncrypt(key, countMax, inTxtLoc, outTxtLoc, inMsg, outMsg);
		}
	else if (menuChoice == 'd' || menuChoice == 'D')
		{
			getKey(key, countMax);
			runDecrypt(key, countMax, inTxtLoc, outTxtLoc, inMsg, outMsg);
		}
	
	}
	while ( menuChoice != 'q' || menuChoice != 'Q');
	
	cout << "Press enter to exit.";
    cin.ignore(2);
 

}

char getMenuChoice (char)
{
	char menuChoice;

	cin >> menuChoice;
	cout << endl;

		switch(menuChoice)
		    {
			case 'd':
			case 'D':
				return menuChoice;
					break;
			case 'e':
			case 'E':
				return menuChoice;
					break;
			case 'q':
			case 'Q':
				 exit(0);
					break;
			default:        
				 cout << "\n You must choose D, E, or Q.  Please choose again.\n\n" << endl;
				 getMenuChoice(menuChoice);
					break;
			}
}

char getKey(char key[], int countMax)
{

	cout << "Input up to 128 characters (lower case letters a-z) for the encryption key :\n\n";
	cin >> key;
	countMax = strlen(key);
	cout << endl << endl;

	return key[129], countMax;

}

void getEncrypt(char key[], int countMax, string inTxtLoc, string outTxtLoc, string inMsg, string outMsg)
{
	int inCount;
	int eCount = 0;
	
	cout << "Please enter the location of the file you wish to encrypt:\n\n";
	cin.ignore();
	cin >> inTxtLoc;
		inTxtFile.open(inTxtLoc);	
			getline(inTxtFile, inMsg);
			inCount = inMsg.length();

	cout << "\n\n";
	cout << "'" << inMsg << "'" << endl << "\nIs to be encrytpted\n\n";
	cout << "Please enter the location of the file to save the encryption: \n\n";
	cin >> outTxtLoc;
		outTxtFile.open(outTxtLoc);
			while (eCount < inCount)
			{
				for (eCount = 0; eCount <= inCount; eCount++)
				{
				char swap;
				inTxtFile.get(swap);
				swap = swap + key[eCount];
				swap = swap - key[0];
				outTxtFile << swap;
				}
			} 
		getline(outTxtFile, outMsg);
	cout << "\n\n";
	cout << "File was encrypted to read :\n\n" << outMsg << endl;



	inTxtFile.close();
	outTxtFile.close();

    cout << "\n  Press the ENTER to return to menu" << endl;
        cin.ignore(2);
		
}

void runDecrypt(char key[], int countMax, string inTxtLoc, string outTxtLoc, string inMsg, string outMsg)
{
	int inCount;
	int dCount;

	cout << "Please enter the location of the file you wish to decrypt:\n\n";
			cin >> inTxtLoc;
				inTxtFile.open(inTxtLoc);
				getline(inTxtFile, inMsg);
				inCount = inMsg.size();
				cout << "\n\n";
		cout << """" << inMsg << """" << endl << "Is to be decrytpted.\n\n";

		cout << "Please enter the location of the file to save the decryption: \n\n";
		cin >> outTxtLoc;
			outTxtFile.open(outTxtLoc);
				getline(outTxtFile, outMsg);
		cout << "\n\n";
			cout << "File was decrypted, and reads : " << outMsg << endl;
		cout << "\n\n";

		while (inTxtFile.good())
			{
				int dCounter;
				for (int dCounter = 0; dCounter < countMax; dCounter++)
					{
					char swap;
					inTxtFile.get(swap);
					swap = inMsg[dCount] - key[dCount];
					outTxtFile << swap;
					}
			}
	inTxtFile.close();
	outTxtFile.close();

    cout << "\n  Press the ENTER to return to menu" << endl;
        cin.ignore(2);

}
Last edited on
In getKey, your return statement doesn't do what you think it does. You can't return multiple things from a function like that, plus neither type is even the same as the return type.

Thus, anything that depends on that function is going to break.
And as it all depends on that function I'm all kinds of jacked up. Ok, I see that I'm not even using the variable kCounter so I can ditch that. But I was under the impression that I was returning a cstring there. Should I have the type set to string instead of char?


*edited*
Having just messed with my above though, that didnt seem to change anything else. But I have removed the kCounter from the code in the origin post.

I'm not saying your wrong firedraco, I'm just saying I'm apparently too inexperienced to see the exact issue there.
Last edited on
Ok, an update.

After stepping through the process my swap variable in the getEncrypt function is not pulling a variable as I though it would. I'm not sure exactly why, but I will continue to play with it until I find out what is going on.

*edited*
I'm fairly certain my fstream is all screwed up aswell. I have never used them up to this point an now have it dropped on me as a must, with no instruction. I'm now getting my key printed in the first several spaces of the output file so I'm getting somewhere I guess.
Last edited on
firedraco is correct. return doesn't work like that.
You can only return one variable.
You can, however, pass references to the function instead, which would work without much modification since arrays pass by reference by default.
However, I'd like to know what exactly the problem is? I've read your original post, but it doesn't seem very detailed. Is your encryption algorithm a simple cipher where you add a value to a character? How exactly does the key affect the digest?
Once you share this information, I'd be glad to help you out with solving your problem, and either I or someone else will get you in the right direction as fat as fstream objects go.
Xismn,

I've actually taken Firedraco's advice and made a few modifications in that area but am still having issues. My drop dead time for this is in about two hours but I dont care about the grade at this point. I'm more interested in finding the solution to my problem.

Now all I was working toward at this point was modifying the input file with the key itself (and failing miserably I might add, which is sad considering I've been messing with it for a week now).

The base I was running off of were two files on my c:. in.txt, and out.txt. In said simply "Hello!". And out is coming out like this "ÌÍÏè‡&ÅÍÍÍÍÍÍÍÍ....." the final character there repeats for another several kb then it exits the way it was intended lol.

My big question is where am I going wrong to that point.

To be honest I feel alot of this stuff is way over my head, and I've been doing quite well in this class. But that is neither here nor there.

Now this is what it is supposed to do (again it doesn't do half this stuff but I want it to so I can see where I'm going wrong):
1. Must ask the user to for file to encrypt / decrypt.
2. Must get an encryption key from the user, all lower case alphabetic characters.
3. Must have a function which takes the encryption, all lower case letters, resulting in encryption map array.
4. Must have function which takes the encryption key and creates a decryption map from it.
a. Each character in the encryption key string, subtract the lower case letter 'a'.
b. Then subtract result from 26 and store the value in the corresponding decryption map array.
5. Must have function which will do the encryption or decryption transformation. This function takes the following parameters:
a. A constant C string containing the line of text to be transformed.
b. A constant C character array which contains the encryption or decryption map.
c. An integer which contains the length of the encryption map.
d. A string reference (output) which will contain the encrypted / decrypted string.
i. The core of the encryption / decryption algorithm is as follows:
1. For each character in the input text do the following:
a. if not alphabetical, add to end of output string
b. if lower case
i. Subtract the character 'a' from the character get the ith % map length element from the map
ii. Add it to the character adjust the value of the character % 26 to keep it within the alphabet
iii. Add the character 'a' to the character
iv. add the encrypted character value to the end of the output string
c. if character is upper case do the same thing as for lower case except use 'A' instead of 'a'
6. For decryption, main program should create an ifstream for the file to be decrypted.
a. It should use the getline method of the ifstream to:
i. Read lines from the file
ii. Call the encryption / decryption function with the line to be decrypted
iii. Display the string which contains the result of the encryption / decryption function call.
iv. Repeat until the ifstream reaches the end of the file, then close the ifstream.

7. For encryption, the main program should create an ifstream for the file to be encrypted.
a. It should also create an ofstream for the file where the encrypted result will be stored.
i. The file name for this file can be gotten from the user or can be the input file name with a special extension added at the end.
b. The getline method of the ifstream is used to read lines from the input file.
i. Then the encryption / decryption function is called to encrypt the line.
ii. Display the string containing the result and write the string to the ofstream.
iii. Close the ifstream and ofstreams when finished.

Topic archived. No new replies allowed.