Decoding Process

For some reason The file doesnt decode.
This is the file
https://www.dropbox.com/s/lex15ntjptlr0fa/test.txt

first command argument is 12345 and second is name of file which is test.txt

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
  #include <iostream>
#include <iomanip>
#include <cctype>
#include <fstream>
#include <sstream>
#include <string>
#include <vector>
#include <cstdlib>
#include <sstream>
#include <string>
//takes three command line argument 
// a key, (decimal number), a name of an endcoded data input file ,a name of clear text output file

using namespace std;
char decode (char aChar)
{

	int result = 1 + rand() % 127;
	result = aChar - result;
	if (result < 0)
	{
		result = result +128;
	}
	return result;
}

void decodeFile(ifstream&  lInput, int aKey)
{
	char a;
	srand(aKey);
	string output;
	while (lInput.good())
	{
		lInput >> a;
		decode(a);
		if ( isupper(a) )
		{
			a = 'A' + (a - 'A' + decode(a)) % 26;
		}
		else
		{
			if ( islower(a) )
			{
				a = 'a' + (a - 'a' + decode(a)) % 26;
			}
		}
		output = output + a;
	}
	cout << output ;
	
}
	

int main (int argc, char* argv[])
{
	ifstream lInput;	// declare an input file variable (object)
	lInput.open( argv[2], ifstream::binary ); // open an input file (binary)
	int aKey;
	aKey = atoi(argv[1]);
	
	//cout << aKey;
	
	decodeFile(lInput, aKey);	
	return 0;
}

Line 35 maybe should be...
a = decode(a);
?

Also loop on operator>> :
while(lInput >> a)
Last edited on
Is it how you wanna do it ??
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
while (lInput.good())
	{
		while (lInput >> a) 
			{
		a = decode(a);
		if ( isupper(a) )
		{
			a = 'A' + (a - 'A' + decode(a)) % 26;
		}
		else
		{
			if ( islower(a) )
			{
				a = 'a' + (a - 'a' + decode(a)) % 26;
			}
		}
		output += a;
		}
	}
	cout << output ;
Remove while (lInput.good())

Also your decoding algorithm is very "vague".
If you show your encoding algorithm, you could have a better way to decode it (using rand() is not suggested.)
Last edited on
Topic archived. No new replies allowed.