Encryption - Decryption not working correctly

So I have a long text file and the Decryption function only reads in 3 lines and I have no idea why.
The Decrypt and the Encrypt converts ints to unsigned char and returns it.
The Encryption encrypts 1 char and adds the step to the key
Thank you to anyone who has an idea what can be the problem

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
  void Source::DecryptFile_2(const string & fileIn, const string & fileOut, int keyU)
{
	ifstream In;
	In.open(fileIn);
	ofstream Out;
	Out.open(fileOut);

	vector<string> array;
	string line;
	while (getline(In, line, '\n'))
	{
		array.push_back(line);
	}

	int letters = 0;
	for (unsigned int i = 0; i < array.size(); i++)
	{
		for (unsigned int k = 0; k < array[i].length(); k++)
		{
			++letters;
		}
	}

	for (unsigned int i = 0; i < array.size(); i++)
	{
		for (unsigned int k = 0; k < array[i].length(); k++)
		{
			Out << Decrypt(array[i][k], keyU);
			keyU += step;
		}
		Out << '\n';
	}

	In.close();
	Out.close();
}


1
2
3
4
5
6
7
char Source::Decrypt(char ch, int keyU)
{
	int used_key = keyU;
	int integer = (ch - used_key);
	unsigned char return_value = integer;
	return return_value;
}
check the basics.
what is in the file you opened? Did it encrypt all the lines into it correctly (stop after encrypt and exit, open encrypted file in text or hex editor to see if it is right)? Did you close or flush it after writing it (an open file may not be fully written to disk, you can flush or close it before reopen to avoid issues)?

xor encryption/decryption (result file is binary, not text, though, is the only aggravation to it) can be done in about 10 lines of code. These exotic newspaper puzzle page algorithms are just too much aggravation, to be honest. That algorithm is
- get file size
- read entire file to a buffer
- set up <random> using (int) of user's password (hashed somehow to get int) as seed
- xor each byte with random
- write file
- self reverses, just re-run with same password to decrypt.
Last edited on
Thank you for the comment but I close the files and I took a look at the ascii and where it stops reading its only the end line code 0D 0A (because im using Windows for this program). I wrote in the clears but didnt help a bit.
This is how I make the file:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
void Source::EncryptFile_2(const string & fileIn, const string & fileOut, int keyU)
{
	ifstream In;
	In.open(fileIn);
	ofstream Out;
	Out.open(fileOut);
	string line;
	while (getline(In, line))
	{
		for (unsigned int i = 0; i < line.length(); i++)
		{
			Out << Encrypt(line[i], keyU);
			keyU += step;
		}
		Out << '\n';
	}

	In.close();
	Out.close();

	In.clear();
	Out.clear();
}
The ASCII 26 (SUB) was that ended the reading. Now it works... I think.
Topic archived. No new replies allowed.