Take the 2-minute tour ×

Although I've used C++ a lot, I'm struggling with the C differences (mainly in strings).

Could you please show me a simple single function that encrypts a message with a key using XOR comparison.


EDIT: Both the key and the message are char*

share|improve this question
What have you tried? –  Tim Nov 9 '11 at 22:58
I would add what I've tryed but I'm on my mobile –  Will03uk Nov 9 '11 at 23:14
Note that an XOR function shouldn't really be considered encrypting, it's way too easy to crack. –  Mark Ransom Nov 9 '11 at 23:51
@Mark, sorry for the lack of clarity but this is my introduction, so to speak, before I try other algorithms –  Will03uk Nov 10 '11 at 0:33
add comment

3 Answers

up vote 3 down vote accepted

OK, I hacked around for a minute and came up with this (only vaguely tested):

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

char * xorencrypt(char * message, char * key) {
    size_t messagelen = strlen(message);
    size_t keylen = strlen(key);

    char * encrypted = malloc(messagelen+1);

    int i;
    for(i = 0; i < messagelen; i++) {
        encrypted[i] = message[i] ^ key[i % keylen];
    encrypted[messagelen] = '\0';

    return encrypted;

    char * message = "test message";
    char * key = "abc";

    char * encrypted = xorencrypt(message, key);
    printf("%s\n", encrypted);

    return 0;

Note that the function xorencrypt allocates and returns a new string, so it's the caller's responsibility to free it when done.

share|improve this answer
Good solution for the key length; I didn't think about %. –  Will03uk Nov 9 '11 at 23:13
The returned string is not NULL terminated here. –  Mike Steinert Nov 9 '11 at 23:27
Good catch, @MikeSteinert - fixed. –  Tim Nov 9 '11 at 23:39
This really won't work reliably. The problem is that the encrypted string may contain any character at any point in the string, including '\0'. Thus you have no way of knowing where the end of the encrypted string is. –  GregS Nov 10 '11 at 22:24
@GregS: true, and I considered that, but the OP hasn't specified that he needs that information (and it's trivially obtainable from strlen(message) anyway). –  Tim Nov 11 '11 at 13:08
show 2 more comments

C is very close to Assembler, so this example is short:

while (*string)
    *string++ ^= key;

assuming char *string; and char key.

share|improve this answer
It's probably worth mentioning that this is exactly how you would write it in C++ also. –  Mike Steinert Nov 9 '11 at 23:12
@Mike I know, but manipulating cstrings are somewhat new to me; do you need to initialize strings to '\0' after malloc? –  Will03uk Nov 9 '11 at 23:17
If you're not sure of the length, yes; you'll note my solution above takes care of that issue instead by only allocating a string of the same length as the original message (so you're sure the entire string will be filled with data, and doesn't need \0-filling). –  Tim Nov 9 '11 at 23:20
@Will03uk You don't have to initialize the string data if you plan to write it, however should make sure it is NULL-terminated. –  Mike Steinert Nov 9 '11 at 23:34
@Will03uk You need to initialize memory before you can read it, but you can write to it without a problem. And you never have to fill a block of memory with zeros when working with strings, since string functions only read up to the first \0. However, you will see that zero-filling be done occasionally because people (ab)use strncat for security reasons. Really the only time you need to do that is when working with fixed-width fields, which are used in systems programming. –  Brian Gordon Nov 10 '11 at 0:13
show 1 more comment

For what it's worth, combine the answers from @ott-- & @Tim to form Xortron.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

char *xor(char *string, const char *key)
    char *s = string;
    size_t length = strlen(key), i = 0;
    while (*s) {
            *s++ ^= key[i++ % length];
    return string;

int main(int argc, char **argv)
    const char *key = "abc";
    if (argc < 2) {
            fprintf(stderr, "%s: no input\n", argv[0]);
            return EXIT_FAILURE;
    printf("%s\n", xor(xor(argv[1], key), key));
    return EXIT_SUCCESS;
share|improve this answer
add comment

Your Answer


