[Question] DWORD XOR in C#

  • (2 Pages)
  • +
  • 1
  • 2

24 Replies - 3171 Views - Last Post: 07 August 2012 - 05:03 PM Rate Topic: -----

#1 lyzerk  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 07-August 12

[Question] DWORD XOR in C#

Posted 07 August 2012 - 01:30 AM

Hello, Im trying convert one project c++ to c#. And i have question about XOR's.

(DWORD*)(m_pPacket + woffset)
                                = *(DWORD*)(m_pPacket + roffset) ^ *(DWORD*)(m_pXOR_ENCODE_BYTES + xoffset);


m_pXOR_ENCODE_BYTES,m_pPacket is char array, But i dont know how to convert it to c#

anyone can help me about that ?


Sorry my english not good, Thanks.

Is This A Good Question/Topic? 0
  • +

Replies To: [Question] DWORD XOR in C#

#2 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 3171
  • View blog
  • Posts: 9,610
  • Joined: 05-May 12

Re: [Question] DWORD XOR in C#

Posted 07 August 2012 - 02:19 AM

XOR's are exactly the same in C# as C++.

I think the problem you are running into is the use of pointers. By default C# doesn't allow the use of pointers unless you use the /unsafe compile option.
Was This Post Helpful? 0
  • +
  • -

#3 lyzerk  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 07-August 12

Re: [Question] DWORD XOR in C#

Posted 07 August 2012 - 02:48 AM

Can you give me some example on c# (i mean for this code) ?
i know something about unsafe, but what about dword ? should i read int32 from byte array and after XOR it ?
Was This Post Helpful? 0
  • +
  • -

#4 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 3171
  • View blog
  • Posts: 9,610
  • Joined: 05-May 12

Re: [Question] DWORD XOR in C#

Posted 07 August 2012 - 03:12 AM

DWORD's are just UInt32.

Here's your example. I don't know why you need one since it is just like C++.
UInt32 a = 0x81881911;
UInt32 b = 0x11918818;
UInt32 c = a ^ b;


Was This Post Helpful? 1
  • +
  • -

#5 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 5957
  • View blog
  • Posts: 23,226
  • Joined: 23-August 08

Re: [Question] DWORD XOR in C#

Posted 07 August 2012 - 03:22 AM

Guess we'll move this to C#
Was This Post Helpful? 0
  • +
  • -

#6 lyzerk  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 07-August 12

Re: [Question] DWORD XOR in C#

Posted 07 August 2012 - 04:14 AM

View PostSkydiver, on 07 August 2012 - 03:12 AM, said:

DWORD's are just UInt32.

Here's your example. I don't know why you need one since it is just like C++.
UInt32 a = 0x81881911;
UInt32 b = 0x11918818;
UInt32 c = a ^ b;



No i mean that
//m_pXOR_ENCODE_BYTES  byte array
//m_pPacket byte array
m_pPacket + woffset = m_pPacket + roffset ^ m_pXOR_ENCODE_BYTES + xoffset;


it will work like that ? that plus will work in c# ?
Was This Post Helpful? 0
  • +
  • -

#7 Curtis Rutland  Icon User is online

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4312
  • View blog
  • Posts: 7,467
  • Joined: 08-June 10

Re: [Question] DWORD XOR in C#

Posted 07 August 2012 - 07:15 AM

Ok, I don't know much about C++ code, but I know that's hideous C# code. You're doing addition and assignment at the same time, and disregarding the result.

This part:

m_pPacket + woffset

is on the left of the equals sign. You're not assigning the result to anything. So, I don't see what the point of that is. This has the same result:

woffset = m_pPacket + roffset ^ m_pXOR_ENCODE_BYTES + xoffset;


Without the pointless and confusing addition.
Was This Post Helpful? 0
  • +
  • -

#8 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 3171
  • View blog
  • Posts: 9,610
  • Joined: 05-May 12

Re: [Question] DWORD XOR in C#

Posted 07 August 2012 - 11:19 AM

View Postlyzerk, on 07 August 2012 - 04:14 AM, said:

View PostSkydiver, on 07 August 2012 - 03:12 AM, said:

DWORD's are just UInt32.

Here's your example. I don't know why you need one since it is just like C++.
UInt32 a = 0x81881911;
UInt32 b = 0x11918818;
UInt32 c = a ^ b;



No i mean that
//m_pXOR_ENCODE_BYTES  byte array
//m_pPacket byte array
m_pPacket + woffset = m_pPacket + roffset ^ m_pXOR_ENCODE_BYTES + xoffset;


it will work like that ? that plus will work in c# ?


So it's not really help about XOR's that you asking about. You are asking about how to translate into C# the following C++ expressions:
// Expression_1: This maps to 'c'
(DWORD*)(m_pPacket + woffset)

// Expression_2: This maps to 'a'
*(DWORD*)(m_pPacket + roffset)

// Expression_3: This maps to 'b'
*(DWORD*)(m_pXOR_ENCODE_BYTES + xoffset);



I suspect that you are missing a leading '*' on Expression_1, because as there is no place to store the result of Expression_2 ^ Expression_3.

I just wanted to make sure that it's the pointer dereferencing that you need help with, and not XOR's.

If it pointer dereferencing, it would help to know what the types are of m_pPacket, woffset, roffset, m_pXOR_ENCODED_BYTES and xoffset.

And the other useful thing to find out is you are willing to compile with the /unsafe flag.

This post has been edited by Skydiver: 07 August 2012 - 11:20 AM

Was This Post Helpful? 0
  • +
  • -

#9 Curtis Rutland  Icon User is online

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4312
  • View blog
  • Posts: 7,467
  • Joined: 08-June 10

Re: [Question] DWORD XOR in C#

Posted 07 August 2012 - 11:37 AM

