Godot Version
4.4.1
Question
I am working on a networking solution that can use the Crypto class to encrypt and decrypt messages after exchanging public key, I have the sending and receiving of public keys working already, but it’s the keys themselves that I am having issues with.
After some testing using the following code:
const key_size : int = 8_192
const message_size : int = 1_013
var private_key : CryptoKey = Crypto.new().generate_rsa(key_size)
var crypto : Crypto = Crypto.new()
## Encrypt a message which is the character 0
## repeated 'message_size' times as a UTF8 buffer
var encrypted : PackedByteArray = crypto.encrypt(private_key, "".lpad(message_size, "0").to_utf8_buffer())
print(crypto.decrypt(private_key, encrypted).get_string_from_utf8())
1,013 characters seems to be the largest amount that can be encrypted using UTF8, as any higher amount of characters results in the error;
Error while encrypting: -16512:
<C++ Source> modules/mbedtls/crypto_mbedtls.cpp:515 @ encrypt()
(Checking the error code in mbedtls error code lookup results Here for “Bad Input Parameters”)
And trying to make the key size any higher then 8,192 (And I checked with the next power of 2, being 16,384, same result so the issue isn’t higher numbers not being a power of 2 I think.) results in the error;
Condition "ret != 0" is true. Returning: nullptr
<C++ Source> modules/mbedtls/crypto_mbedtls.cpp:389 @ generate_rsa()
I am aware that keys can only encrypt up to certain sized messages as said in the Crypto encrypt and decrypt function descriptions, so my question is how can I / is it possible to generate larger keys so I can encrypt and decrypt larger messages?
Thanks.