Stream Encryption

E3Kit provides a method for encryption that allows for streaming of data. This is useful when the size of data is too large to be encrypted or decrypted all at once. This guide will cover its usage and limitations.

Encryption and decryption can get slower and heavier as the size of data increases, negatively impacting your application’s performance. To mitigate this, stream encryption is included in E3Kit for iOS and Android. We recommend using stream encryption with files larger than 1mb.

Before you begin

Be sure to implement the following:

Encrypt stream

To encrypt a stream of data, simply pass the input stream into the encryption method along with the recipient(s) public keys.

// Listener for keys lookup OnResultListener<FindUsersResult> lookupKeysListener = new OnResultListener<FindUsersResult>() { @Override public void onSuccess(FindUsersResult result) { byte[] data = "I was text but became byte array".getBytes(); try (ByteArrayInputStream inputStream = new ByteArrayInputStream(data); ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) { // Encrypt input stream using user public keys and writes output to the output stream ethree.authEncrypt(inputStream, inputStream.available(), outputStream, result); } catch (IOException e) { // Handle error } } @Override public void onError(@NotNull Throwable throwable) { } }; // Lookup destination user public keys List<String> identities = new ArrayList<>(3); identities.add("userUID1"); identities.add("userUID2"); identities.add("userUID3"); ethree.findUsers(identities).addCallback(lookupKeysListener);

Decrypt stream

To decrypt a stream of data, simply pass the input stream into the decryption method.

try (ByteArrayInputStream inputStream = new ByteArrayInputStream(encryptedData); ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) { // Decrypt input stream and writes output to the output stream ethree.authDecrypt(inputStream, outputStream); } catch (IOException e) { // Handle error }

Note that it's not necessary to pass the public key of the sender in the decryption method. This is because data signature and verification does not happen in Stream Encryption.

Limitations

Available for Browsers through File API

Since JavaScript does not have a standardized implementation of streams across all supported platforms, stream encryption is not available yet.

If you're using Javascript, you can use the encryptFile method, which encrypts the large data chunk-by-chunk. This method takes an instance of File instead of ArrayBuffer. The large data is encrypted in small chunks, so it doesn't block the main thread, and it returns an encrypted instance of File. The chunk size by default is 64kb which produces the best speed/browser performance ratio, but it can be changed as needed within your application. Larger chunk size speeds up encryption but can cause browser lags. This method supports efficient file encryption of any size for web browsers and Ionic-based apps. To take a look at code-snippets or samples that use the encryptFile method, head over the E3Kit JS GitHub (Browser package) repository and follow the instructions in the README.

No data signature

Since the size of streamed data is not known ahead of time, data signature is not available.