[Godot] Encrypt your game files

Secure your game assets before release

Posted by Tib Averus on 30th Jun 2024

I made a guide a while ago on the Godot forums that explained how you can encrypt your Godot games with very simple steps to follow. While this blog post will be more or less the same, I wanted to use different wording and re-write some bits to make it more simple to follow!

Please note that this method does not make your assets completely safe. No encryption is foolproof, but it will make it much harder for the average user to retrieve them.

ANY TIPS FOR IMPROVEMENT OR MAKING THIS EASIER TO UNDERSTAND ARE WELCOME! IF YOU NEED HELP, DON'T HESITATE TO REACH OUT!

This is just ONE way to do this, and I find it to be the easiest! Feel free to experiment with other methods if you prefer.

IMPORTANT: This method limits your game to 64-bit machines only!

This tutorial is written for Windows machines; the steps are different for macOS and Linux.

Let's get started! The post might seem long, but the steps are simple. I've detailed each step as much as possible.

Prerequisites

  • Git - You need Git installed on your machine to clone the Godot repository. Download Git here and run the installer, selecting the "Recommended" options.

  • Scons - Scons is a Software Build Tool used to compile the Godot source code for our custom template. Here's how to install Scons:

    1. Install Python - Scons uses Python to automate the build process. Download Python here and follow the installation process. If prompted to reboot your PC, do so.
    2. After installing Python, open a terminal window (or CMD) as an administrator. Search for "cmd" in the start menu, right-click it, and select "Run as Administrator." Then, install Scons by running:
      pip install scons
  • LLVM MinGW - Download and install a toolchain to aid in building the Godot source. Follow these steps:

    1. Go to this link and download the file ending with "msvcrt-x86_64.zip".
    2. Extract the file to a memorable location. I recommend renaming the folder to something simple, like "llvm-mingw." In this example, I'll use C:\Users\YourUsername\Applications\llvm-mingw.
    3. Include the folder in your system's PATH. Right-click "This PC" and choose "Properties."
    4. Go to "Advanced system settings" -> "Environment Variables."
    5. Under "System variables," find "Path" and click "Edit."
    6. Add a new entry with the path to the bin folder inside the extracted folder. In this example, it's C:\Users\YourUsername\Applications\llvm-mingw\bin.
    7. Click OK and close the terminal (CMD) window, then reopen it to apply the changes.

Procedure

Time to get started now that we have everything set up!

  • Encryption Key

    1. Generate an encryption key. This key is crucial, like a password, and is used to decrypt the PCK file. Store it safely and do not share it. Use this key for all future releases of your game. The easiest way to generate this key is via the Godot documentation or directly here.
    2. On the site, select aes-256-cbc and enter a random passphrase.
    3. Click "Generate Key." The encryption key will appear in the "Encrypted (Base-64)" field.
    4. Copy the key part of the output. For example:
      salt=BBC680352068FB3B
      key=AD2166A50EF24DBC2B298A049C529B3D68638DA2CACC22E7F87BD8ECB4D1098A
      iv =EA073E2E2171F7F85C73E7F2C108DEAB

      (THIS IS AN EXAMPLE! DO NOT USE THIS KEY, GENERATE YOUR OWN!)

    5. Store the key safely, such as in a password manager like Bitwarden.
  • Getting the Source Code

    1. Create a folder for the Godot source code. For example, C:\Users\YourUsername\SourceCodes.
    2. Open a terminal window and navigate to the folder using:
      cd C:\Users\YourUsername\SourceCodes
    3. Clone the Godot repository with:
      git clone https://github.com/godotengine/godot.git
    4. Navigate inside the cloned repository:
      cd godot
  • Switching Versions

    1. Choose the version to compile, typically the same version you are using to create your game. As of March 2024, the latest release is 4.2.1-stable.
    2. List all current releases with:
      git tag -l
    3. Find your version and switch to it with:
      git switch --detach versionNumber

      In this example:

      git switch --detach 4.2.1-stable
  • Using the Encryption Key

    1. Use your encryption key in the terminal session. Depending on whether you use CMD or PowerShell, run one of the following commands:
      • For CMD:
        set SCRIPT_AES256_ENCRYPTION_KEY=your_generated_key
      • For PowerShell:
        $env:SCRIPT_AES256_ENCRYPTION_KEY="your_generated_key"
  • Compiling the Source Code

    Now, compile the source code. Use one of the following commands, depending on your version of Godot:

    • For non-C# versions:

      scons platform=windows use_mingw=yes arch=x86_64 target=template_release use_llvm=true mingw64_prefix=x86_64-w64-mingw32-
    • For C# and mono versions:

      scons platform=windows use_mingw=yes arch=x86_64 target=template_release use_llvm=true mingw64_prefix=x86_64-w64-mingw32- module_mono_enabled=yes 

    This process may take a long time. Once finished, you'll find new files in the bin folder:

    godot.windows.template_release.x86_64.llvm.mono.console.exe
    godot.windows.template_release.x86_64.llvm.mono.exe

    Use the one without "console" at the end.

  • Using the Template

    1. Open your Godot project and go to "Project" -> "Export..."
    2. Click "Add..." and select "Windows Desktop."
    3. In the Options tab, set the "Custom Template" fields for both Debug and Release to the file created during the compilation:
      C:\Users\YourUsername\SourceCodes\godot\bin\godot.windows.template_release.x86_64.llvm.mono.exe
    4. In the "Encryption" tab, tick "Encrypt Exported PCK" and paste the same encryption key.
    5. Include all files by entering a single asterisk * in the "Filters to include files/folders" field.
    6. Set up other export options and export your project using the "Export Project..." button.

And that's it! Your game's PCK file is now encrypted, making it difficult for players to open with tools like this one. Keep your encryption key safe, and if you change the engine version, repeat the steps starting from "Switching Versions."