Skip to content

Commit

Permalink
Convert Android encrypt/decrypt to use chunking (#301)
Browse files Browse the repository at this point in the history
Fixes #300
  • Loading branch information
geraintwhite committed Jun 27, 2021
1 parent 662b737 commit 495dd7f
Showing 1 changed file with 39 additions and 3 deletions.
42 changes: 39 additions & 3 deletions android/src/main/java/dev/mcodex/RNSensitiveInfoModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@
import com.facebook.react.bridge.WritableNativeMap;
import com.facebook.react.modules.core.DeviceEventManagerModule;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.math.BigInteger;
import java.security.InvalidKeyException;
import java.security.Key;
Expand All @@ -45,6 +49,7 @@

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.CipherInputStream;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
Expand Down Expand Up @@ -662,8 +667,25 @@ public String encrypt(String input) throws Exception {
c = Cipher.getInstance(RSA_ECB);
c.init(Cipher.ENCRYPT_MODE, publicKey);
}
byte[] encodedBytes = c.doFinal(bytes);
String encryptedBase64Encoded = Base64.encodeToString(encodedBytes, Base64.DEFAULT);

int cipherTextSize = 0;
ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
DataOutputStream dataStream = new DataOutputStream(byteStream);
ByteArrayInputStream plaintextStream = new ByteArrayInputStream(bytes);
final int chunkSize = 4*1024;
byte[] buffer = new byte[chunkSize];
while (plaintextStream.available() > chunkSize) {
int readBytes = plaintextStream.read(buffer);
byte[] ciphertextChunk = c.update(buffer, 0, readBytes);
cipherTextSize += ciphertextChunk.length;
dataStream.write(ciphertextChunk);
}
int readBytes = plaintextStream.read(buffer);
byte[] ciphertextChunk= c.doFinal(buffer, 0, readBytes);
cipherTextSize += ciphertextChunk.length;
dataStream.write(ciphertextChunk);

String encryptedBase64Encoded = Base64.encodeToString(byteStream.toByteArray(), Base64.NO_WRAP);
return encryptedBase64Encoded;
}

Expand All @@ -685,7 +707,21 @@ public String decrypt(String encrypted) throws Exception {
c = Cipher.getInstance(RSA_ECB);
c.init(Cipher.DECRYPT_MODE, privateKey);
}
byte[] decodedBytes = c.doFinal(Base64.decode(encrypted, Base64.DEFAULT));

byte[] bytes = Base64.decode(encrypted, Base64.NO_WRAP);
ByteArrayInputStream byteStream = new ByteArrayInputStream(bytes);
DataInputStream dataStream = new DataInputStream(byteStream);

CipherInputStream cipherStream = new CipherInputStream(byteStream, c);
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();

byte[] buffer = new byte[1024];
int len;
while ((len = cipherStream.read(buffer)) != -1) {
outputStream.write(buffer, 0, len);
}

byte[] decodedBytes = outputStream.toByteArray();
return new String(decodedBytes);
}
}

0 comments on commit 495dd7f

Please sign in to comment.