Scrambling a Text File (Save+Load) - how?

Godot Version

4.2 and 4.3

Question

How do we scramble a text file in Godot? I have a JSON save game, but I want to make it difficult for users to edit so that they don’t hack the game easily and get to all the multiple endings without context.

I tried hex_encode which makes it a nice scramble but can’t go backwards.


And this documentation is quite confusing. What is buf?

Hi davek,
In order to make a save file hard to mess with, you can encrypt the file.
Check out the open_encrypted() and open_encrypted_with_pass() functions at the FileAccess documentation.

(I can’t put any more links but I’d recommend Game Endeavor’s tutorial for savefiles: “Save And Load Data in Godot 3.2”)
Although the tutorial is old, the section that is about the encryption is still relevant as far as I’m aware (Starts at 9:30).

Also check out the feature to have savefiles as resources (.tres).

1 Like

Here’s a video explaining using resources as savefiles.
I hope I helped.

Thanks Corner I’ll take a look, the encrypted stuff looks promising.

I’m already saving+loading, I just want to scramble it

Anyone know how to save encrypted?

Works same way as open() since open_encrypted() returns a FileAccess object. Just look at example :

func save(content):
	var file = FileAccess.open("user://save_game.dat", FileAccess.WRITE)
	file.store_string(content)

func load():
	var file = FileAccess.open("user://save_game.dat", FileAccess.READ)
	var content = file.get_as_text()
	return content

What does that have to do with saving encrypted?

I’m wondering if people are reading the topic. This is about more than standard saving+loading

I can only seem to get half the solution. I can save scrambled file, but can’t load it, or vice versa, depending on what I try

@wyattbiker 's example is very relevant, one must replace open with open_encrypted or, I prefer with a password

func save(content):
	var file = FileAccess.open_encrypted_with_pass("user://save_game.dat", FileAccess.WRITE, "my secret password!")
	file.store_string(content)

func load():
	var file = FileAccess.open_encrypted_with_pass("user://save_game.dat", FileAccess.READ, "my secret password!")
	var content = file.get_as_text()
	return content

You could also show your script for hex_encode, it’s better if we know what you’ve got so far.

Yay this works for me gertkeno, thanks!

It may have been obvious to everyone else, but, once you put the replacement there I finally got it, haha

1 Like

Another relevant function is open_compressed, makes the files scrambled and bonus reduced file size! It’s not impossible to reverse, but it’s not a standard compression so it’s harder than “unzip”

I read the topic but I thought you would check the docs. Glad it works for you though!

It wasn’t obvious to me that open_encrypted() works for both saving and loading. So when I saw “open_encrypted” I didn’t realize it was used for both, I thought there must be another function for saving like “save_encrypted”. That might be blindingly obvious to everyone else but not to me I guess

Also the docs for hex_decode() have broken example that I posted above so my trust was broken to examine them closely

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.