Encrypting long text
Mark Waddingham
mark at livecode.com
Thu Dec 16 11:45:38 EST 2021
On 2021-12-16 16:23, Sean Cole via use-livecode wrote:
> Hi all,
> I'm trying to use RSA to encrypt data from a text field like an address
> or
> notes. When I try to use the encrypt command I get a result 'message
> too
> long'. What is the method for encrypting long or large data?
>
> My current line of code:
> encrypt tData using rsa with public key tMyKey and
> passphrase tMyPass
RSA encryption can only encrypt data up to a certain length (I can't
remember off the top of my head the exact relation, but it is related to
the size of the key) so it isn't designed to be used on arbitrary length
messages.
Encrypting arbitrary length messages is the domain of symmetric
encryption functions - like AES and friends.
The solution, therefore, is to combine the two:
1) Generate a random (using randomBytes()) fixed length encryption key
FixedKey
2) Encrypt the actual data using a symmetric algorithm with FixedKey
as password
2) Use RSA to encrypt the (fixed length!) key FixedKey
4) Make you message the RSA-encrypted FixedKey followed by the
encrypted data
The RSA encrypted FixedKey will be a constant length, and thus you can
just split that off of the combined data, decrypt it using RSA and then
use the result to decrypt the payload.
Hope this helps!
Mark.
--
Mark Waddingham ~ mark at livecode.com ~ http://www.livecode.com/
LiveCode: Everyone can create apps
More information about the use-livecode
mailing list