:github_url: hide .. _class_AESContext: AESContext ========== **继承:** :ref:`RefCounted` **<** :ref:`Object` 提供对原始数据的 AES 加密/解密的访问。 .. rst-class:: classref-introduction-group 描述 ---- 这个类存放的是进行 AES(Advanced Encryption Standard,高级加密标准)加解密所需的上下文信息。支持 AES-ECB 和 AES-CBC 两种模式。 .. tabs:: .. code-tab:: gdscript extends Node var aes = AESContext.new() func _ready(): var key = "My secret key!!!" # 密钥必须是 16 或 32 字节。 var data = "My secret text!!" # 数据大小必须是 16 字节的倍数,需要时添加补白。 # ECB 加密 aes.start(AESContext.MODE_ECB_ENCRYPT, key.to_utf8_buffer()) var encrypted = aes.update(data.to_utf8_buffer()) aes.finish() # ECB 解密 aes.start(AESContext.MODE_ECB_DECRYPT, key.to_utf8_buffer()) var decrypted = aes.update(encrypted) aes.finish() # ECB 校验 assert(decrypted == data.to_utf8_buffer()) var iv = "My secret iv!!!!" # IV 必须是 16 字节。 # CBC 加密 aes.start(AESContext.MODE_CBC_ENCRYPT, key.to_utf8_buffer(), iv.to_utf8_buffer()) encrypted = aes.update(data.to_utf8_buffer()) aes.finish() # CBC 解密 aes.start(AESContext.MODE_CBC_DECRYPT, key.to_utf8_buffer(), iv.to_utf8_buffer()) decrypted = aes.update(encrypted) aes.finish() # CBC 校验 assert(decrypted == data.to_utf8_buffer()) .. code-tab:: csharp using Godot; using System.Diagnostics; public partial class MyNode : Node { private AesContext _aes = new AesContext(); public override void _Ready() { string key = "My secret key!!!"; // 密钥必须是 16 或 32 字节。 string data = "My secret text!!"; // 数据大小必须是 16 字节的倍数,需要时添加补白。 // ECB 加密 _aes.Start(AesContext.Mode.EcbEncrypt, key.ToUtf8Buffer()); byte[] encrypted = _aes.Update(data.ToUtf8Buffer()); _aes.Finish(); // ECB 解密 _aes.Start(AesContext.Mode.EcbDecrypt, key.ToUtf8Buffer()); byte[] decrypted = _aes.Update(encrypted); _aes.Finish(); // ECB 校验 Debug.Assert(decrypted == data.ToUtf8Buffer()); string iv = "My secret iv!!!!"; // IV 必须是 16 字节。 // CBC 加密 _aes.Start(AesContext.Mode.EcbEncrypt, key.ToUtf8Buffer(), iv.ToUtf8Buffer()); encrypted = _aes.Update(data.ToUtf8Buffer()); _aes.Finish(); // CBC 解密 _aes.Start(AesContext.Mode.EcbDecrypt, key.ToUtf8Buffer(), iv.ToUtf8Buffer()); decrypted = _aes.Update(encrypted); _aes.Finish(); // CBC 校验 Debug.Assert(decrypted == data.ToUtf8Buffer()); } } .. rst-class:: classref-reftable-group 方法 ---- .. table:: :widths: auto +-----------------------------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | |void| | :ref:`finish`\ (\ ) | +-----------------------------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | :ref:`PackedByteArray` | :ref:`get_iv_state`\ (\ ) | +-----------------------------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | :ref:`Error` | :ref:`start`\ (\ mode\: :ref:`Mode`, key\: :ref:`PackedByteArray`, iv\: :ref:`PackedByteArray` = PackedByteArray()\ ) | +-----------------------------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | :ref:`PackedByteArray` | :ref:`update`\ (\ src\: :ref:`PackedByteArray`\ ) | +-----------------------------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ .. rst-class:: classref-section-separator ---- .. rst-class:: classref-descriptions-group 枚举 ---- .. _enum_AESContext_Mode: .. rst-class:: classref-enumeration enum **Mode**: :ref:`🔗` .. _class_AESContext_constant_MODE_ECB_ENCRYPT: .. rst-class:: classref-enumeration-constant :ref:`Mode` **MODE_ECB_ENCRYPT** = ``0`` AES 电子密码簿加密模式。 .. _class_AESContext_constant_MODE_ECB_DECRYPT: .. rst-class:: classref-enumeration-constant :ref:`Mode` **MODE_ECB_DECRYPT** = ``1`` AES 电子密码簿解密模式。 .. _class_AESContext_constant_MODE_CBC_ENCRYPT: .. rst-class:: classref-enumeration-constant :ref:`Mode` **MODE_CBC_ENCRYPT** = ``2`` AES cipher block chaining encryption mode. .. _class_AESContext_constant_MODE_CBC_DECRYPT: .. rst-class:: classref-enumeration-constant :ref:`Mode` **MODE_CBC_DECRYPT** = ``3`` AES cipher block chaining decryption mode. .. _class_AESContext_constant_MODE_MAX: .. rst-class:: classref-enumeration-constant :ref:`Mode` **MODE_MAX** = ``4`` 模式列举的最大值。 .. rst-class:: classref-section-separator ---- .. rst-class:: classref-descriptions-group 方法说明 -------- .. _class_AESContext_method_finish: .. rst-class:: classref-method |void| **finish**\ (\ ) :ref:`🔗` 关闭此 AES 上下文,以便可以再次启动它。见 :ref:`start()`\ 。 .. rst-class:: classref-item-separator ---- .. _class_AESContext_method_get_iv_state: .. rst-class:: classref-method :ref:`PackedByteArray` **get_iv_state**\ (\ ) :ref:`🔗` 获取此上下文的当前 IV 状态(调用 :ref:`update()` 时会更新 IV)。通常不需要此函数。 \ **注意:**\ 仅当上下文以 :ref:`MODE_CBC_ENCRYPT` 或 :ref:`MODE_CBC_DECRYPT` 开头时,此函数才有意义。 .. rst-class:: classref-item-separator ---- .. _class_AESContext_method_start: .. rst-class:: classref-method :ref:`Error` **start**\ (\ mode\: :ref:`Mode`, key\: :ref:`PackedByteArray`, iv\: :ref:`PackedByteArray` = PackedByteArray()\ ) :ref:`🔗` 在给定的 ``mode`` 中启动 AES 上下文。必须始终提供 16 或 32 字节的 ``key``\ ,而仅当 ``mode`` 为 :ref:`MODE_CBC_ENCRYPT` 或 :ref:`MODE_CBC_DECRYPT` 时,才需要正好为 16 字节的 ``iv``\ (初始化向量)。 .. rst-class:: classref-item-separator ---- .. _class_AESContext_method_update: .. rst-class:: classref-method :ref:`PackedByteArray` **update**\ (\ src\: :ref:`PackedByteArray`\ ) :ref:`🔗` 运行此 AES 上下文所需的操作。将返回包含加密(或解密)给定 ``src`` 结果的 :ref:`PackedByteArray` 。有关操作模式,请参阅 :ref:`start()`\ 。 \ **注意:**\ ``src`` 的大小必须是 16 倍的倍数。如果需要,应用一些填充。 .. |virtual| replace:: :abbr:`virtual (本方法通常需要用户覆盖才能生效。)` .. |required| replace:: :abbr:`required (This method is required to be overridden when extending its base class.)` .. |const| replace:: :abbr:`const (本方法无副作用,不会修改该实例的任何成员变量。)` .. |vararg| replace:: :abbr:`vararg (本方法除了能接受在此处描述的参数外,还能够继续接受任意数量的参数。)` .. |constructor| replace:: :abbr:`constructor (本方法用于构造某个类型。)` .. |static| replace:: :abbr:`static (调用本方法无需实例,可直接使用类名进行调用。)` .. |operator| replace:: :abbr:`operator (本方法描述的是使用本类型作为左操作数的有效运算符。)` .. |bitfield| replace:: :abbr:`BitField (这个值是由下列位标志构成位掩码的整数。)` .. |void| replace:: :abbr:`void (无返回值。)`