Trying to write a decrypion tool

Discussion and help for Easy Save 3
Post Reply
diccon
Posts: 5
Joined: Wed Jun 08, 2022 9:39 pm

Trying to write a decrypion tool

Post by diccon »

Hello,
I've just started to implement encryption for the save files on my project and its worked quite smoothly so far, but now I am trying to write a tool to decrypt the save files and resave them as text for debugging and I have run into a problem. The code looks like this:

Code: Select all

 
ES3Settings fileSettings = new ES3Settings(path,  ES3.Location.Cache);
ES3Settings settings = new ES3Settings(path, ES3.EncryptionType.AES, info.key, fileSettings);
ES3.CacheFile(path);
string contents = ES3.LoadRawString(settings);

if (!string.IsNullOrEmpty(contents))
{
	contents = ES3.DecryptString(contents, info.key);
      	ES3Settings textSettings = new ES3Settings(newPath, ES3.EncryptionType.None);
        ES3.SaveRaw(contents, newPath, textSettings);
}
However ES3.DecryptString throws an exception:
ArgumentException: Offset and length were out of bounds for the array or count is greater than the number of elements from index to the end of the source collection.

I'm just guessing that this is a reasonable method to do what I want, and I'm guessing that I need to call DecryptString because the result of LoadRawString is not human readable. Can you let me know if I am on the right track here or if I need to do something different?

Thanks for any help!
User avatar
Joel
Moodkie Staff
Posts: 4849
Joined: Wed Nov 07, 2012 10:32 pm

Re: Trying to write a decrypion tool

Post by Joel »

Hi there,

To decrypt a file you can simply use the ES3.LoadRawString method with encryption enabled. I.e.

Code: Select all

var settings = new ES3Settings(path, ES3.EncryptionType.AES, password);
var decrypted = ES3.LoadRawString(settings));
All the best,
Joel
Joel @ Moodkie Interactive
Purchase Easy Save | Contact | Guides | Docs | Getting started
diccon
Posts: 5
Joined: Wed Jun 08, 2022 9:39 pm

Re: Trying to write a decrypion tool

Post by diccon »

Thanks for your quick reply, I am finding that ES3.LoadRawString is returning a non readable strings so I set up a test to generate and read a file with a few simple keys and I found that using the cache setting seems to determine whether LoadRawString returns the correct string.
I tested these combinations:

- Write to Location.File, Read from Location.File, LoadRawString returns readable string
- Write to Location.Cache (and then StoreCachedFile), Read from Location.Cache, returns non readable string
- Write to Location.Cache, Read from Location.File, LoadRawString throws exception.

Since I need to use Location.Cache I do need this option to work. Can you tell me if this looks correct?
Writing:

Code: Select all

 	ES3Settings fileSettings = new ES3Settings(path, ES3.Location.Cache);
        ES3Settings settings = new ES3Settings(path, ES3.EncryptionType.AES, "mykey", fileSettings);
        ES3.Save("list", ints, path, settings);
        ES3.Save("string", test, path, settings);
        ES3.Save("float", testFloat, path, settings);

        ES3.StoreCachedFile(path);
Reading:

Code: Select all

	ES3Settings fileSettings = new ES3Settings(path,  ES3.Location.Cache);
        ES3Settings settings = new ES3Settings(path, ES3.EncryptionType.AES, "mykey", fileSettings);
        ES3.CacheFile(path);
        string[] keys = ES3.GetKeys(path, settings);
        for (int i = 0; i < keys.Length; ++i)
        {
        	Debug.Log(keys[i]);
        }   
        string contents = ES3.LoadRawString(path, settings);
What I am finding is that GetKeys correctly gets the keys but LoadRawString is not readable so the data seems correct, but something else is wrong, can you see what that might be?
diccon
Posts: 5
Joined: Wed Jun 08, 2022 9:39 pm

Re: Trying to write a decrypion tool

Post by diccon »

I have figured out part of my problem - I need to do ES3.StoreCachedFile(path, settings) in order for it to save as encrypted. This seems to be enough to get my test working so I'll post back if I have more problems.
diccon
Posts: 5
Joined: Wed Jun 08, 2022 9:39 pm

