AesContext
Provides access to AES encryption/decryption of raw data.
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.
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
public AesContext();Methods6
Finishvoid
public void Finish();Close this AES context so it can be started again. See AesContext.Start.
GetIVStatebyte[] · Deprecated
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.
byte[]Start2 overloads
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.
| Name | Type | Default |
|---|---|---|
mode | AesContext.Mode | — |
key | byte[] | — |
iV | byte[] | null |
iVIf the parameter is null, then the default value is Array.Empty<byte>().
Errorpublic 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.
| Name | Type | Default |
|---|---|---|
mode | AesContext.Mode | — |
key | ReadOnlySpan<byte> | — |
iV | ReadOnlySpan<byte> | — |
ErrorUpdate2 overloads
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.
| Name | Type | Default |
|---|---|---|
src | byte[] | — |
byte[]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.
| Name | Type | Default |
|---|---|---|
src | ReadOnlySpan<byte> | — |
byte[]AesContext.Mode
| Value | Description |
|---|---|
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. |