Thread: File encryption succeeds, but decryption fails

  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!

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    Use fgetc() and fputc() instead. Or better yet, at a lower level, use fread() and fwrite() with the buffer.
    Devoted my life to programming...

  3. #3
    Registered User Sir Galahad's Avatar
    Join Date
    Nov 2016
    Location
    The Round Table
    Posts
    277
    Exactly, fgets() is for text, not binary data.

  4. #4
    Registered User
    Join Date
    Nov 2020
    Posts
    6
    You were right. Thank you both. GReaper να'σαι καλα αδερφε.

  5. #5
    Registered User
    Join Date
    Apr 2019
    Posts
    114
    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?!?

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    You need to use fread() and fwrite() if you're working with binary data.

    And buf[i] when looping through the buffer.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

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