mirror of
https://github.com/godotengine/godot-docs-l10n.git
synced 2026-01-05 14:10:19 +03:00
233 lines
9.8 KiB
ReStructuredText
233 lines
9.8 KiB
ReStructuredText
:github_url: hide
|
||
|
||
.. _class_AESContext:
|
||
|
||
AESContext
|
||
==========
|
||
|
||
**繼承:** :ref:`RefCounted<class_RefCounted>` **<** :ref:`Object<class_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<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 (無回傳值。)`
|