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;
}
String sentSomething = null;
String sentOriginal = null;
String sentEncrypted = null;
public void onPacketRead(PacketEvent.Read e) {
if (e.getPacket() instanceof SPacketChat) {
@ -122,23 +123,32 @@ public class ChatCrypt extends Feature {
message = message.substring(0, message.length() - getTerminator().length());
}
boolean isOurs = message.equals(sentSomething);
sentSomething = null;
boolean isOurs = message.equals(sentEncrypted);
GlobalUtil.LOGGER.info("decrypt: {}", message);
byte[] original = recoverBytes(message);
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
if(Arrays.equals(trypt.encryptChunk(message.getBytes(StandardCharsets.UTF_8), original[0]), original)) {
GlobalUtil.LOGGER.debug("Successfully kept Trypt key up-to-date.");
}
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 {
FieldFinder.findUnmarked(SPacketChat.class, ITextComponent.class, 0).set(e.getPacket(), new TextComponentString("§dChatCrypt> §r" + username + ": " + message));
} catch (IllegalAccessException ex) {
@ -167,13 +177,15 @@ public class ChatCrypt extends Feature {
if(s.startsWith("/"))
return;
sentOriginal = s;
s = encrypt(s);
sentSomething = s;
sentEncrypted = s;
s += getTerminator();
if (s.length() > 256) {
ChatUtil.print("Encrypted message length was too long, couldn't send!");
e.setCancelled(true);
sentSomething = null;
sentEncrypted = null;
sentOriginal = null;
}
((ICPacketChat)e.getPacket()).setMessage(s);
}