Skip to content

Commit 495dd7f

Browse files
authored
Convert Android encrypt/decrypt to use chunking (mCodex#301)
Fixes mCodex#300
1 parent 662b737 commit 495dd7f

File tree

1 file changed

+39
-3
lines changed

1 file changed

+39
-3
lines changed

android/src/main/java/dev/mcodex/RNSensitiveInfoModule.java

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@
2929
import com.facebook.react.bridge.WritableNativeMap;
3030
import com.facebook.react.modules.core.DeviceEventManagerModule;
3131

32+
import java.io.ByteArrayInputStream;
33+
import java.io.ByteArrayOutputStream;
34+
import java.io.DataInputStream;
35+
import java.io.DataOutputStream;
3236
import java.math.BigInteger;
3337
import java.security.InvalidKeyException;
3438
import java.security.Key;
@@ -45,6 +49,7 @@
4549

4650
import javax.crypto.BadPaddingException;
4751
import javax.crypto.Cipher;
52+
import javax.crypto.CipherInputStream;
4853
import javax.crypto.IllegalBlockSizeException;
4954
import javax.crypto.KeyGenerator;
5055
import javax.crypto.SecretKey;
@@ -662,8 +667,25 @@ public String encrypt(String input) throws Exception {
662667
c = Cipher.getInstance(RSA_ECB);
663668
c.init(Cipher.ENCRYPT_MODE, publicKey);
664669
}
665-
byte[] encodedBytes = c.doFinal(bytes);
666-
String encryptedBase64Encoded = Base64.encodeToString(encodedBytes, Base64.DEFAULT);
670+
671+
int cipherTextSize = 0;
672+
ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
673+
DataOutputStream dataStream = new DataOutputStream(byteStream);
674+
ByteArrayInputStream plaintextStream = new ByteArrayInputStream(bytes);
675+
final int chunkSize = 4*1024;
676+
byte[] buffer = new byte[chunkSize];
677+
while (plaintextStream.available() > chunkSize) {
678+
int readBytes = plaintextStream.read(buffer);
679+
byte[] ciphertextChunk = c.update(buffer, 0, readBytes);
680+
cipherTextSize += ciphertextChunk.length;
681+
dataStream.write(ciphertextChunk);
682+
}
683+
int readBytes = plaintextStream.read(buffer);
684+
byte[] ciphertextChunk= c.doFinal(buffer, 0, readBytes);
685+
cipherTextSize += ciphertextChunk.length;
686+
dataStream.write(ciphertextChunk);
687+
688+
String encryptedBase64Encoded = Base64.encodeToString(byteStream.toByteArray(), Base64.NO_WRAP);
667689
return encryptedBase64Encoded;
668690
}
669691

@@ -685,7 +707,21 @@ public String decrypt(String encrypted) throws Exception {
685707
c = Cipher.getInstance(RSA_ECB);
686708
c.init(Cipher.DECRYPT_MODE, privateKey);
687709
}
688-
byte[] decodedBytes = c.doFinal(Base64.decode(encrypted, Base64.DEFAULT));
710+
711+
byte[] bytes = Base64.decode(encrypted, Base64.NO_WRAP);
712+
ByteArrayInputStream byteStream = new ByteArrayInputStream(bytes);
713+
DataInputStream dataStream = new DataInputStream(byteStream);
714+
715+
CipherInputStream cipherStream = new CipherInputStream(byteStream, c);
716+
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
717+
718+
byte[] buffer = new byte[1024];
719+
int len;
720+
while ((len = cipherStream.read(buffer)) != -1) {
721+
outputStream.write(buffer, 0, len);
722+
}
723+
724+
byte[] decodedBytes = outputStream.toByteArray();
689725
return new String(decodedBytes);
690726
}
691727
}

0 commit comments

Comments
 (0)