#include <iostream>
#include <string>
#include <stdlib.h>
#include <cstring>
#include <fstream>
using namespace std;
void encrypt(const string &key, ifstream &from, ofstream &to) {
int key_size = key.size();
char *key_cstr;
key_cstr = (char*) malloc(key_size*sizeof(char));
strcpy(key_cstr, key.c_str());
char ch;
int i=0;
while (from.get(ch)) {
int tex_c =(int) ch;
int key_c =(int) key[i%key_size];
to.put((char) tex_c^key_c);
i++;
}
free(key_cstr);
}
void error(const char* p1, const char* p2="") {
cerr<<p1<<" "<<p2<<endl;
exit(1);
}
int main(int argc, char* argv[]) {
if (argc!=4) error("./ex key fromfile tofile");
string key;
key.assign(argv[1]);
ifstream from(argv[2]);
ofstream to(argv[3]);
if (!from) error("cannot open file", argv[2]);
encrypt(key, from, to);
return 1;
}