Thread: How to apply XOR encryption to a file

  1. #1
    Registered User
    Join Date
    Nov 2020
    Posts
    6

    How to apply XOR encryption to a file

    I'm reading a file and I want to apply XOR encryption to it and print it.
    This is my code
    Code:
    #include <stdio.h>#include <stdlib.h>
    #include <string.h>
    #include <unistd.h>
    #include <sys/stat.h>
    
    #define XOR_KEY0x6F
    
    int main() {
        
        FILE *infp;
        char buf[1024];
        infp = fopen("test.txt", "r");
        while ((fgets(buf, sizeof(buf), infp)) != NULL)
        {
            printf("Contents: %s", buf ^ XOR_KEY);
        }
        
        return 0;
    
    }
    I get this error:
    error: invalid operands to binary ^ (have 'char *' and 'int')
    17 | printf("Contents: %s", buf ^ XOR_KEY);
    | ~~~ ^
    | |
    | char *

    I tried to encrypt an initialized char array and it worked, but why am I not able to do this to char buf[1024] array?

    Thanks

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by nohemon
    I tried to encrypt an initialized char array and it worked
    What is the code that you used for this? Perhaps you did something different, e.g., you used XOR character by character rather than trying to use it with the entire array at once.

    Also, printing the ciphertext as normal text might be problematic since some characters could become unprintable. Some approaches to this would be to print the bytes in a hex representation, or to base64 encode the ciphertext.
    Last edited by laserlight; 11-19-2020 at 05:16 PM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Nov 2020
    Posts
    6
    What is the code that you used for this?
    Something like this:

    Code:
     #include
    Code:
    <stdio.h>
     int main(int argc, char *argv[]) 
     {
        char xorString[] = "This is a test";
        int i = 0;
        for (int i = 0; xorString[i] != '\0'; i++)
        {
            xorString[i] ^= 0xAA;
        }
    
        printf("%s", xorString);
        
        return 0;
     }

    It outputs this: ■┬├┘Λ├┘Λ╦Λ▐╧┘▐ (I'm not sure if you can see it)
    I tried applying it to buf, but I get the error that I described on my first post.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Yeah, that's exactly what I guessed. Why don't you do the same thing in your code in post #1 as you did in your code in post #3?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Nov 2020
    Posts
    6
    Quote Originally Posted by laserlight View Post
    Yeah, that's exactly what I guessed. Why don't you do the same thing in your code in post #1 as you did in your code in post #3?
    I did. I tried doing it on buf array. I tried one-by-one (by iterating to it), and as a whole, but I got the same error in both situations.

    error: invalid operands to binary ^ (have 'char *' and 'int')


  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Let's see your code with the "one-by-one (by iterating to it)" version.

    EDIT:
    Well, I decided to adapt and combine your code in post #3 into your code in post #1:
    Code:
    #include <stdio.h>
    
    #define XOR_KEY 0x6F
    
    int main() {
    
        FILE *infp;
        char buf[1024];
        infp = fopen("test.txt", "r");
        while ((fgets(buf, sizeof(buf), infp)) != NULL)
        {
            for (int i = 0; buf[i] != '\0'; i++)
            {
                buf[i] ^= XOR_KEY;
            }
    
            printf("Contents: %s", buf);
        }
    
        return 0;
    
    }
    You're still going to have potential problems with the output as I mentioned earlier if you don't encode the ciphertext in some way to make it text. You also need to check that fopen did not return a null pointer before you use infp, and then you should fclose when done.
    Last edited by laserlight; 11-19-2020 at 06:43 PM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Nov 2020
    Posts
    6
    You're still going to have potential problems with the output as I mentioned earlier if you don't encode the ciphertext in some way to make it text.


    It compiles but it outpus nothing. Maybe it's what you said. But why output doesn't show now but it was visible when I iterated through the initialized char array?

    If I write the output to a new file? Would it become visible?

    *EDIT: I had to use %s and not %c. Thank you for your useful help

  8. #8
    Registered User Sir Galahad's Avatar
    Join Date
    Nov 2016
    Location
    The Round Table
    Posts
    277
    Quote Originally Posted by nohemon View Post


    It compiles but it outpus nothing. Maybe it's what you said. But why output doesn't show now but it was visible when I iterated through the initialized char array?

    If I write the output to a new file? Would it become visible?

    *EDIT: I had to use %s and not %c. Thank you for your useful help
    Maybe just print it within the XOR loop?

    Code:
    #include <stdio.h>
     
    #define XOR_KEY 0x6F
     
    int main() {
     
        FILE *infp;
        char buf[1024];
        infp = fopen("test.txt", "r");
        while ((fgets(buf, sizeof(buf), infp)) != NULL)
        {
            for (int i = 0; buf[i] != '\0'; i++)
            {
                buf[i] ^= XOR_KEY;
                printf("%x", (int)buf[i]);
            }
     
            puts("");
        }
     
        return 0; 
    }
    I understand this is probably just an exercise, but just so you know, this doesn't even come close to being real encryption.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. XOR File Encryption in C
    By andrey.117 in forum C Programming
    Replies: 7
    Last Post: 03-20-2013, 07:49 AM
  2. Replies: 6
    Last Post: 03-08-2012, 05:37 PM
  3. file encryption?
    By elfjuice in forum C++ Programming
    Replies: 5
    Last Post: 07-02-2002, 07:39 PM
  4. File Encryption...
    By |<4D4\/3r in forum C++ Programming
    Replies: 3
    Last Post: 04-02-2002, 03:30 AM
  5. File Encryption in C
    By a_learner in forum C Programming
    Replies: 2
    Last Post: 10-18-2001, 03:19 PM

Tags for this Thread