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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T09:34:07+00:00 2026-05-23T09:34:07+00:00

I am playing around with xor decoding via a small C file, and am

  • 0

I am playing around with xor decoding via a small C file, and am running into issues with endianness …I am a bit stuck on how to work around them. This is really the first time I’ve played this deeply with bitwise operations in C.

If I use a one-byte xor key and pick up several xor-encoded values into a uint8_t pointer, my basic code works fine. Walk each byte, xor it against the key, and store the result in a decoded byte array/buffer and then print it back to the console.

However, if I try a two-byte xor key, then endianness starts to get in the way. I currently stick the key into a uint32_t, because I don’t plan on dealing with xor keys greater than 32bits. On a little-endian system, a xor key of 0xc39f gets stored as 0x9fc3. The bytes to be decoded are big-endian if I play them back one byte at a time, but they too, get flipped to little-endian if I try to play them back two-bytes at a time (same size as the xor key).

I am tempted to #include <byteswap.h> and then call bswap_32(). But while this will work on little endian, it might have the opposite effect on big-endian. I assume then I’d need ugly #ifdef’s to only use bswap_32() for little-endian archs. I figure, there has got to be a more portable way for this to work.

Random sample string:

g   e   n   e   r   a   t   e
67  65  6e  65  72  61  74  65

Xor 0xc39f

a4  fa  ad  fa  b1  fe  b7  fa

If I play back the xor-encoded buffer with two-byte (uint16_t) pointers, I get this (via a basic printf):

0xfaa4 0xfaad 0xfeb1 0xfab7

And with four-byte pointers (uint32_t):

0xfaadfaa4 0xfab7feb1

I would expect for the above, to get instead for two-byte pointers:

0xa4fa 0xadfa 0xb1fe 0xb7fa

And four-byte pointers:

0xa4faadfa 0xb1feb7fa

Thoughts?

Edit: Any takers? Current answers aren’t adequate to my needs.

  • 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-23T09:34:07+00:00Added an answer on May 23, 2026 at 9:34 am

    You’re overthinking this—just treat your xor key as an endianless binary blob, and convert it to a native uint32_t for performance:

    void xor_encrypt_slow(uint8_t *data, size_t len, uint8_t key[4])
    {
        // key is a 4-byte xor key
        size_t i;
        for(i = 0; i < len; i++)
            data[i] ^= key[i % 4];
    }
    
    void xor_encrypt_fast(uint8_t *data, size_t len, uint8_t key[4])
    {
        // Convert key to a 32-bit value
        uint32_t key32 = *(uint32_t *)key;
    
        // This assumes that data is aligned on a 4-byte boundary; if not, adjust
        // accordingly
        size_t i;
        for(i = 0; i + 3 < len; i += 4)
            ((uint32_t *)data)[i] ^= key32;
        // Handle the remainder, if len is not a multiple of 4
        for( ; i < len; i++)
            data[i] ^= key[i % 4];
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Playing around a bit more with jqTouch and am running into a problem with
I'm playing around with LinqToSQL using an existing multi-lingual database, but I'm running into
After playing around with haskell a bit I stumbled over this function: Prelude Data.Maclaurin>
While playing around with playframework yabe-siena-gae, I noticed a datastore file created for the
I am playing around with a small MFC-wizard-generated application, in Visual C++ 2010, and
playing around with a property file i figured that there seems to be a
Playing around with jQuery a bit (sorry, complete noob) I was wondering why this
After playing around with URL decoding myself, I managed to come up with some
Been playing around with node.js and I am developing a file uploading web app
Playing around with Timers, and trying to make them behave right when app is

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.