So, if you wouldn't mind educating me a bit in C++, is the original intent to store the result at some address of m_pPacket's address plus woffset's value?
Was This Post Helpful? 0
  • +
  • -

#10 Momerath  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 996
  • View blog
  • Posts: 2,392
  • Joined: 04-October 09

Re: [Question] DWORD XOR in C#

Posted 07 August 2012 - 11:45 AM

Using base+offset is common in C/C++ and translates to base[offset] in C/C++/C# (you can use either in C/C++ but have to be careful of what type of pointer base is as that's how it determines how much it should 'skip' (if I remember my C/C++ correctly, it's been a while :))
Was This Post Helpful? 0
  • +
  • -

#11 Curtis Rutland  Icon User is online

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4312
  • View blog
  • Posts: 7,467
  • Joined: 08-June 10

Re: [Question] DWORD XOR in C#

Posted 07 August 2012 - 11:55 AM

So it's pretty much a mechanism for array traversal. I'd say that if they're interested in re-writing this in C#, they should learn the "standard" C# mechanisms and paradigms, rather than compiling as unsafe.
Was This Post Helpful? 0
  • +
  • -

#12 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 3171
  • View blog
  • Posts: 9,610
  • Joined: 05-May 12

Re: [Question] DWORD XOR in C#

Posted 07 August 2012 - 12:02 PM

View PostMomerath, on 07 August 2012 - 11:45 AM, said:

Using base+offset is common in C/C++ and translates to base[offset] in C/C++/C# (you can use either in C/C++ but have to be careful of what type of pointer base is as that's how it determines how much it should 'skip' (if I remember my C/C++ correctly, it's been a while :))


You are correct. That is why I was asking what the type of m_pPacket and m_pXOR_ENCODE_BYTES was.
Was This Post Helpful? 0
  • +
  • -

#13 lyzerk  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 07-August 12

Re: [Question] DWORD XOR in C#

Posted 07 August 2012 - 12:25 PM

Thanks for answers, i solved my problem with like that in C#;

                                *((uint*)(mPacket + woffset)) 
                                    = *((uint*)mPacket + roffset) ^ *((uint*)xor + xoffset);



But now i have problem with another XOR. i want explain my project. my project is encoding/decoding with c++ but i want to convert it to c#, but i can't get correct checkSum.

C++ :
	while(roffset < nEndValue)
	{
		*(DWORD*)(m_pPacket+woffset)
			= *(DWORD*)(m_pPacket+roffset) ^ *(DWORD*)(m_pXOR_ENCODE_BYTES+xoffset);

		checkSum_4B ^= *(DWORD*)(m_pPacket+woffset);

		woffset += 4;
		roffset += 4;
		xoffset = (xoffset + 4) % SIZE_XOR_ENCODE_BYTES;
	}
	checkSum ^= ( *((char*)&checkSum_4B)
				^ *((char*)&checkSum_4B+1)
				^ *((char*)&checkSum_4B+2)
				^ *((char*)&checkSum_4B+3) );



and i covert it like that;

                            while (roffset < nEndValue)
                            {
                                *((uint*)(mPacket + woffset)) 
                                    = *((uint*)mPacket + roffset) ^ *((uint*)xor + xoffset);
                                checkSum_4B ^= *((uint*)(mPacket + woffset));

                                woffset += 4;
                                roffset += 4;
                                xoffset = (xoffset + 4) % 128;
                            }
                            checkSum ^= (*((byte*)&checkSum_4B)
                                ^ *((byte*)&checkSum_4B + 1)
                                ^ *((byte*)&checkSum_4B + 2)
                                ^ *((byte*)&checkSum_4B + 3));



but checkSum not correct. Anyone can help me ?
Thanks
Was This Post Helpful? 0
  • +
  • -

#14 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 3171
  • View blog
  • Posts: 9,610
  • Joined: 05-May 12

Re: [Question] DWORD XOR in C#

Posted 07 August 2012 - 12:31 PM

View PostCurtis Rutland, on 07 August 2012 - 11:55 AM, said:

So it's pretty much a mechanism for array traversal. I'd say that if they're interested in re-writing this in C#, they should learn the "standard" C# mechanisms and paradigms, rather than compiling as unsafe.


Yes. Assuming that m_pPacket and m_pXOR_ENCODED_BYTES were pointers to DWORD's then his original code would simply translate to something like:

m_pPacket[woffset] = m_pPacket[roffset] ^ m_pXOR_ENCODED_BYTES[xoffset];



Chances are likely though that m_pPacket is a pointer to BYTE's, and things get a bit more complicated, but doable by simply dividing the indexes (_offset) by 4 and pray to the deity of your choice that you don't have any remainders.

The value manipulations are pretty simple. The thing that is haunting me, though, is how will one make the arrays be at a particular memory location without using pointers and unsafe code. For example, you have the following in C#:
byte [] myByteBuffer = new byte[4096];


Let's say that I have an array of DWORDs:
Uint32 [] myDwords;


but I want myDwords to start at myByteBuffer[23] onwards. How would I force myDwords to be at that same memory address? I think that the answer is that I cannot without using unsafe code.
Was This Post Helpful? 0
  • +
  • -

#15 Curtis Rutland  Icon User is online

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4312
  • View blog
  • Posts: 7,467
  • Joined: 08-June 10

Re: [Question] DWORD XOR in C#

Posted 07 August 2012 - 12:35 PM

Quote

The thing that is haunting me, though, is how will one make the arrays be at a particular memory location without using pointers and unsafe code.


My question would have to be "why would you want to?" I mean, I could understand severe resource or performance requirements, but in that case, shouldn't it be using C/C++ anyway? Isn't one of the reasons we use C# to abstract that kind of thing away?
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2