2929import com .facebook .react .bridge .WritableNativeMap ;
3030import 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 ;
3236import java .math .BigInteger ;
3337import java .security .InvalidKeyException ;
3438import java .security .Key ;
4549
4650import javax .crypto .BadPaddingException ;
4751import javax .crypto .Cipher ;
52+ import javax .crypto .CipherInputStream ;
4853import javax .crypto .IllegalBlockSizeException ;
4954import javax .crypto .KeyGenerator ;
5055import 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