11package chat .rocket .reactnative .notification ;
22
3- import android .database .Cursor ;
3+ import android .database .Cursor ;
44import android .util .Base64 ;
55import android .util .Log ;
66
1010import com .wix .reactnativenotifications .core .AppLifecycleFacade ;
1111import com .wix .reactnativenotifications .core .AppLifecycleFacadeHolder ;
1212import com .google .gson .Gson ;
13- import com .pedrouid .crypto .RCTAes ;
14- import com .pedrouid .crypto .RCTRsaUtils ;
15- import com .pedrouid .crypto .RSA ;
16- import com .pedrouid .crypto .Util ;
13+ import chat .rocket .mobilecrypto .algorithms .AESCrypto ;
14+ import chat .rocket .mobilecrypto .algorithms .RSACrypto ;
15+ import chat .rocket .mobilecrypto .algorithms .CryptoUtils ;
1716import com .nozbe .watermelondb .WMDatabase ;
1817
19- import java .io .File ;
2018import java .lang .reflect .Field ;
2119import java .security .SecureRandom ;
2220import java .util .Arrays ;
@@ -149,7 +147,7 @@ public String readUserKey(final Ejson ejson) throws Exception {
149147 jwk .putString ("dq" , privKey .dq );
150148 jwk .putString ("qi" , privKey .qi );
151149
152- return new RCTRsaUtils ( reactContext ). jwkToPrivatePkcs1 (jwk );
150+ return RSACrypto . INSTANCE . importJwkKey (jwk );
153151 }
154152
155153 public String decryptRoomKey (final String e2eKey , final Ejson ejson ) throws Exception {
@@ -161,28 +159,29 @@ public String decryptRoomKey(final String e2eKey, final Ejson ejson) throws Exce
161159 return null ;
162160 }
163161
164- RSA rsa = new RSA ();
165- rsa .setPrivateKey (userKey );
166- String decrypted = rsa .decrypt (key );
162+ String decrypted = RSACrypto .INSTANCE .decrypt (key , userKey );
167163
168164 RoomKey roomKey = gson .fromJson (decrypted , RoomKey .class );
169165 byte [] decoded = Base64 .decode (roomKey .k , Base64 .NO_PADDING | Base64 .NO_WRAP | Base64 .URL_SAFE );
170166
171- return Util .bytesToHex (decoded );
167+ return CryptoUtils . INSTANCE .bytesToHex (decoded );
172168 }
173169
174170 private String decryptText (String text , String e2eKey ) throws Exception {
175171 String msg = text .substring (12 );
176172 byte [] msgData = Base64 .decode (msg , Base64 .NO_WRAP );
177173 String b64 = Base64 .encodeToString (Arrays .copyOfRange (msgData , 16 , msgData .length ), Base64 .DEFAULT );
178- String decrypted = RCTAes . decrypt (b64 , e2eKey , Util .bytesToHex (Arrays .copyOfRange (msgData , 0 , 16 )));
174+ String decrypted = AESCrypto . INSTANCE . decryptBase64 (b64 , e2eKey , CryptoUtils . INSTANCE .bytesToHex (Arrays .copyOfRange (msgData , 0 , 16 )));
179175 byte [] data = Base64 .decode (decrypted , Base64 .NO_WRAP );
180176 return new String (data , "UTF-8" );
181177 }
182178
183179 public String decryptMessage (final Ejson ejson , final ReactApplicationContext reactContext ) {
184180 try {
185- this .reactContext = reactContext ;
181+ AppLifecycleFacade facade = AppLifecycleFacadeHolder .get ();
182+ if (facade != null && facade .getRunningReactContext () instanceof ReactApplicationContext ) {
183+ this .reactContext = (ReactApplicationContext ) facade .getRunningReactContext ();
184+ }
186185
187186 Room room = readRoom (ejson );
188187 if (room == null || room .e2eKey == null ) {
@@ -219,12 +218,6 @@ public String encryptMessage(final String message, final String id, final Ejson
219218 AppLifecycleFacade facade = AppLifecycleFacadeHolder .get ();
220219 if (facade != null && facade .getRunningReactContext () instanceof ReactApplicationContext ) {
221220 this .reactContext = (ReactApplicationContext ) facade .getRunningReactContext ();
222- } else {
223- this .reactContext = null ;
224- }
225- if (this .reactContext == null ) {
226- Log .i ("[ROCKETCHAT][E2E]" , "ReactApplicationContext is null, returning unencrypted message" );
227- return message ;
228221 }
229222
230223 Room room = readRoom (ejson );
@@ -244,7 +237,7 @@ public String encryptMessage(final String message, final String id, final Ejson
244237 byte [] bytes = new byte [16 ];
245238 random .nextBytes (bytes );
246239
247- String encrypted = RCTAes . encrypt (Base64 .encodeToString (cypher .getBytes ("UTF-8" ), Base64 .NO_WRAP ), e2eKey , Util .bytesToHex (bytes ));
240+ String encrypted = AESCrypto . INSTANCE . encryptBase64 (Base64 .encodeToString (cypher .getBytes ("UTF-8" ), Base64 .NO_WRAP ), e2eKey , CryptoUtils . INSTANCE .bytesToHex (bytes ));
248241 byte [] data = Base64 .decode (encrypted , Base64 .NO_WRAP );
249242
250243 return keyId + Base64 .encodeToString (concat (bytes , data ), Base64 .NO_WRAP );
0 commit comments