Encrypting a Config save file

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By MOSN

The docs here are a bit to short and not broad enough to be of help. Here they just use the store_var function in File, but what if I want to encrypt a ConfigFile and use set_value()?. Tried to apply the same technique directly, but it doesn’t seem to work:

var save_path = "res://SaveFile.cfg"
var config = ConfigFile.new()
var load_respone = config.load(save_path)
var file = File.new()

func saveValue(section, key, value):
	file.open_encrypted_with_pass(save_path, File.WRITE, "mypass")
	config.set_value(section, key, value)

func loadValue(section, key):
	file.open_encrypted_with_pass(save_path, File.READ, "mypass")
	return config.get_value(section, key, 0)
:bust_in_silhouette: Reply From: tim_e

You don’t have to use the File class along with ConfigFile. Try this:

var password = OS.get_unique_id() # works only on this computer
var save_path = "res://SaveFile.cfg"
var config = ConfigFile.new()
# skip this load if the file doesn't exist yet
var error = config.load_encrypted_pass(save_path, password)
config.set_value(section, key, value)
config.save_encrypted_pass(save_path, password)
config.save("res://SaveFile_unencrypted.cfg", password) # for testing

Ahh I see the problem. I thought ConfigFile didn’t have the save or load encrypted functions, because when I want to check the docs I usually just google it and the 3.1 docs apparently shows up when you do that. So I was looking at an outdated docs where ConfigFile seemingly didn’t have those functions. Well thanks I can hopefully get it working now.

MOSN | 2020-04-23 10:29