0
votes

i write actual a Game in Unity and i want to secure the GameData with this Code.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;
using System;
using System.Text;
using System.Security.Cryptography;
using System.Linq;

public class Data : MonoBehaviour {

static readonly string PasswordHash = "P@@Sw0rd";
static readonly string SaltKey = "S@LT&KEY";
static readonly string VIKey = "@1B2c3D4e5F6g7H8";

public GameObject dirtBlock;

public void Save(){

    BinaryFormatter bf = new BinaryFormatter();
    FileStream file = File.Open(Application.persistentDataPath + "/playerData.dat",FileMode.Open);

    PlayerData data = new PlayerData();
    data.dirtCount = dirtBlock.GetComponent<BlockHandler>().blockCount;
    data.stoneCount = 0;
    data.ironCount = 0;

    bf.Serialize(file,Encrypt(ToByteArray(data)));
    file.Close();

}

public void Load(){

    if(File.Exists(Application.persistentDataPath + "/playerData.dat")){

        BinaryFormatter bf = new BinaryFormatter();
        FileStream file = File.Open(Application.persistentDataPath + "/playerData.dat",FileMode.Open);
        PlayerData data = FromByteArray<PlayerData>(Decrypt(bf.Deserialize(file).ToString()));
        file.Close();

        dirtBlock.GetComponent<BlockHandler>().blockCount = data.dirtCount;

    } else {
        FileStream file = File.Create(Application.persistentDataPath + "/playerData.dat");
        file.Close();
        this.Save();
        this.Load();
    }

}

public static string Encrypt(byte[] plainTextBytes){

    byte[] keyBytes = new Rfc2898DeriveBytes(PasswordHash, Encoding.ASCII.GetBytes(SaltKey)).GetBytes(256 / 8);
    var symmetricKey = new RijndaelManaged() { Mode = CipherMode.CBC, Padding = PaddingMode.Zeros };
    var encryptor = symmetricKey.CreateEncryptor(keyBytes, Encoding.ASCII.GetBytes(VIKey));

    byte[] cipherTextBytes;

    using (var memoryStream = new MemoryStream()){

        using (var cryptoStream = new CryptoStream(memoryStream, encryptor, CryptoStreamMode.Write)){

            cryptoStream.Write(plainTextBytes, 0, plainTextBytes.Length);
            cryptoStream.FlushFinalBlock();
            cipherTextBytes = memoryStream.ToArray();
            cryptoStream.Close();

        }

        memoryStream.Close();

    }

    return Convert.ToBase64String(cipherTextBytes);

}

public static byte[] Decrypt(string encryptedText){

    byte[] cipherTextBytes = Convert.FromBase64String(encryptedText);
    byte[] keyBytes = new Rfc2898DeriveBytes(PasswordHash, Encoding.ASCII.GetBytes(SaltKey)).GetBytes(256 / 8);
    var symmetricKey = new RijndaelManaged() { Mode = CipherMode.CBC, Padding = PaddingMode.None };

    var decryptor = symmetricKey.CreateDecryptor(keyBytes, Encoding.ASCII.GetBytes(VIKey));
    var memoryStream = new MemoryStream(cipherTextBytes);
    var cryptoStream = new CryptoStream(memoryStream, decryptor, CryptoStreamMode.Read);
    byte[] plainTextBytes = new byte[cipherTextBytes.Length];

    memoryStream.Close();
    cryptoStream.Close();
    return plainTextBytes;

}


public byte[] ToByteArray<T>(T obj){

    if(obj == null){
        return null;
    }
    BinaryFormatter bf = new BinaryFormatter();
    using(MemoryStream ms = new MemoryStream()){
        bf.Serialize(ms, obj);
        return ms.ToArray();
    }
}

public T FromByteArray<T>(byte[] data){
    if(data == null){
        return default(T);
    }

    BinaryFormatter bf = new BinaryFormatter();
    using(MemoryStream ms = new MemoryStream(data)){
        object obj = bf.Deserialize(ms);
        return (T)obj;
    }
}

}

[System.Serializable]
public class PlayerData {

public float dirtCount;
public float stoneCount;
public float ironCount;

}

yesterday it work fine but today and i dont know why, i get this error message

