Skip to content

Commit

Permalink
Content decrypt
Browse files Browse the repository at this point in the history
  • Loading branch information
diegolmello committed Sep 18, 2024
1 parent bf57d7a commit 21e322b
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 10 deletions.
7 changes: 7 additions & 0 deletions android/app/src/play/java/chat/rocket/reactnative/Ejson.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ public class Ejson {

String tmid;

Content content;

private MMKV mmkv;

private String TOKEN_KEY = "reactnativemeteor_usertoken-";
Expand Down Expand Up @@ -102,4 +104,9 @@ public class Sender {
String username;
String _id;
}

public class Content {
String ciphertext;
String algorithm;
}
}
40 changes: 30 additions & 10 deletions android/app/src/play/java/chat/rocket/reactnative/Encryption.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,14 @@ class Message {
}
}

class DecryptedContent {
String msg;

DecryptedContent(String msg) {
this.msg = msg;
}
}

class PrivateKey {
String d;
String dp;
Expand Down Expand Up @@ -166,6 +174,15 @@ public String decryptRoomKey(final String e2eKey, final Ejson ejson) throws Exce
return Util.bytesToHex(decoded);
}

private String decryptText(String text, String e2eKey) throws Exception {
String msg = text.substring(12);
byte[] msgData = Base64.decode(msg, Base64.NO_WRAP);
String b64 = Base64.encodeToString(Arrays.copyOfRange(msgData, 16, msgData.length), Base64.DEFAULT);
String decrypted = RCTAes.decrypt(b64, e2eKey, Util.bytesToHex(Arrays.copyOfRange(msgData, 0, 16)));
byte[] data = Base64.decode(decrypted, Base64.NO_WRAP);
return new String(data, "UTF-8");
}

public String decryptMessage(final Ejson ejson, final ReactApplicationContext reactContext) {
try {
this.reactContext = reactContext;
Expand All @@ -179,17 +196,20 @@ public String decryptMessage(final Ejson ejson, final ReactApplicationContext re
return null;
}

String message = ejson.msg;
String msg = message.substring(12);
byte[] msgData = Base64.decode(msg, Base64.NO_WRAP);

String b64 = Base64.encodeToString(Arrays.copyOfRange(msgData, 16, msgData.length), Base64.DEFAULT);

String decrypted = RCTAes.decrypt(b64, e2eKey, Util.bytesToHex(Arrays.copyOfRange(msgData, 0, 16)));
byte[] data = Base64.decode(decrypted, Base64.NO_WRAP);
Message m = gson.fromJson(new String(data, "UTF-8"), Message.class);
if (ejson.msg != null && !ejson.msg.isEmpty()) {
String message = ejson.msg;
String decryptedText = decryptText(message, e2eKey);
Message m = gson.fromJson(decryptedText, Message.class);
return m.text;
} else if (ejson.content != null && "rc.v1.aes-sha2".equals(ejson.content.algorithm)) {
String message = ejson.content.ciphertext;
String decryptedText = decryptText(message, e2eKey);
DecryptedContent m = gson.fromJson(decryptedText, DecryptedContent.class);
return m.msg;
} else {
return null;
}

return m.text;
} catch (Exception e) {
Log.e("[ROCKETCHAT][E2E]", Log.getStackTraceString(e));
}
Expand Down

0 comments on commit 21e322b

Please sign in to comment.