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 7630359
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T06:04:56+00:00 2026-05-31T06:04:56+00:00

I’ve been trying to make a basic XOR header file for use in some

  • 0

I’ve been trying to make a basic XOR header file for use in some future programs. So far I’ve gotten almost everything to work, but I can’t seem to use the same function twice. If I call the function to encrypt the string it works, but then if I call it again it crashes. I don’t know if I’m doing something wrong memory-wise or am missing something obvious. Hope someone can point out a flaw in this because I can’t seem to find anything wrong.

Edit: If posting this much is too much, feel free to trim the code. I already took out quite a bit, so I’m not just pasting my project and hoping someone fixes it.

// Main.c
#define MAX_LENGTH 255
#define KEY_SIZE 8
int main(int argc, char *argv[]) {
    //Get String to XOR
    char *input = malloc (MAX_LENGTH);
    printf("Enter a string to encrypt: ");
    fgets(input, MAX_LENGTH, stdin);

    if(input[strlen (input) - 1] == '\n') {
        input[strlen (input) - 1] = '\0';
    }

    //Create a random key
    char *pass = _create_key(KEY_SIZE);
    int len = strlen (input);
    printf("Length of key is %d\n", KEY_SIZE);
    printf("Entered String: %s - Password: %s\n", input, pass);

    //Encrypt works fine
    char *encrypted = malloc (sizeof (input));
    _xor_str_s(input, pass, len, encrypted);
    printf("Encrypted String: %s\n", encrypted);

    char *decrypted = malloc (sizeof (input));
    //Crashes here
    _xor_str_s(encrypted, pass, len, decrypted);
    printf("Decrypted String: %s\n", decrypted);
    return 0;
}

//Header File Function
void _xor_str_s(char *str, char *pass, int len, char *out) {
    int i = 0;
    for(i = 0; i < len; i++) {
        *(out + i) = str[i] ^ pass[i % strlen (pass)];
    }
    *(out + i) = 0;
}

char * _create_key(int len) {
    len = !len ? 16 : len;
    char *ret = (char *)malloc (len);
    unsigned int _GLOBAL_SEED_ = (unsigned int)time(NULL);
    srand (_GLOBAL_SEED_);
    int i = 0;
    for(i = 0; i < len; i++) {
        ret[i] = (char)(rand() + 1); //+1 avoids NULL
    }
    ret[i] = '\0';
    return ret;
}
  • 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-31T06:04:57+00:00Added an answer on May 31, 2026 at 6:04 am
    char *encrypted = malloc (sizeof (input));
    

    is probably the problem as this will always be sizeof(char *). I think you want

    char *encrypted = malloc (strlen (input) + 1);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have just tried to save a simple *.rtf file with some websites and
I am trying to understand how to use SyndicationItem to display feed which is
I want use html5's new tag to play a wav file (currently only supported
I am trying to render a haml file in a javascript response like so:
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
I have a jquery bug and I've been looking for hours now, I can't
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
Basically, what I'm trying to create is a page of div tags, each has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function

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.