SerializationException: Unexpected binary element: 0 System.Runtime.Serialization.Formatters.Binary.ObjectReader.ReadObject (BinaryElement element, System.IO.BinaryReader reader, System.Int64& objectId, System.Object& value, System.Runtime.Serialization.SerializationInfo& info) (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System.Runtime.Serialization.Formatters.Binary/ObjectReader.cs:254) System.Runtime.Serialization.Formatters.Binary.ObjectReader.ReadNextObject (BinaryElement element, System.IO.BinaryReader reader) (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System.Runtime.Serialization.Formatters.Binary/ObjectReader.cs:130) System.Runtime.Serialization.Formatters.Binary.ObjectReader.ReadObjectGraph (BinaryElement elem, System.IO.BinaryReader reader, Boolean readHeaders, System.Object& result, System.Runtime.Remoting.Messaging.Header[]& headers) (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System.Runtime.Serialization.Formatters.Binary/ObjectReader.cs:104) System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.NoCheckDeserialize (System.IO.Stream serializationStream, System.Runtime.Remoting.Messaging.HeaderHandler handler) (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System.Runtime.Serialization.Formatters.Binary/BinaryFormatter.cs:179) System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Deserialize (System.IO.Stream serializationStream) (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System.Runtime.Serialization.Formatters.Binary/BinaryFormatter.cs:136) Data.FromByteArray[PlayerData] (System.Byte[] data) (at Assets/Scripts/Player/Data.cs:119) Data.Load () (at Assets/Scripts/Player/Data.cs:40)

i searched know 4 hours in the web but i cannot find a solution for this problem. i hope anyone can help me.

1
This that exact Load-code work yesterday?Fredrik Widerberg
yes this code work yesterdayPla558
Did you write the serialize/de-serialize and encryption code or did you get it from some tutorial? If you got it from a tutorial, please link to that as that might help solve the issue.Programmer
i use this for the encryption "social.msdn.microsoft.com/Forums/vstudio/en-US/…" and this for the game object save (44:40): unity3d.com/de/learn/tutorials/topics/scripting/…Pla558

1 Answers

1
votes

After some testing with your code i ended up with these two methods.

I think the main issue with your code was the Decrypt that never did anything with the plainTextBytes-array.

private static string Encrypt(byte[] plainTextBytes)
{
    byte[] keyBytes = new Rfc2898DeriveBytes(PasswordHash, Encoding.ASCII.GetBytes(SaltKey)).GetBytes(256 / 8);
    var symmetricKey = new RijndaelManaged() { Mode = CipherMode.CBC, Padding = PaddingMode.Zeros };
    var encryptor = symmetricKey.CreateEncryptor(keyBytes, Encoding.ASCII.GetBytes(VIKey));

    byte[] cipherTextBytes;

    using (var memoryStream = new MemoryStream())
    {
        using (var cryptoStream = new CryptoStream(memoryStream, encryptor, CryptoStreamMode.Write))
        {
            cryptoStream.Write(plainTextBytes, 0, plainTextBytes.Length);
            cryptoStream.FlushFinalBlock();
            cipherTextBytes = memoryStream.ToArray();
        }
    }

    return Convert.ToBase64String(cipherTextBytes);
}

Decrypt

public static byte[] Decrypt(string base64)
{
    byte[] cipherTextBytes = Convert.FromBase64String(base64);
    byte[] keyBytes = new Rfc2898DeriveBytes(PasswordHash, Encoding.ASCII.GetBytes(SaltKey)).GetBytes(256 / 8);
    var symmetricKey = new RijndaelManaged() { Mode = CipherMode.CBC, Padding = PaddingMode.None };

    var decryptor = symmetricKey.CreateDecryptor(keyBytes, Encoding.ASCII.GetBytes(VIKey));
    using (var memoryStream = new MemoryStream(cipherTextBytes))
    {
        using (var cryptoStream = new CryptoStream(memoryStream, decryptor, CryptoStreamMode.Read))
        {
            using (BinaryReader srDecrypt = new BinaryReader(cryptoStream))
            {
                return srDecrypt.ReadBytes(cipherTextBytes.Length);
            }
        }
    }
}