Files
godot-docs-l10n/classes/zh_Hans/class_aescontext.rst
Rémi Verschelde c3f2364c10 Sync classref with 4.6 branch
Lots of translations invalidated (fuzzied) as we just synced Weblate.
2025-12-19 16:39:51 +01:00

233 lines
9.7 KiB
ReStructuredText
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

:github_url: hide
.. _class_AESContext:
AESContext
==========
**继承:** :ref:`RefCounted<class_RefCounted>` **<** :ref:`Object<class_Object>`
提供对原始数据的 AES 加密/解密的访问。
.. rst-class:: classref-introduction-group
描述
----
这个类存放的是进行 AESAdvanced 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<class_AESContext_method_finish>`\ (\ ) |
+-----------------------------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| :ref:`PackedByteArray<class_PackedByteArray>` | :ref:`get_iv_state<class_AESContext_method_get_iv_state>`\ (\ ) |
+-----------------------------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| :ref:`Error<enum_@GlobalScope_Error>` | :ref:`start<class_AESContext_method_start>`\ (\ mode\: :ref:`Mode<enum_AESContext_Mode>`, key\: :ref:`PackedByteArray<class_PackedByteArray>`, iv\: :ref:`PackedByteArray<class_PackedByteArray>` = PackedByteArray()\ ) |
+-----------------------------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| :ref:`PackedByteArray<class_PackedByteArray>` | :ref:`update<class_AESContext_method_update>`\ (\ src\: :ref:`PackedByteArray<class_PackedByteArray>`\ ) |
+-----------------------------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
.. rst-class:: classref-section-separator
----
.. rst-class:: classref-descriptions-group
枚举
----
.. _enum_AESContext_Mode:
.. rst-class:: classref-enumeration
enum **Mode**: :ref:`🔗<enum_AESContext_Mode>`
.. _class_AESContext_constant_MODE_ECB_ENCRYPT:
.. rst-class:: classref-enumeration-constant
:ref:`Mode<enum_AESContext_Mode>` **MODE_ECB_ENCRYPT** = ``0``
AES 电子密码簿加密模式。
.. _class_AESContext_constant_MODE_ECB_DECRYPT:
.. rst-class:: classref-enumeration-constant
:ref:`Mode<enum_AESContext_Mode>` **MODE_ECB_DECRYPT** = ``1``
AES 电子密码簿解密模式。
.. _class_AESContext_constant_MODE_CBC_ENCRYPT:
.. rst-class:: classref-enumeration-constant
:ref:`Mode<enum_AESContext_Mode>` **MODE_CBC_ENCRYPT** = ``2``
AES cipher block chaining encryption mode.
.. _class_AESContext_constant_MODE_CBC_DECRYPT:
.. rst-class:: classref-enumeration-constant
:ref:`Mode<enum_AESContext_Mode>` **MODE_CBC_DECRYPT** = ``3``
AES cipher block chaining decryption mode.
.. _class_AESContext_constant_MODE_MAX:
.. rst-class:: classref-enumeration-constant
:ref:`Mode<enum_AESContext_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:`🔗<class_AESContext_method_finish>`
关闭此 AES 上下文,以便可以再次启动它。见 :ref:`start()<class_AESContext_method_start>`\ 。
.. rst-class:: classref-item-separator
----
.. _class_AESContext_method_get_iv_state:
.. rst-class:: classref-method
:ref:`PackedByteArray<class_PackedByteArray>` **get_iv_state**\ (\ ) :ref:`🔗<class_AESContext_method_get_iv_state>`
获取此上下文的当前 IV 状态(调用 :ref:`update()<class_AESContext_method_update>` 时会更新 IV。通常不需要此函数。
\ **注意:**\ 仅当上下文以 :ref:`MODE_CBC_ENCRYPT<class_AESContext_constant_MODE_CBC_ENCRYPT>`:ref:`MODE_CBC_DECRYPT<class_AESContext_constant_MODE_CBC_DECRYPT>` 开头时,此函数才有意义。
.. rst-class:: classref-item-separator
----
.. _class_AESContext_method_start:
.. rst-class:: classref-method
:ref:`Error<enum_@GlobalScope_Error>` **start**\ (\ mode\: :ref:`Mode<enum_AESContext_Mode>`, key\: :ref:`PackedByteArray<class_PackedByteArray>`, iv\: :ref:`PackedByteArray<class_PackedByteArray>` = PackedByteArray()\ ) :ref:`🔗<class_AESContext_method_start>`
在给定的 ``mode`` 中启动 AES 上下文。必须始终提供 16 或 32 字节的 ``key``\ ,而仅当 ``mode``:ref:`MODE_CBC_ENCRYPT<class_AESContext_constant_MODE_CBC_ENCRYPT>`:ref:`MODE_CBC_DECRYPT<class_AESContext_constant_MODE_CBC_DECRYPT>` 时,才需要正好为 16 字节的 ``iv``\ (初始化向量)。
.. rst-class:: classref-item-separator
----
.. _class_AESContext_method_update:
.. rst-class:: classref-method
:ref:`PackedByteArray<class_PackedByteArray>` **update**\ (\ src\: :ref:`PackedByteArray<class_PackedByteArray>`\ ) :ref:`🔗<class_AESContext_method_update>`
运行此 AES 上下文所需的操作。将返回包含加密(或解密)给定 ``src`` 结果的 :ref:`PackedByteArray<class_PackedByteArray>` 。有关操作模式,请参阅 :ref:`start()<class_AESContext_method_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 (无返回值。)`