Quote Originally Posted by nohemon View Post
My code supposedly encrypts a file (XOR method) and then (when I rerun it with the appropriate argument), decrypts it.

I can successfully encrypt and decrypt .txt files, but when it comes to .exe files it fails.

What I mean by "fails":
I double-click a test.exe file, and it runs fine. Then I encrypt it with the below code. I rerun it and it doesn't execute with the message "This app can't run on yout PC. To find a version for your PC, check with the software publisher.".
This, in my mind means that the exe is indeed encrypted. Then I decrypt it (with the same key) and the output exe file still gives me the same error although it's supposedly decrypted and converted back to its initial state.

My code:

Code:
#define XOR_KEY 0XAA
void xorFile();

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

  if (argc != 4) {
    perror("Invalid arguments");
    exit(1);
  }

  if (strcmp(argv[1], "crypt") == 0) {
    xorFile(argv[2], argv[3]);
  }

  else if ((strcmp(argv[1], "decrypt") == 0)) {
    xorFile(argv[2], argv[3]);

  }

  else {
    fprintf(stderr, "Select a valid operation (crypt or decrypt).");
    exit(1);
  }

  return 0;
}

void xorFile(char *infile, char *outfile)
{
  FILE *infp;
  FILE *outfp;
  char buf[2048];
  infp = fopen(infile, "rb");
  outfp = fopen(outfile, "wb");
  if (infp == NULL) {
    printf("Could not open file for READ");
    exit(1);
  }

  if (outfp == NULL) {
    printf("Could not open file for WRITE");
    exit(1);
  }

  while ((fgets(buf, sizeof(buf), infp)) != NULL) {
    for (int i = 0; buf != '\0'; i++) {
      buf ^= XOR_KEY;
      fprintf(outfp, "%c", buf);
    }

  }

  fclose(infp);
  fclose(outfp);

}
Why is this happening? It's actually the same xor operation with the same key.

Thanks

After adding the following:
Code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
I get the following error:
Code:
encrypt.c: In function 'xorFile':
encrypt.c:52:11: error: assignment to expression with array type
   52 |       buf ^= XOR_KEY;
How were you able to compile this?!?