Up to date

This page is up to date for Godot 4.1. If you still find outdated information, please open an issue.

AESContext

Inherits: RefCounted < Object

Provides access to AES encryption/decryption of raw data.

Description

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.

GDScriptC#

  1. extends Node
  2. var aes = AESContext.new()
  3. func _ready():
  4. var key = "My secret key!!!" # Key must be either 16 or 32 bytes.
  5. var data = "My secret text!!" # Data size must be multiple of 16 bytes, apply padding if needed.
  6. # Encrypt ECB
  7. aes.start(AESContext.MODE_ECB_ENCRYPT, key.to_utf8_buffer())
  8. var encrypted = aes.update(data.to_utf8_buffer())
  9. aes.finish()
  10. # Decrypt ECB
  11. aes.start(AESContext.MODE_ECB_DECRYPT, key.to_utf8_buffer())
  12. var decrypted = aes.update(encrypted)
  13. aes.finish()
  14. # Check ECB
  15. assert(decrypted == data.to_utf8_buffer())
  16. var iv = "My secret iv!!!!" # IV must be of exactly 16 bytes.
  17. # Encrypt CBC
  18. aes.start(AESContext.MODE_CBC_ENCRYPT, key.to_utf8_buffer(), iv.to_utf8_buffer())
  19. encrypted = aes.update(data.to_utf8_buffer())
  20. aes.finish()
  21. # Decrypt CBC
  22. aes.start(AESContext.MODE_CBC_DECRYPT, key.to_utf8_buffer(), iv.to_utf8_buffer())
  23. decrypted = aes.update(encrypted)
  24. aes.finish()
  25. # Check CBC
  26. assert(decrypted == data.to_utf8_buffer())
  1. using Godot;
  2. using System.Diagnostics;
  3. public partial class MyNode : Node
  4. {
  5. private AesContext _aes = new AesContext();
  6. public override void _Ready()
  7. {
  8. string key = "My secret key!!!"; // Key must be either 16 or 32 bytes.
  9. string data = "My secret text!!"; // Data size must be multiple of 16 bytes, apply padding if needed.
  10. // Encrypt ECB
  11. _aes.Start(AesContext.Mode.EcbEncrypt, key.ToUtf8Buffer());
  12. byte[] encrypted = _aes.Update(data.ToUtf8Buffer());
  13. _aes.Finish();
  14. // Decrypt ECB
  15. _aes.Start(AesContext.Mode.EcbDecrypt, key.ToUtf8Buffer());
  16. byte[] decrypted = _aes.Update(encrypted);
  17. _aes.Finish();
  18. // Check ECB
  19. Debug.Assert(decrypted == data.ToUtf8Buffer());
  20. string iv = "My secret iv!!!!"; // IV must be of exactly 16 bytes.
  21. // Encrypt CBC
  22. _aes.Start(AesContext.Mode.EcbEncrypt, key.ToUtf8Buffer(), iv.ToUtf8Buffer());
  23. encrypted = _aes.Update(data.ToUtf8Buffer());
  24. _aes.Finish();
  25. // Decrypt CBC
  26. _aes.Start(AesContext.Mode.EcbDecrypt, key.ToUtf8Buffer(), iv.ToUtf8Buffer());
  27. decrypted = _aes.Update(encrypted);
  28. _aes.Finish();
  29. // Check CBC
  30. Debug.Assert(decrypted == data.ToUtf8Buffer());
  31. }
  32. }

Methods

void

finish ( )

PackedByteArray

get_iv_state ( )

Error

start ( Mode mode, PackedByteArray key, PackedByteArray iv=PackedByteArray() )

PackedByteArray

update ( PackedByteArray src )


Enumerations

enum Mode:

Mode MODE_ECB_ENCRYPT = 0

AES electronic codebook encryption mode.

Mode MODE_ECB_DECRYPT = 1

AES electronic codebook decryption mode.

Mode MODE_CBC_ENCRYPT = 2

AES cipher blocker chaining encryption mode.

Mode MODE_CBC_DECRYPT = 3

AES cipher blocker chaining decryption mode.

Mode MODE_MAX = 4

Maximum value for the mode enum.


Method Descriptions

void finish ( )

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


PackedByteArray get_iv_state ( )

Get the current IV state for this context (IV gets updated when calling update). You normally don’t need this function.

Note: This function only makes sense when the context is started with MODE_CBC_ENCRYPT or MODE_CBC_DECRYPT.


Error start ( Mode mode, PackedByteArray key, PackedByteArray iv=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.


PackedByteArray update ( PackedByteArray src )

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.