Thread: File encryption succeeds, but decryption fails

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Nov 2020
    Posts
    6

    File encryption succeeds, but decryption fails

    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
    Last edited by Salem; 11-23-2020 at 10:04 AM. Reason: removed eyebleed font/colour scheme - learn to do this YOURSELF!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Encryption/Decryption/File I/O in C
    By RagingGrim in forum C Programming
    Replies: 2
    Last Post: 11-18-2014, 05:49 AM
  2. Replies: 6
    Last Post: 03-08-2012, 05:37 PM
  3. encryption / decryption
    By ghatoz in forum C++ Programming
    Replies: 2
    Last Post: 11-18-2010, 06:45 AM
  4. Encryption/Decryption Help
    By coolage in forum C++ Programming
    Replies: 9
    Last Post: 04-25-2008, 01:53 AM
  5. Ask about Encryption and Decryption
    By ooosawaddee3 in forum C Programming
    Replies: 3
    Last Post: 07-19-2002, 12:55 AM

Tags for this Thread