使用脚本加密密钥编译

发布项目时,导出对话框为您提供了使用 256 位 AES 密钥加密脚本的选项。这样可以确保您的脚本不会以纯文本格式存储,不会被某些脚本小子轻易解包。当然,密钥需要存储在二进制文件中,但是如果对其进行了编译、优化,并且不带符号,则将需要花费一些功夫才能找到它。

当然,密钥需要存储在二进制文件中,但是经过编译、优化、去除符号后,是需要花费一些精力才能找到的。

为此,您需要使用相同的密钥,从源代码构建导出模板。

警告

如果你使用官方预编译的导出模板,是不会起作用的。要使用 PCK 加密,绝对必须自己编译导出模板。

渐进式教程

  1. 生成十六进制的 256 位 AES 密钥。您可以使用此服务的 aes-256-cbc 版本。

    或者,您可以通过使用 OpenSSL 命令行工具自己生成:

    openssl rand -hex 32 > godot.gdkey
    

    godot.gdkey 中的输出应该类似:

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

    不将输出重定向到文件也可以生成密钥,但这样能将暴露密钥的风险降到最低。

  2. 在控制台中将此处的密钥(key)设置为环境变量,编译 Godot 时会用到,如下所示:

    export SCRIPT_AES256_ENCRYPTION_KEY="your_generated_key"
    
  3. 编译 Godot 导出模板,并在导出预设的选项中将它们设置为自定义导出模板。

  4. 在导出预设的脚本选项卡中设置加密密钥:

    ../../_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