more automatic chatcrypt

This commit is contained in:
Daniella / Tove 2024-05-26 01:55:47 +02:00
parent 5ea7152315
commit 3552b973c3

View file

@ -102,7 +102,8 @@ public class ChatCrypt extends Feature {
useTrypt = !useSBE; useTrypt = !useSBE;
} }
String sentSomething = null; String sentOriginal = null;
String sentEncrypted = null;
public void onPacketRead(PacketEvent.Read e) { public void onPacketRead(PacketEvent.Read e) {
if (e.getPacket() instanceof SPacketChat) { if (e.getPacket() instanceof SPacketChat) {
@ -122,23 +123,32 @@ public class ChatCrypt extends Feature {
message = message.substring(0, message.length() - getTerminator().length()); message = message.substring(0, message.length() - getTerminator().length());
} }
boolean isOurs = message.equals(sentSomething); boolean isOurs = message.equals(sentEncrypted);
sentSomething = null;
GlobalUtil.LOGGER.info("decrypt: {}", message); GlobalUtil.LOGGER.info("decrypt: {}", message);
byte[] original = recoverBytes(message); byte[] original = recoverBytes(message);
message = decrypt(original); message = decrypt(original);
if(!useSBE && keepTrypt && !isOurs) { if(!useSBE && keepTrypt && (!isOurs || !sentOriginal.equals(message))) {
// we must re-encrypt anything we get, unless it is from ourselves, to make sure our key stays up-to-date // we must re-encrypt anything we get, unless it is from ourselves, to make sure our key stays up-to-date
if(Arrays.equals(trypt.encryptChunk(message.getBytes(StandardCharsets.UTF_8), original[0]), original)) { if(Arrays.equals(trypt.encryptChunk(message.getBytes(StandardCharsets.UTF_8), original[0]), original)) {
GlobalUtil.LOGGER.debug("Successfully kept Trypt key up-to-date."); GlobalUtil.LOGGER.debug("Successfully kept Trypt key up-to-date.");
} }
else { else {
HUD.notifyAndPrint("§c§lChat>§c Unable to keep Trypt key up-to-date. You must reset it."); HUD.notifyAndPrint("§c§lChat>§c Unable to keep Trypt key up-to-date. Disabled keep. (Enable and sync by sending CC:keep)");
keepTrypt = false;
} }
} }
sentEncrypted = null;
sentOriginal = null;
if(!useSBE && message.equals("CC:keep")) {
keepTrypt = true;
trypt = null;
HUD.notifyAndPrint("§c§lChat>§c Enabled and synchronized Trypt keep.");
}
try { try {
FieldFinder.findUnmarked(SPacketChat.class, ITextComponent.class, 0).set(e.getPacket(), new TextComponentString("§dChatCrypt> §r" + username + ": " + message)); FieldFinder.findUnmarked(SPacketChat.class, ITextComponent.class, 0).set(e.getPacket(), new TextComponentString("§dChatCrypt> §r" + username + ": " + message));
} catch (IllegalAccessException ex) { } catch (IllegalAccessException ex) {
@ -167,13 +177,15 @@ public class ChatCrypt extends Feature {
if(s.startsWith("/")) if(s.startsWith("/"))
return; return;
sentOriginal = s;
s = encrypt(s); s = encrypt(s);
sentSomething = s; sentEncrypted = s;
s += getTerminator(); s += getTerminator();
if (s.length() > 256) { if (s.length() > 256) {
ChatUtil.print("Encrypted message length was too long, couldn't send!"); ChatUtil.print("Encrypted message length was too long, couldn't send!");
e.setCancelled(true); e.setCancelled(true);
sentSomething = null; sentEncrypted = null;
sentOriginal = null;
} }
((ICPacketChat)e.getPacket()).setMessage(s); ((ICPacketChat)e.getPacket()).setMessage(s);
} }