1 Replies - 1890 Views - Last Post: 12 March 2013 - 05:00 PM Rate Topic: -----

#1 Mindee  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 103
  • Joined: 08-February 11

Need help about understanding one thing.

Posted 12 March 2013 - 04:46 PM

Hello.

I'm trying to recode C# XTEA encrypter in JAVA, but I can't understand one thing.
Here is my code:

        public unsafe static bool XteaEncrypt(ref byte[] buffer, ref int length, int index, uint[] key)
        {
            if (key == null)
                return false;

            int msgSize = length - index;

            int pad = msgSize % 8;
            if (pad > 0)
            {
                msgSize += (8 - pad);
                length = index + msgSize;
            }

            fixed (byte* bufferPtr = buffer)
            {
                uint* words = (uint*)(bufferPtr + index);

                for (int pos = 0; pos < msgSize / 4; pos += 2)
                {
                    uint x_sum = 0, x_delta = 0x9e3779b9, x_count = 32;

                    while (x_count-- > 0)
                    {
                        words[pos] += (words[pos+1] << 4 ^ words[pos+1] >> 5) + words[pos+1] ^ x_sum
                            + key[x_sum & 3];
                        x_sum += x_delta;
                        words[pos+1] += (words[pos] << 4 ^ words[pos] >> 5) + words[pos] ^ x_sum
                            + key[x_sum >> 11 & 3];
                    }
                }
            }

            return true;
        }


What does it mean:
uint* words = (uint*)(bufferPtr + index);


bufferPtr is supposed to be a byte array and how you can add an integer like that?

Is This A Good Question/Topic? 0
  • +

Replies To: Need help about understanding one thing.

#2 Momerath  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1010
  • View blog
  • Posts: 2,444
  • Joined: 04-October 09

Re: Need help about understanding one thing.

Posted 12 March 2013 - 05:00 PM

Pointers allow you to add integers to them, it's how you use them to index arrays. Just one of those pointer things. They are smart, too, so if you have an integer pointer, adding 1 actually adds the size of an int to the pointer.
Was This Post Helpful? 2
  • +
  • -

Page 1 of 1