views:

740

answers:

6

So a friend was telling me how a game was hacked and how the technique worked. He then asked whats the best way to prevent that kind of attack. The most straight forward way i knew was to A) the shuffle the bits of important value B) hash the values and compare them every time (an int that holds the score or money is likely to be checked rarely).

Then i tried the implementation in C#, i couldnt overload the = operator. How can i do this?

ex code.

class EncryptVal <T>
{
    T v;
    public T operator = (T v2)
    {
        //shuffle bits
    }
    public T operator ()()
    {
        //return unshuffle bits
    }
}
+2  A: 

You are not allowed to overload the assignment operator in C#. Here's the MSDN documentation on it.

You'll have to create your own function for this behavior.

Josh G
+12  A: 

You're looking for the implicit and explicit operator, rather than saying =. This allows you to define how things will work when cast implicitly (ie, just an assignment) and explicitly (ie, there's a casting operator).

public static implicit operator Type1(Type2 p) {}
public static explicit operator Type1(Type2 p) {}
Adam Robinson
I think he doesn't want to cast. He wants a container that holds an encrypted value.
Stefan Steinegger
This seems like a pretty elegant way of accomplishing the task in C#. I was going to suggest method accessors or even a value property, but this makes usage far simpler.
Noldorin
Casting was the wrong word to use. Conversion would have been more accurate. This method will accomplish what you describe.
Adam Robinson
+1  A: 

I assume that you come from C++ where it is very common to write classes that are used like primitive data types. In C# you do things more explicitly.

I would write it as a property or as two methods, eg:

class EncryptVal <T>
{
    T v;
    public T Value
    {
      get
      {
        //return unshuffle bits
      }
      set
      {
        //shuffle bits
      }
    }
}
Stefan Steinegger
Your close but no dice. I would need to do o.v = val; instead of o = val;
acidzombie24
Yes, it's like with a Nullable<T>. int i = nullableInt.Value
Stefan Steinegger
A: 

Dont use = for setting the value. You cant overload assignment.

What you can do is hide it behind a property.

int _encyptedValue;
Public int myInt
{
    get
    {
        return Decrypt(_encryptedValue);
    }
    set
    {
         _encryptedValue = Encrypt(value);
    }
}

You get to chosse your decryption/encryption

Omar Kooheji
A: 

I would go the for implicit/explicit operator overloading for the implementation part.

Probably the explicit one since your conversion does heavy processing, and that it eventually could fail.

I would just add that shuffling bits seems to be only an obfuscation technic that will surely not last long if you have wishfull hackers interested in your game. You probably need stronger cryptography to protect your data, but more context is needed.

Think Before Coding
+8  A: 

You can encapsulate the value in the class and overload the implicit conversions to and from the class:

public class EncryptVal<T> {

 private T _value;

 private EncryptVal(T value) {
  _value = value;
 }

 public static implicit operator EncryptVal<T>(T value) {
  //shuffle bits
  return new EncryptVal<T>(value);
 }

 public static implicit operator T(EncryptVal<T> value) {
  //unshuffle bits
  return value._value;
 }

}

Usage:

// implicit conversion from int
EncryptVal<int> e = 42;

// implicit conversion to int
int i = e;
Guffa
:D .
acidzombie24