以腳本加密密鑰進行編譯

The export dialog gives you the option to encrypt your scripts with a 256-bit AES key when releasing your project. This will make sure your scripts are not stored in plain text and can not easily be ripped by some script kiddie.

Of course, the key needs to be stored in the binary, but if it's compiled, optimized and without symbols, it would take some effort to find it.

要加密腳本,需要以同一組密鑰來從原始碼編譯匯出腳本。

警告

This will not work if you use official, precompiled export templates. It is absolutely required to compile your own export templates to use PCK encryption.

按部就班

  1. Generate a 256-bit AES key in hexadecimal format. You can use the aes-256-cbc variant from this service.

    Alternatively, you can generate it yourself using OpenSSL command-line tools:

    openssl rand -hex 32 > godot.gdkey
    

    The output in godot.gdkey should be similar to:

    # NOTE: Do not use the key below! Generate your own key instead.
    aeb1bc56aaf580cc31784e9c41551e9ed976ecba10d315db591e749f3f64890f
    

    產生密鑰時可以不將輸出重新導向輸出至檔案,這樣可以最小化密鑰暴露的風險。

  2. 將該密鑰在用來編譯 Godot 的主控台中設為環境變數,如:

    export SCRIPT_AES256_ENCRYPTION_KEY="your_generated_key"
    
  3. 編譯 Godot 匯出樣板,並將該匯出樣板在匯出預設設定中設為自定匯出樣板。

  4. Set the encryption key in the Script tab of the export preset:

    ../../_images/script_encryption_key.png
  5. 匯出專案。該遊戲現在應該會使用加密腳本來執行。

疑難排解

若遇到類似下方的錯誤,這代表加密密鑰沒有被正確包含到 Godot 建置中。這代表 Godot 在匯出時有加密腳本,但執行階段無法讀取。

ERROR: open_and_parse: Condition "String::md5(md5.digest) != String::md5(md5d)" is true. Returning: ERR_FILE_CORRUPT
   At: core/io/file_access_encrypted.cpp:103
ERROR: load_byte_code: Condition ' err ' is true. returned: err
   At: modules/gdscript/gdscript.cpp:755
ERROR: load: Condition ' err != OK ' is true. returned: RES()
   At: modules/gdscript/gdscript.cpp:2135
ERROR: Failed loading resource: res://Node2D.gde
   At: core/io/resource_loader.cpp:279
ERROR: poll: res://Node2D.tscn:3 - Parse Error: [ext_resource] referenced nonexistent resource at: res://Node2D.gd
   At: scene/resources/scene_format_text.cpp:439
ERROR: load: Condition ' err != OK ' is true. returned: RES()
   At: core/io/resource_loader.cpp:202
ERROR: Failed loading resource: res://Node2D.tscn
   At: core/io/resource_loader.cpp:279
ERROR: Failed loading scene: res://Node2D.tscn
   At: main/main.cpp:1727
WARNING: cleanup: ObjectDB Instances still exist!
     At: core/object.cpp:2081
ERROR: clear: Resources Still in use at Exit!
   At: core/resource.cpp:425