Problem in Encryption / Decryption

I'm trying to implement simple symmetric key cryptography algorithm. In which provided a key and message stream it just XORs the character of key and message one by one and encrypts them and similarly for decryption.
In my code while reading the file, after the all characters are read from the file provided as input an extra last character is read (this is before EOF as I've used while(fp) //fp is the file stream to detect EOF) which gets XOR with the key element.
Because of this when I decrypt using the same logic the extra character again gets XOR and gives me a junk character at the end of the message.
How do I remove this last character from getting XORed?
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
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main()
{
	int opt;
	string key="asdfghjklpo";	//Key for encryption/decryption
	cout<<"Enter your option"<<endl;
	cout<<"1. Encrypt\n2. Decrypt"<<endl;
	cin>>opt;
	cin.ignore();	//To flush the '\n' fron the stream
	char ch;
	
	switch(opt)
	{
		case 1:
		{
			cout<<"Enter the file name containing message stream : ";
			string ifn,ofn;
			getline(cin,ifn);
			ofn=ifn;
			ofn.append("_enc");	//new file name with _enc at the end
			
			fstream msg,op;
			msg.open(ifn.c_str(),ios::in);	//converting string to (char *) using c_str()
			op.open(ofn.c_str(),ios::out);	//converting string to (char *) using c_str()
			
			int i=0;
			while(msg)
			{
				msg.get(ch);
				ch=char(int(ch)^int(key[i]));	//key XOR message
				op.put(ch);
				i=(++i)%key.size();		//The key rotates if message is greater than
											//key size
			}
			msg.close();
			op.close();
			break;
		}

		case 2:
		{
			string k;
			cout<<"Enter the key"<<endl;
			getline(cin,k);
			
			if(!key.compare(k))		//Key Authentication
			{
				cout<<"Enter the file name containing encrypted message stream : ";
				string ifn,ofn;
				getline(cin,ifn);

				ofn=ifn;
				ofn.append("_dec");	//new file name with _dec at the end
				
				fstream msg,op;
				msg.open(ifn.c_str(),ios::in);	//converting string to (char *) using c_str()
				op.open(ofn.c_str(),ios::out);	//converting string to (char *) using c_str()
				
				int i=0;
				while(msg)
				{
					msg.get(ch);
					ch=char(int(ch)^int(key[i]));	//encrypted_message XOR key
					op.put(ch);
					i=(i+1)%key.size();		//The key rotates if message is greater than
												//key size
				}
				msg.close();
				op.close();
			}
			else
				cout<<endl<<"Entered key not valid........."<<endl;
			break;
		}
	}

}


I tried to debug the code and found that the extra character was \b (ASCII 8) on the encryption side, so i tried for an if condition to check the same but even then it didn't work.
Last edited on
The problem is solved, I just added an extra if condition inside the while loop for checking the eof.
1
2
3
4
5
6
7
8
9
10
while(msg)
			{
                                if(msg.eof())
                                         break;
				msg.get(ch);
				ch=char(int(ch)^int(key[i]));	//key XOR message
				op.put(ch);
				i=(++i)%key.size();		//The key rotates if message is greater than
											//key size
			}
Topic archived. No new replies allowed.