Actually, I found that Godot 3.4 has built-in support for AES now. However, I still ran into the same problem with the UTF8. But I found a work around to convert to UTF8 to BASE64 and it seems to work. This example shows two encryption decryption methods. You only need to choose one method.
extends Node2D
onready var aes = AESContext.new()
var data = "The rabbit-hole went straight on like a tunnel for some way, and then dipped suddenly down, so suddenly that Alice had not a moment to think about stopping herself before she found herself falling down a very deep well."
func _ready():
var key = "My secret key!!!"
# Key must be either 16 or 32 bytes.
# Data size must be multiple of 16 bytes, apply padding if needed.
data = Marshalls.utf8_to_base64(data)
while data.length() % 16 != 0:
data += " "
# Encrypt ECB
aes.start(AESContext.MODE_ECB_ENCRYPT, key.to_utf8())
var encrypted = aes.update(data.to_utf8())
aes.finish()
var cipher_text = encrypted.get_string_from_ascii()
print("encrypted = ", cipher_text)
# Decrypt ECB
aes.start(AESContext.MODE_ECB_DECRYPT, key.to_utf8())
var decrypted = aes.update(cipher_text.to_ascii())
aes.finish()
# Check ECB
assert(decrypted == data.to_utf8())
print("decrypted = ", Marshalls.base64_to_utf8(decrypted.get_string_from_utf8()))
var iv = "My secret iv!!!!" # IV must be of exactly 16 bytes.
# Encrypt CBC
aes.start(AESContext.MODE_CBC_ENCRYPT, key.to_utf8(), iv.to_utf8())
encrypted = aes.update(data.to_utf8())
aes.finish()
cipher_text = encrypted.get_string_from_ascii()
print("encrypted = ", cipher_text)
# Decrypt CBC
aes.start(AESContext.MODE_CBC_DECRYPT, key.to_utf8(), iv.to_utf8())
decrypted = aes.update(cipher_text.to_ascii())
aes.finish()
# Check CBC
assert(decrypted == data.to_utf8())
print("decrypted = ", Marshalls.base64_to_utf8(decrypted.get_string_from_utf8()))