1

I need to encrypt some text in the browser to be stored server side. After a little research I decided to use the SubtleCrypto API. I created the the crypto key using the following code:

window.crypto.subtle
        .generateKey(
            {
                name: "RSA-OAEP",
                modulusLength: 4096,
                publicExponent: new Uint8Array([1, 0, 1]),
                hash: "SHA-256",
            },
            true,
            ["encrypt", "decrypt"]
        )

My initial testing was successful but later I discovered that any text longer than 446 bytes is failing. Which looks like to be expected according to this answer. I am no crypto expert so what should I do to encrypt larger amounts of data (low megabytes). Choose a different algorithm, or split the data in chunks etc?

1 Answers1

4

RSA can not directly encrypt large data. You need to generate a random key for a symmetric cipher (secure random), encrypt the large data with this key using a secure mode (e.g GCM). encrypt the key with your RSA public key, prepend encrypted key to encrypted data. store.

This is just for understanding of what goes on, you should look for a library which does as much of this together as possible from a reputable source, start with libsodium which I believe has a pure JS implementation.

Meir Maor
  • 12,053
  • 1
  • 24
  • 55