I've been attempting to do a basic XOR encoder but I got this problem:
Everytime the result of the XOR operation is 10, the app writes 0xA 0xD, and I can't avoid this.
Code:

#include <iostream>
#include <fstream>
#include <stdio.h>
using namespace std;
char* readfile(char * filename,int* size){
  int i = 0;
  ifstream file (filename, ios::in|ios::binary|ios::ate);
  if (file.is_open())
  {
    *size = (int)file.tellg();
    static char * memblock = new char [*size];
    file.seekg (0, ios::beg);
    file.read (memblock, *size);
	file.close();
	return memblock;
  }
  else
  {
	cout << "Error while opening the file";
	return 0;
  }
}
void echo_file(char* memblock,int file_length)
{
	for(int i = 0;i < file_length; i++) putchar(*(memblock+i));
}
void writefile(char * memblock, int size, char * filename){
	ofstream file (filename);
	int i = 0;
	for(i=0; i < size; i++){
		if(file.is_open()){file << memblock[i];}
		else{cout << "An error happened";}
	}
	file.close();
}
int xor(int a, int b)// I know about the ^ operator, is only for learning purposes
{
	_asm{
	mov eax,a
	xor eax,b
	}
}
char* encode_file(char * memblock, char * key, int size){
	int temp = 0;
	static char * encoded_char = new char[size];
	int i = 0;
	for(i=0; i < size; i++){
		*(encoded_char+i) = xor(*(memblock+i),*((key+i%strlen(key))));
	}
	return encoded_char;
}

int main (int argc, char* argv[]) {
  int tmpchar = 0;
  int k = 2;
  int i = 0;
  if(argc < 4){
	  cout << "Use: [input file][key][output file]";
	  return 0;
  }
  else{
		int size = 0;
		char * thefile = readfile(argv[1], &size);
		if(thefile == 0) exit(EXIT_SUCCESS);
		char* enc_file = encode_file(thefile,argv[2],size);
		writefile(enc_file,size,argv[3]);
		printf("File size: %d \n",size);
	}
  return 0;
}

P.D.Sorry for my poor english

Salem commented: At least you figured out code tags on your first post! +17

Open the output file as binary too.

> ifstream file (filename, ios::in|ios::binary|ios::ate);
The output stream needs to be binary as well.

Thanks, it's working fine now :icon_cheesygrin:

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.