4

I want to encrypt a large file (100 MB) with the a public-private-key method in libsodium. For small messages, I use crypto_box_easy(), but this does not work for large files. What is the best approach to use crypto_box_easy() for files? I cannot use a secret key, as I cannot to exchange the key securely and hence need to have sender and receiver a public and a private key.

What I thought about was to split the file up in small chunks (1 MB each), encrypt them and merge them into a large file, then split this up again and decrypt each chunk again with crypto_box_open_easy(). Is this a valid and - most important - a secure approach? Or are there other ways to encrypt a file in libsodium with public-private-keys?

Thanks!

Chris R.
  • 415
  • 2
  • 5
  • 15

1 Answers1

6

Since crypto_box_easy() seems to be limited to in-memory data and the file size is to large you need to perform the hybrid encryption yourself.

This amounts to creating a random symmetric key and encrypt this key with asymmetric encryption. Then with the symmetric key using crypto_secretstream... encrypt the data. Then combine the encrypted key and the encrypted data.

See Hybrid Encryption for more details.

zaph
  • 111,848
  • 21
  • 189
  • 228
  • Sounds like a good approach. But is this as safe as the asymmetric encryption in terms of security? – Chris R. Aug 30 '18 at 09:26
  • 2
    Yes Hybrid Encryption is secure, this is how TLS (SSL/HTTPS) encrypt data. This is essentially what crypto_box_easy is doing. The data size that can be encrypted with asymmetric encryption is limited to the key size. Generally asymmetric key size is equal to or less than 4096-bits (512-bytes) for RSA and 512-bits (64-bytes) for EC. So you can see that asymmetric encryption can not be used for data of any size and the solution is Hybrid Encryption. Ion general asymmetric encryption us used to encrypt keys and symmetric encryption to encrypt data. – zaph Aug 30 '18 at 11:18
  • 1
    Symmetric encryption such as AES with a key size of 128-bits or greater is secure for data in transit and data at rest. This essentially equals 3072-bit RSA and 256-bit ECC see [NIST Recommended Key Sizes](https://www.globalsign.com/en/blog/elliptic-curve-cryptography/). Also AES is quantum safe at 256-bits while RSA and ECC are not quantum safe (if that ever really becomes a problem). – zaph Aug 30 '18 at 11:31
  • Thanks a lot for your detailed answer! – Chris R. Aug 30 '18 at 13:42