AESContext
Inherits: RefCounted < Object
Description
Section titled “Description”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 Orblit;using System.Diagnostics;
public partial class MyNode : Entity{ private AesContext _aes = new AesContext();
public override void OnStart() { 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()); }}Methods
Section titled “Methods”| Returns | Method |
|---|---|
| void | finish() |
| Error | start(mode: Mode, key: PackedByteArray, iv: PackedByteArray = PackedByteArray()) |
| PackedByteArray | update(src: PackedByteArray) |
Enumerations
Section titled “Enumerations”Mode
enum Mode
MODE_ECB_ENCRYPT = 0
AES electronic codebook encryption mode.
MODE_ECB_DECRYPT = 1
AES electronic codebook decryption mode.
MODE_CBC_ENCRYPT = 2
AES cipher block chaining encryption mode.
MODE_CBC_DECRYPT = 3
AES cipher block chaining decryption mode.
MODE_MAX = 4
Maximum value for the mode enum.
Method descriptions
Section titled “Method descriptions”finish
void finish()
Close this AES context so it can be started again. See start().
start
Error start(mode: Mode, key: PackedByteArray, iv: PackedByteArray = PackedByteArray())
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 MODE_CBC_ENCRYPT or MODE_CBC_DECRYPT.
update
PackedByteArray update(src: PackedByteArray)
Run the desired operation for this AES context. Will return a PackedByteArray containing the result of encrypting (or decrypting) the given src. See start() for mode of operation.
Note: The size of src must be a multiple of 16. Apply some padding if needed.
