Help with simple XOR file encryption program

The program I have will encrypt text files properly, but when I try to encrypt something with a different file extension such as a word document it only encrypts the first few kilobytes.


This previously could decrypt the files too but I made a decryption program to do it faster. It does the same thing to the key(as far as I can tell) but I get completely different output if I put the key in instead of using the key integer (keyi) that the encryption program generated. The decrypter currently works, but I would like to know why this happens.
The different file extensions are there to deter the average person and to separate the two files with the encrypted data and encryption information.

I know there is a better way to change the string key into and int or char but I don't know how to do it. I would greatly appreciate it if you could help with this.

I am still learning a lot about basic function so could you keep the solution as simple as possible please.
thanks
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
#include <iostream>
#include <string>
#include <fstream>
using namespace std;

Encrypt (string key,string fname, string oname){
ifstream fin;
char c;
int keyi;
    for (int i=0;i < key.size();i++)
        keyi*=key[i];
string kname=oname;
oname.append(".cxpt");
kname.append(".ckey");
ofstream fo, keyo(kname.c_str());
fin.open(fname.c_str());
fo.open(oname.c_str());

while(fin >> std::noskipws >> c)
    {
    c=c^keyi;
    fo<<c;
        }
            keyo<<keyi<<endl<<oname<<endl<<fname<<endl<<key;
}


int main(){
string in,key,fname,oname;
//initializes more variables
cout<<"What is the file name?"<<endl;
getline(cin,fname);
cout<<"What will the output file be?"<<endl;
getline(cin,oname);
check:
cout<<"Enter an encryption key."<<endl;
getline(cin,key);
if (key=="1") {cout<<"Invalid key, enter a different one"<<endl; goto check;}
// inputting one doesn't decrypt properly
Encrypt (key,fname,oname);
cout<<"The file has been encrypted/decrypted."<<endl;
string wait;
getline(cin,wait);
//input to pause before closing program
return 0;
}

}

Decryption program - the working code is commented out right now
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
#include <iostream>
#include <string>
#include <fstream>
using namespace std;


Encrypt (string key,string fname, string oname, string xname){
string blank;//for demonstration purposes
ifstream fin,kin;
kin.open(xname.c_str());
kin>>blank>>fname>>oname>>key;
//blank so it doesn't import to keyi
char c;
int keyi;
    for (int i=0;i < key.size();i++)
        keyi*=key[i];
ofstream fo;
fin.open(fname.c_str());
fo.open(oname.c_str());

while(fin >> std::noskipws >> c)
    {
    c=c^keyi;
    fo<<c;
        }
}
/*
Encrypt (string key,string fname, string oname, string xname){
ifstream fin, kin;
ofstream fo;
kin.open(xname.c_str());
int keyi;
kin>>keyi>>fname>>oname;
char c;
fin.open(fname.c_str());
fo.open(oname.c_str());

while(fin >> std::noskipws >> c)
    {
    c=c^keyi;
    fo<<c;
        }
}
*/
int main(){
string in,key,fname,oname,xname;
cout<<"What is the .ckey file named?"<<endl;
getline(cin,xname);
xname.append(".ckey");
//initializes more variables
Encrypt (key,fname,oname,xname);
//input to pause before closing program
cout<<"The file has been decrypted.";
getline(cin,in);
return 0;
}
Topic archived. No new replies allowed.