Class AesContext
- Namespace
- Godot
- Assembly
- GodotSharp.dll
This class holds the context information required for encryption and decryption operations with AES (Advanced Encryption Standard). Both AES-ECB and AES-CBC modes are supported.
using Godot;
using System.Diagnostics;
public partial class MyNode : Node
{
private AesContext _aes = new AesContext();
public override void _Ready()
{
string key = "My secret key!!!"; // Key must be either 16 or 32 bytes.
string data = "My secret text!!"; // Data size must be multiple of 16 bytes, apply padding if needed.
// Encrypt ECB
_aes.Start(AesContext.Mode.EcbEncrypt, key.ToUtf8Buffer());
byte[] encrypted = _aes.Update(data.ToUtf8Buffer());
_aes.Finish();
// Decrypt ECB
_aes.Start(AesContext.Mode.EcbDecrypt, key.ToUtf8Buffer());
byte[] decrypted = _aes.Update(encrypted);
_aes.Finish();
// Check ECB
Debug.Assert(decrypted == data.ToUtf8Buffer());
string iv = "My secret iv!!!!"; // IV must be of exactly 16 bytes.
// Encrypt CBC
_aes.Start(AesContext.Mode.EcbEncrypt, key.ToUtf8Buffer(), iv.ToUtf8Buffer());
encrypted = _aes.Update(data.ToUtf8Buffer());
_aes.Finish();
// Decrypt CBC
_aes.Start(AesContext.Mode.EcbDecrypt, key.ToUtf8Buffer(), iv.ToUtf8Buffer());
decrypted = _aes.Update(encrypted);
_aes.Finish();
// Check CBC
Debug.Assert(decrypted == data.ToUtf8Buffer());
}
}
[GodotClassName("AESContext")]
public class AesContext : RefCounted, IDisposable
- Inheritance
-
AesContext
- Implements
- Inherited Members
Constructors
AesContext()
public AesContext()
Methods
Finish()
Close this AES context so it can be started again. See Start(Mode, byte[], byte[]).
public void Finish()
GetIVState()
Get the current IV state for this context (IV gets updated when calling Update(byte[])). You normally don't need this function.
Note: This function only makes sense when the context is started with CbcEncrypt or CbcDecrypt.
public byte[] GetIVState()
Returns
- byte[]
HasGodotClassMethod(in godot_string_name)
Check if the type contains a method with the given name. This method is used by Godot to check if a method exists before invoking it. Do not call or override this method.
protected override bool HasGodotClassMethod(in godot_string_name method)
Parameters
method
godot_string_nameName of the method to check for.
Returns
HasGodotClassSignal(in godot_string_name)
Check if the type contains a signal with the given name. This method is used by Godot to check if a signal exists before raising it. Do not call or override this method.
protected override bool HasGodotClassSignal(in godot_string_name signal)
Parameters
signal
godot_string_nameName of the signal to check for.
Returns
InvokeGodotClassMethod(in godot_string_name, NativeVariantPtrArgs, out godot_variant)
Invokes the method with the given name, using the given arguments. This method is used by Godot to invoke methods from the engine side. Do not call or override this method.
protected override bool InvokeGodotClassMethod(in godot_string_name method, NativeVariantPtrArgs args, out godot_variant ret)
Parameters
method
godot_string_nameName of the method to invoke.
args
NativeVariantPtrArgsArguments to use with the invoked method.
ret
godot_variantValue returned by the invoked method.
Returns
Start(Mode, byte[], byte[])
Start the AES context in the given mode
. A key
of either 16 or 32 bytes must always be provided, while an iV
(initialization vector) of exactly 16 bytes, is only needed when mode
is either CbcEncrypt or CbcDecrypt.
public Error Start(AesContext.Mode mode, byte[] key, byte[] iV = null)
Parameters
mode
AesContext.Modekey
byte[]iV
byte[]If the parameter is null, then the default value is
Array.Empty<byte>()
.
Returns
Update(byte[])
Run the desired operation for this AES context. Will return a byte[] containing the result of encrypting (or decrypting) the given src
. See Start(Mode, byte[], byte[]) for mode of operation.
Note: The size of src
must be a multiple of 16. Apply some padding if needed.
public byte[] Update(byte[] src)
Parameters
src
byte[]
Returns
- byte[]