Too many and defective bytes after en-/decryption

Hi there,

I've implemented a small XOR-Encryption, BUT when I encrypt and decrypt again, there are always 1-2 Bytes too much there, and those are defective as well (some characters that aren't of UTF-8, for instance).

The more often I en- and decrypt the bigger the file gets and the more defective as well :( I dont want this, so, please help me and tell me whats wrong with my program:

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
/* 
 * File:   main.cpp
 * Author: lukas
 *
 * Created on 24. Februar 2013, 16:37
 */

#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <fstream>


using namespace std;

char key[256] = "0123456780";
int keypos=0;                   // Schlüsselposition

ifstream ein;
ofstream aus;


char encc(char enc){

        
        enc = enc ^ key[keypos];
        
        keypos++;
        
        if (keypos == (strlen(key)-1)){
            
            keypos = 0;
            
        }
        
    
    
    return enc;


}

int main(int argc, char** argv) {

    
    cout << "+---------------------------+" << endl << "|Binary XOR-Encryption|" << endl << "+---------------------------+" << endl;
    cout << "\nEn- and Decrypt\n" << endl;
    cout << "Input: input.xvs Output: output.xvs ";
   
    cout << endl;
    
    ein.open("input.xvs",ios::binary);
    aus.open("output.xvs",ios::binary);
    
 
    cout << "Output: ";
    
    for (int k = 0; k < strlen(key);k++){cout << key[k];}
    cout << endl << "Encryption starts...";
    

  while (ein.good())          // loop while extraction from file is possible
  {
    char c = ein.get();       // get character from file
    if (ein.good())
        cout << c;
      aus.put(encc(c));       // write encrypted char to the output file
  }

  ein.close();
  aus.close();
    
  
          
    
    return 0;
}


Many thanks in advance,

Lukas
Last edited on
In the last iteration of the while loop, when ein.good() becomes false, line 68 will still run adding an extra character to the end of the file.
thanks, I tried do {...} while() now, but it didnt help :(

How can I avoid that issue? It is strange, because if it runs one time too often, then it doesnt have an input, how can it give an output, then?!

How to avoid the extra run?

Lukas
Last edited on
One way is to make line 68 part of the if statement.
1
2
3
4
5
6
7
8
9
while (ein.good())
{
	char c = ein.get();
	if (ein.good())
	{
		cout << c;
		aus.put(encc(c));
	}
}


If you use the version of get that returns a reference to the stream object you can get rid of the if statement completely.
1
2
3
4
5
6
char c;
while (ein.get(c))
{
	cout << c;
	aus.put(encc(c));
}
Last edited on
aaaaaah, tyvm :)

it worked perfectly (i tried the 1st)


Thank you very much for your help,

Lukas
Last edited on
Topic archived. No new replies allowed.