About Encryption & Decryption

Discussion and help for Easy Save 3
Johnny Star
Posts: 25
Joined: Sat Mar 21, 2020 2:17 pm

About Encryption & Decryption

Post by Johnny Star »

Hello, hope you are doing well!

I have 2 questions.

Question 1
So I have set up saving and loading normally and everything is working good.
So I decided to enable some protection now.
I went into Easy Save > Settings (Runtime Settings) > Under Encryption selected AES & set a password 'Johnny$uper$tar'

When I run the game I encounter this error:

Code: Select all

FormatException: Cannot load from file because the data in it is not JSON data, or the data is encrypted.
If the save data is encrypted, please ensure that encryption is enabled when you load, and that you are using the same password used to encrypt the data.
ES3Internal.ES3JSONReader..ctor (System.IO.Stream stream, ES3Settings settings, System.Boolean readHeaderAndFooter) (at Assets/Settings/Asset Store Packages/Easy Save 3/Scripts/Readers/ES3JSONReader.cs:39)
ES3Reader.Create (ES3Settings settings) (at Assets/Settings/Asset Store Packages/Easy Save 3/Scripts/Readers/ES3Reader.cs:348)
ES3.Load[T] (System.String key, T defaultValue, ES3Settings settings) (at Assets/Settings/Asset Store Packages/Easy Save 3/Scripts/ES3.cs:415)
ES3AutoSaveMgr.Load () (at Assets/Settings/Asset Store Packages/Easy Save 3/Scripts/Auto Save/ES3AutoSaveMgr.cs:81)
ES3AutoSaveMgr.Awake () (at Assets/Settings/Asset Store Packages/Easy Save 3/Scripts/Auto Save/ES3AutoSaveMgr.cs:100)
What do I do?

Question 2
Also, I opened the Persistent Data Path folder and opened the file and it is a long list of
...
46d5 e6d0 80b6 e284 5260 fd4a e977 6dfc
350a fc0e 404a 3314 91da 4928 1897 7ddf
898c 4422 b7d9 da4f 1cee 4ca6 71c8 5797
c75d cff5 3d9b 5b2e db91 f9f0 5f3e ef08
4203 950e 74ff b1d0 0401 3574 ba04 efdd
19cc cdbd 0060 b7a5 ca71 669b 2256 20e9
9ec9 3b76 ff24 2328 07bd 7b7b 735a 53e1
...
I copied everything from the file and found a random online decryption site and pasted in the content and used my key to decrypt it but nothing happens. Is the encryption method different in the way Unity handles it?

User avatar
Joel
Moodkie Staff
Posts: 2971
Joined: Wed Nov 07, 2012 10:32 pm

Re: About Encryption & Decryption

Post by Joel »

Hi Johnny,

Regarding your first issue, this is because you've saved the data using encryption, but it's not being loaded with encryption enabled. If you show me the code you're using I can see what is happening.

With regards to decrypting it, the data is unencoded, so you cannot copy it as text and put it into an online decryptor (as this would apply an encoding to it, corrupting it). It's also encrypted using a buffer rather than as one large piece of data, meaning it would also need to be decrypted in a buffered way. You should only decrypt the data using the ES3 methods.

All the best,
Joel
Joel @ Moodkie Interactive
Twitter - Unity

Johnny Star
Posts: 25
Joined: Sat Mar 21, 2020 2:17 pm

Re: About Encryption & Decryption

Post by Johnny Star »

Sure here are the codes from 1 of my classes. All other classes also have the same saving and loading.

Code: Select all

    [Button, FoldoutGroupS01("Editor Methods Testing")]
    public void NewWallet(bool saveDataLocal = false, bool saveDataCloud = false)
    {
        collectableGoldCoin = 1000;
        collectableDiamond = 100;

        JUtil.Log(JUtil.LogHandle().Wallet, $"<b>Wallet ►</b> Created. (Local Save: {saveDataLocal}, Cloud Save: {saveDataCloud})");

        if (saveDataLocal) { SaveWalletLocal(); }
        if (saveDataCloud) { SaveWalletCloud(); }
    }

Code: Select all

    [Button, FoldoutGroupS01("Editor Methods Testing")]
    public void LoadWallet(bool newEntrySaveDataLocal = false, bool newEntrySaveDataCloud = false)
    {
        if (ES3.FileExists())
        {
            collectableGoldCoin = ES3.KeyExists("collectableGoldCoin") == true ? ES3.Load<int>("collectableGoldCoin") : -1;

            if (collectableGoldCoin != -1)
            {
                collectableDiamond = ES3.Load<int>("collectableDiamond");

                JUtil.Log(JUtil.LogHandle().Wallet, "<b>Wallet ►</b> Loaded.");
            }
            else
            {
                // Todo: Check if connected to an online game service and try to retrieve data from there.
                NewWallet(newEntrySaveDataLocal, newEntrySaveDataCloud);
            }
        }
        else
        {
            // Todo: Check if connected to an online game service and try to retrieve data from there.
            NewWallet(newEntrySaveDataLocal, newEntrySaveDataCloud);
        }
    }

