encrypting_save_games.rst 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. .. _doc_encrypting_save_games:
  2. Encrypting save games
  3. =====================
  4. Why?
  5. ----
  6. The class :ref:`File <class_File>` can open a file at a
  7. location and read/write data (integers, strings and variants).
  8. It also supports encryption.
  9. To create an encrypted file, a passphrase must be provided, like this:
  10. .. tabs::
  11. .. code-tab:: gdscript GDScript
  12. var f = File.new()
  13. var err = f.open_encrypted_with_pass("user://savedata.bin", File.WRITE, "mypass")
  14. f.store_var(game_state)
  15. f.close()
  16. .. code-tab:: csharp
  17. var f = new File();
  18. var err = f.OpenEncryptedWithPass("user://savedata.bin", (int)File.ModeFlags.Write, "mypass");
  19. f.StoreVar(gameState);
  20. f.Close();
  21. This will make the file unreadable to users, but will still not prevent
  22. them sharing save files. To solve this, use the device unique id or
  23. some unique user identifier, for example:
  24. .. tabs::
  25. .. code-tab:: gdscript GDScript
  26. var f = File.new()
  27. var err = f.open_encrypted_with_pass("user://savedata.bin", File.WRITE, OS.get_unique_id())
  28. f.store_var(game_state)
  29. f.close()
  30. .. code-tab:: csharp
  31. var f = new File();
  32. var err = f.OpenEncryptedWithPass("user://savedata.bin", (int)File.ModeFlags.Write, OS.GetUniqueId());
  33. f.StoreVar(gameState);
  34. f.Close();
  35. Note that ``OS.get_unique_ID()`` only works on iOS and Android.
  36. This is all! Thanks for your cooperation, citizen.