XOR encryption of text files

guys i read this article on XOR encryption
http://www.cplusplus.com/forum/articles/38516/ and tried to implement it for text files, but there are a few hurdles...
I am just learning the c++ now, so can you point out any errors,
the same class i have written is intended for both encoding and decoding
80 characers at a time until EOF is reached
in the decoded file some characters are decoded and some are not

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
class CRYPT
{
private:
    char in[80];
    unsigned int a,b,c;
    unsigned char d;
public:
    unsigned int index;

    CRYPT(void)
    {
        cout << "enter 3 integers(0 to 10) : ";
        cin >> a >> b >> c;
        index=1;
    }
    void encode(void)
    {
        long int len=strlen(in);
        long int i=0;
        while(i<=len)
        {
            in[i]^=(unsigned char)((a+b)*index);
            //index++;
            i++;
            if(i==len) break;
            in[i]^=(unsigned char)((c-a)*index);
            //index++;
            i++;
            if(i==len) break;
            in[i]^=(unsigned char)((2*c)*index);
            //index++;
            i++;
            if(i==len) break;
            //index++;
        }
    }
    int getein(ifstream &fin)
    {
        fin.get(in,80,EOF);
        if(fin.bad())
        {
            cout << endl << "bad input" << endl;
        }
        return fin.eof();
    }
    void puteout(ofstream &fout)
    {
        fout << in << endl;
    }
};
There are a number of problems. Change your 80 to 4 and step thru it in the debugger.
I found out 1 problem in line 48 fout << in; and what should i look out for while debugging, the debugger i have GNU debugger in code::blocks IDE.

And one more thing i have noticed that the original file contained 800+characters but the encoded and decoded files contain 500+ characters
EDIT:
* if i give 1000 in place of 80 full file is being read
* would it be better if i used string object
Last edited on
i even used the ios::binary tag to open the files but its still the same problem:(
and many more times the decoding becomes an infinite loop
Topic archived. No new replies allowed.