Code: Select all

    private void SaveWalletLocal()
    {
        ES3.Save<int>("collectableGoldCoin", collectableGoldCoin);
        ES3.Save<int>("collectableDiamond", collectableDiamond);
    }
Ok, I understand regarding Question 2! Thank you :)

User avatar
Joel
Moodkie Staff
Posts: 2971
Joined: Wed Nov 07, 2012 10:32 pm

Re: About Encryption & Decryption

Post by Joel »

Hi there,

I'm afraid I can't work out why you're getting that error from the code you've shown me, which suggests it might be a configuration issue. Please could you create a new project with a basic scene which replicates it? I also recommend going to Window > Easy Save 3 > Tools > Clear Persistent Data Path to ensure that there's no old save data.

All the best,
Joel
Joel @ Moodkie Interactive
Twitter - Unity

Johnny Star
Posts: 25
Joined: Sat Mar 21, 2020 2:17 pm

Re: About Encryption & Decryption

Post by Johnny Star »

Hi Joel, thank you for getting back!

Unfortunately, I can't do that at the moment. My project is duck-taped together and it's quite big and messy.
I can copy-paste scripts or take screenshots if that helps but I really don't think I'm in the capacity to simplify my project. I hope you understand my situation.

I also ensured that there was no old data before running my game which then creates the save file with a long list of alphanumeric values as shown above in question 2.

*UPDATE*
The weird thing is that saving and loading works fine.

*UPDATE2*
I'm not giving up on this. Hopefully, the info I provide is useful.
Going into Easy Save > Auto Save > Then turning both 'Save Event' and 'Load Event' to None no longer logs the error anymore.
BUT, this has to be done in all scenes.

Johnny Star
Posts: 25
Joined: Sat Mar 21, 2020 2:17 pm

Re: About Encryption & Decryption

Post by Johnny Star »

So the *UPDATE2* mentioned above fixed the error. But I have a major concern or maybe I'm not thinking about the process right.

I have sent my APK to several people. The ES3 save file was not password protected. But now it is.
Within the Editor, a non password protected file has to be deleted (like you mentioned above) before enabling password protection. Also once password protected, I can't run the game as there are so many errors simply because the save file (originally non password protected) cannot be opened.

Then how can I update my APK for the people with my new APK with password protection enabled?

Also, what if Google saving was already done using non password protected bytes? How do I go about handling that?

User avatar
Joel
Moodkie Staff
Posts: 2971
Joined: Wed Nov 07, 2012 10:32 pm

Re: About Encryption & Decryption

Post by Joel »

Hi there,

Just to check, are you on the latest version of Easy Save? In a previous version there was an issue where Auto Save was being called even if there was no data to load which would cause the behaviour you're describing. If you private message me your invoice number I can send over the latest version to ensure that the Asset Store is distributing this to you.

With regards to handling unencrypted data, you would need to use a try/catch block to detect any error loading the data, and then attempt to load it with unencrypted settings if an error is thrown.

All the best,
Joel
Joel @ Moodkie Interactive
Twitter - Unity

Johnny Star
Posts: 25
Joined: Sat Mar 21, 2020 2:17 pm

Re: About Encryption & Decryption

Post by Johnny Star »

Yup my is the latest version just checked the changelog text in the changelog folder. It's 3.3.1f9

Erm... What codes do I use to try catch errors? Also, how do I load with unencrypted settings if I choose to encrypt it within the settings? Sorry, I'm really confused.

User avatar
Joel
Moodkie Staff
Posts: 2971
Joined: Wed Nov 07, 2012 10:32 pm

Re: About Encryption & Decryption

Post by Joel »

Hi there,

Catching any exception in the try/catch block should work, but more specifically you can catch a CryptographicException. Regarding loading with unencrypted settings, you need to provide an ES3Settings object as a parameter as described in the Encryption guide:

https://docs.moodkie.com/easy-save-3/es ... mpression/

All the best,
Joel
Joel @ Moodkie Interactive
Twitter - Unity

Johnny Star
Posts: 25
Joined: Sat Mar 21, 2020 2:17 pm

Re: About Encryption & Decryption

Post by Johnny Star »

I just checked the link. My god the example is so long! Do I have to add all this just to catch the exception? Sorry I've never did this before. Don't easy save have a way to tell if it's encrypted and then I handle it from there?

Post Reply