Secure way to store and read Json data?

Hello,

So this might be a vague question, if so, let me know and I’ll try to elaborate.

(preface, I am using C#)

I am currently using a Json file so I can easily and quickly create a varied amount of items to be collected in game.

However, if I release my game, I won’t want the data to be stored in an easily read and edited json file - so how would I go about encrypting the Json for a release build? Or is there an easier way to read from a file, that is encrypted, but easily editable while developing the project?

I have attached some pseudo code below so people can take a look at how I am currently operating and if there is an easy solution.

Thanks in advance!

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO;

public class ItemPickupClass : MonoBehaviour
{

    public string itemDataPath;
    public Spring SpringData;
    public Summer SummerData;
    public Autumn AutumnData;
    public Winter WinterData;

    [System.Serializable]
    public class AllSeasons
    {
        public Spring Spring;
        public Summer Summer;
        public Autumn Autumn;
        public Winter Winter;
    }

    public class Season
    {
        public List<Item> Day;
        public List<Item> Night;
    }

    [System.Serializable]
    public class Spring : Season
    {
    }

    [System.Serializable]
    public class Summer : Season
    {
    }

    [System.Serializable]
    public class Autumn : Season
    {
    }

    [System.Serializable]
    public class Winter : Season
    {
    }

    [System.Serializable]
    public class Item
    {
        public int ItemID;
        public string name;
        public string description;
        public int value;
        public int weight;
    }

    private void Start()
    {
        itemDataPath = Path.Combine(Application.streamingAssetsPath, "ItemDatabase.json");
        string JsonText = File.ReadAllText(itemDataPath);
        AllSeasons getSeasonData = new AllSeasons();
        getSeasonData = JsonUtility.FromJson<AllSeasons>(JsonText);
        SpringData = getSeasonData.Spring;
        SummerData = getSeasonData.Summer;
        AutumnData = getSeasonData.Autumn;
        WinterData = getSeasonData.Winter;
    }

}

and here is an example (stripped down to be shorter) of the Json I am reading from:

{
    "Spring":
        {
            "Day": [
                {
                    "ItemID": 1,
                    "name": "Spring Day Book",
                    "description": "A small book",
                    "value": 2,
                    "weight": 50,
                }
            ],
            "Night": [
                {
                    "ItemID": 1,
                    "name": "Spring Night Book",
                    "description": "A small book",
                    "value": 2,
                    "weight": 50,
                }
            ]
        },
    "Summer":
        {
            "Day": [
                {
                    "ItemID": 1,
                    "name": "Summer Day Book",
                    "description": "A small book",
                    "value": 2,
                    "weight": 50,
                }
            ],
            "Night": [
                {
                    "ItemID": 1,
                    "name": "Summer Night Book",
                    "description": "A small book",
                    "value": 2,
                    "weight": 50,
                }
            ]
        },
    "Autumn":
    {
            "Day": [
                {
                    "ItemID": 1,
                    "name": "Autumn Day Book",
                    "description": "A small book",
                    "value": 2,
                    "weight": 50,
                }
            ],
            "Night": [
                {
                    "ItemID": 1,
                    "name": "Autumn Night Book",
                    "description": "A small book",
                    "value": 2,
                    "weight": 50,
                }
            ]
        },
    "Winter":
        {
            "Day": [
                {
                    "ItemID": 1,
                    "name": "Winter Day Book",
                    "description": "A small book",
                    "value": 2,
                    "weight": 50,
                }
            ],
            "Night": [
                {
                    "ItemID": 1,
                    "name": "Winter Night Book",
                    "description": "A small book",
                    "value": 2,
                    "weight": 50,
                }
            ]
        },
}

I believe you can just put it in Resources and load it from there, or if nothing else, put it in Assets and create a TextAsset variable that you can drag and drop the file into and it should be fine.

Since you’re loading from streaming assets, I’m assuming you’re just storing the data and not planning to modify it in a live build?

You might also look into scriptable objects as another possible option.

Hello,

No I will not be modifying it at runtime. I will try out those suggestions, thanks!

Nothing you encrypt locally is ever really secure, but if you’d like to just implement some very basic encryption/obfuscation that would deter most casual people from making changes, you could just use a simple xor obfuscation technique against the entire json file. When you read it in you would decrypt the entire file. While developing you could just have this simple encryption disabled.

1 Like