Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6783211
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T16:50:13+00:00 2026-05-26T16:50:13+00:00

Although I’ve used C++ a lot, I’m struggling with the C differences (mainly in

  • 0

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.

Thank-you

EDIT:
Both the key and the message are char*

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T16:50:13+00:00Added an answer on May 26, 2026 at 4:50 pm

    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;
    }
    
    int main(int argc, char * argv[]) {
        char * message = "test message";
        char * key = "abc";
    
        char * encrypted = xorencrypt(message, key);
        printf("%s\n", encrypted);
        free(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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Although I do understand the serious implications of playing with this function (or at
Although I grasp the concept of Bitwise Operators, I can't say that I have
Although I have some experience in Android development, I don't have a lot of
Although probarly quite simple, i cannot seem to find the following. The form takes
Although I've used Swing before I've never used a GUI designer and I'm having
Although it's documented that this is supposedly possible, the API currently doesn't allow liking
Although I know that there are more idomatic ways of doing this, why doesn't
Although this is pretty basic, I can't find a similar question, so please link
Although it seems pretty simple, I'm not sure of the most efficient way of
Although there are plenty of unit test frameworks that support C, I'm a little

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.