Re: Trying to write a decrypion tool

Post by diccon »

So I have made some good progress, and can now successfully save encrypted files and the reload them with the tool to decrypt to save as text, using the Location.File setting.
The problem is now that my game uses Location.Cache to load and save and I get an exception trying to cache the encrypted files, this is the loading code:

Code: Select all

	 	ES3Settings fileSettings = new ES3Settings(path,  ES3.Location.Cache);
	 	ES3Settings settings = new ES3Settings(path, ES3.EncryptionType.AES, "my_key", fileSettings);
	 	ES3.CacheFile(path, settings);
The exception gets thrown from EncryptionAlgorithm.CopyStream in the while loop

Code: Select all

ArgumentException: Offset and length were out of bounds for the array or count is greater than the number of elements from index to the end of the source collection.
ES3Internal.ES3Stream.CreateStream (System.IO.Stream stream, ES3Settings settings, ES3Internal.ES3FileMode fileMode) (at Assets/Plugins/Easy Save 3/Scripts/Streams/ES3Stream.cs:102)
ES3Reader.Create (System.Byte[] bytes, ES3Settings settings) (at Assets/Plugins/Easy Save 3/Scripts/Readers/ES3Reader.cs:364)
ES3File.SaveRaw (System.Byte[] bytes, ES3Settings settings) (at Assets/Plugins/Easy Save 3/Scripts/ES3File.cs:170)
ES3File..ctor (System.Byte[] bytes, ES3Settings settings) (at Assets/Plugins/Easy Save 3/Scripts/ES3File.cs:83)
ES3File.CacheFile (ES3Settings settings) (at Assets/Plugins/Easy Save 3/Scripts/ES3File.cs:371)
ES3.CacheFile (ES3Settings settings) (at Assets/Plugins/Easy Save 3/Scripts/ES3.cs:1464)
Can you see if I am doing anything wrong here? Thanks again!
User avatar
Joel
Moodkie Staff
Posts: 4849
Joined: Wed Nov 07, 2012 10:32 pm

Re: Trying to write a decrypion tool

Post by Joel »

Hi there,

This error usually means that the data you're trying to load is not encrypted but you're trying to load it with encryption enabled, or was encrypted with a different password to the one you're using to load.

All the best,
Joel
Joel @ Moodkie Interactive
Purchase Easy Save | Contact | Guides | Docs | Getting started
diccon
Posts: 5
Joined: Wed Jun 08, 2022 9:39 pm

Re: Trying to write a decrypion tool

Post by diccon »

Thanks Joel,
the thing that confuses me is that when I load exactly the same file using Location.File instead of Location.Cache then it loads correctly and I can read the keys. I can confirm that the file is encrypted and the password is the same, which leads me to think that I might be calling ES3.CacheFile incorrectly. Do you think you can post a couple of lines of code to show how you would set up a call to ES3.CacheFile for an encrypted file?
User avatar
Joel
Moodkie Staff
Posts: 4849
Joined: Wed Nov 07, 2012 10:32 pm

Re: Trying to write a decrypion tool

Post by Joel »

Hi there,

Here is a full example:

Code: Select all

var settings = new ES3Settings(ES3.Location.Cache, ES3.EncryptionType.AES);

// Save a file to the cache.
ES3.Save("myKey", 123, "myFile.es3", settings);

// Store the cached file to hard disk.
ES3.StoreCachedFile("myFile.es3", settings);

// Delete the file.
ES3.DeleteFile("myFile.es3", settings);

// Load the file from disk into the cache.
ES3.CacheFile("myFile.es3", settings);

// Load our data from the file in cache.
Debug.Log(ES3.Load<int>("myKey", "myFile.es3", settings));

// Load our file as a raw, unencrypted string.
Debug.Log(ES3.LoadRawString("myFile.es3", settings));
Note that you should not have to specify File as a location, as ES3.CacheFile and ES3.StoreCachedFile both assume that data is to and from File.

I also recommend deleting any previous save data in case you've previously saved unencrypted data.

All the best,
Joel
Joel @ Moodkie Interactive
Purchase Easy Save | Contact | Guides | Docs | Getting started
Post Reply