Files
godot-docs/tutorials/io/encrypting_save_games.rst
T

51 lines
1.4 KiB
ReStructuredText

.. _doc_encrypting_save_games:
Encrypting save games
=====================
Why?
----
The class :ref:`File <class_File>` can open a file at a
location and read/write data (integers, strings and variants).
It also supports encryption.
To create an encrypted file, a passphrase must be provided, like this:
.. tabs::
.. code-tab:: gdscript GDScript
var f = File.new()
var err = f.open_encrypted_with_pass("user://savedata.bin", File.WRITE, "mypass")
f.store_var(game_state)
f.close()
.. code-tab:: csharp
var f = new File();
var err = f.OpenEncryptedWithPass("user://savedata.bin", (int)File.ModeFlags.Write, "mypass");
f.StoreVar(gameState);
f.Close();
This will make the file unreadable to users, but will still not prevent
them sharing save files. To solve this, use the device unique id or
some unique user identifier, for example:
.. tabs::
.. code-tab:: gdscript GDScript
var f = File.new()
var err = f.open_encrypted_with_pass("user://savedata.bin", File.WRITE, OS.get_unique_id())
f.store_var(game_state)
f.close()
.. code-tab:: csharp
var f = new File();
var err = f.OpenEncryptedWithPass("user://savedata.bin", (int)File.ModeFlags.Write, OS.GetUniqueId());
f.StoreVar(gameState);
f.Close();
Note that ``OS.get_unique_ID()`` only works on iOS and Android.
This is all! Thanks for your cooperation, citizen.