reckfellENGINE

AesContext

Provides access to AES encryption/decryption of raw data.

On this page
class AesContextReckfellSharpInherits RefCounted

Provides access to AES encryption/decryption of raw data.

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.

C#
using Reckfell;
using System.Diagnostics;

public partial class MyNode : Entity
{
    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());
    }
}

Declarations checked against C# SDK 1.0.0. Expand a name to see declarations and documentation.

Constructors1

AesContextConstructor
# AesContext
C#
public AesContext();

Methods6

Finishvoid
# Finish
C#
public void Finish();

Close this AES context so it can be started again. See AesContext.Start.

GetIVStatebyte[] · Deprecated
# GetIVState
C#
public byte[] GetIVState();

Returns an empty byte[]. Note: This method used to return the active IV state as modified by one or more AesContext.Update calls. This is no longer supported.

Returns byte[]
Start2 overloads
# Start · 1/2
C#
public Error Start(AesContext.Mode mode, byte[] key, byte[] iV = null);

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 AesContext.Mode.CbcEncrypt or AesContext.Mode.CbcDecrypt.

Parameters
NameTypeDefault
modeAesContext.Mode—
keybyte[]—
iVbyte[]null
iV

If the parameter is null, then the default value is Array.Empty<byte>().

Returns Error
# Start · 2/2
C#
public Error Start(AesContext.Mode mode, ReadOnlySpan<byte> key, ReadOnlySpan<byte> iV);

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 AesContext.Mode.CbcEncrypt or AesContext.Mode.CbcDecrypt.

Parameters
NameTypeDefault
modeAesContext.Mode—
keyReadOnlySpan<byte>—
iVReadOnlySpan<byte>—
Returns Error
Update2 overloads
# Update · 1/2
C#
public byte[] Update(byte[] src);

Run the desired operation for this AES context. Will return a byte[] containing the result of encrypting (or decrypting) the given src. See AesContext.Start for mode of operation. Note: The size of src must be a multiple of 16. Apply some padding if needed.

Parameters
NameTypeDefault
srcbyte[]—
Returns byte[]
# Update · 2/2
C#
public byte[] Update(ReadOnlySpan<byte> src);

Run the desired operation for this AES context. Will return a byte[] containing the result of encrypting (or decrypting) the given src. See AesContext.Start for mode of operation. Note: The size of src must be a multiple of 16. Apply some padding if needed.

Parameters
NameTypeDefault
srcReadOnlySpan<byte>—
Returns byte[]

AesContext.Mode

ValueDescription
CbcDecrypt = 3

AES cipher block chaining decryption mode.

CbcEncrypt = 2

AES cipher block chaining encryption mode.

EcbDecrypt = 1

AES electronic codebook decryption mode.

EcbEncrypt = 0

AES electronic codebook encryption mode.

Max = 4

Maximum value for the mode enum.

Reckfell Engine documentationBack to top ↑
QUICK LINKSGetting startedEntity hierarchyC# & .NET 10Scenes and prefabs
Reckfell guides and full API K
reckfellENGINE
RECKFELL ACCOUNT

Sign in

Sign in to your Reckfell account.

One account for the entire Reckfell platform.