The encoding is broken now

This commit is contained in:
Bruno Rybársky 2024-04-05 15:49:26 +02:00
parent 7d0e773196
commit 22a7d78152

@ -19,57 +19,45 @@ import javax.crypto.NoSuchPaddingException;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.security.*; import java.security.*;
import java.time.Instant; import java.time.Instant;
import java.util.Base64; import java.util.Arrays;
public class SecureChat implements ClientModInitializer { public class SecureChat implements ClientModInitializer {
private PublicKey publicKey; private PublicKey publicKey;
private PrivateKey privateKey; private PrivateKey privateKey;
private Cipher encryptingCipher = null;
private Cipher decryptingCipher = null;
private void initKeys() { private void initKeys() {
KeyPairGenerator kpg = null; KeyPairGenerator kpg;
try { try {
kpg = KeyPairGenerator.getInstance("RSA"); kpg = KeyPairGenerator.getInstance("RSA");
} catch (NoSuchAlgorithmException e) { } catch (NoSuchAlgorithmException e) {
throw new RuntimeException(e); throw new RuntimeException(e);
} }
kpg.initialize(1024); kpg.initialize(512);
KeyPair kp = kpg.genKeyPair(); KeyPair kp = kpg.genKeyPair();
publicKey = kp.getPublic(); publicKey = kp.getPublic();
privateKey = kp.getPrivate(); privateKey = kp.getPrivate();
try {
encryptingCipher = Cipher.getInstance("RSA");
decryptingCipher = Cipher.getInstance("RSA");
} catch (NoSuchAlgorithmException e) {
throw new RuntimeException(e);
} catch (NoSuchPaddingException e) {
throw new RuntimeException(e);
}
try {
encryptingCipher.init(Cipher.ENCRYPT_MODE, publicKey);
decryptingCipher.init(Cipher.DECRYPT_MODE, privateKey);
} catch (InvalidKeyException e) {
throw new RuntimeException(e);
}
} }
private boolean decryptChatMessage(Text message, @Nullable SignedMessage signedMessage, @Nullable GameProfile sender, MessageType.Parameters params, Instant receptionTimestamp) { private boolean decryptChatMessage(Text message, @Nullable SignedMessage signedMessage, @Nullable GameProfile sender, MessageType.Parameters params, Instant receptionTimestamp) {
TranslatableTextContent content = (TranslatableTextContent) message.getContent(); TranslatableTextContent content = (TranslatableTextContent) message.getContent();
String message_content = content.getArg(1).getString(); String message_content = content.getArg(1).getString();
if(message_content.startsWith("BRNCrypt:")){ String player_name = content.getArg(0).getString();
if(message_content.startsWith("®") && message_content.endsWith("®")){
try { try {
String strippedMessage = message_content.replace("BRNCrypt:", ""); String strippedMessage = message_content.replace("®", "");
byte[] decodedMessage = Base64.getDecoder().decode(strippedMessage); byte[] decodedMessage = strippedMessage.getBytes(java.nio.charset.StandardCharsets.UTF_16);
String decryptedMessage = new String(decryptingCipher.doFinal(decodedMessage)); byte[] unpaddedMessage = Arrays.copyOfRange(decodedMessage, 2, decodedMessage.length);
assert sender != null; Cipher decryptingCipher = Cipher.getInstance("RSA");
String outputMessage = "{" + sender.getName() + "} " + decryptedMessage; decryptingCipher.init(Cipher.DECRYPT_MODE, privateKey);
decryptingCipher.update(unpaddedMessage);
String decryptedMessage = new String(decryptingCipher.doFinal());
String outputMessage = "{" + player_name + "} " + decryptedMessage;
MinecraftClient.getInstance().inGameHud.getChatHud().addMessage(Text.of(outputMessage)); MinecraftClient.getInstance().inGameHud.getChatHud().addMessage(Text.of(outputMessage));
return false; return false;
} }
catch (IllegalBlockSizeException | BadPaddingException e){ catch (IllegalBlockSizeException | BadPaddingException | NoSuchAlgorithmException | NoSuchPaddingException |
InvalidKeyException e){
return true; return true;
} }
} }
@ -77,14 +65,18 @@ public class SecureChat implements ClientModInitializer {
} }
private String encryptChatMessage(String message) { private String encryptChatMessage(String message) {
encryptingCipher.update(message.getBytes(StandardCharsets.UTF_8)); String encodedMessage;
String encodedMessage = null;
try { try {
encodedMessage = Base64.getEncoder().withoutPadding().encodeToString(encryptingCipher.doFinal()); Cipher encryptingCipher = Cipher.getInstance("RSA");
} catch (IllegalBlockSizeException | BadPaddingException e) { encryptingCipher.init(Cipher.ENCRYPT_MODE, publicKey);
encryptingCipher.update(message.getBytes(StandardCharsets.UTF_8));
byte[] encryptedMessage = encryptingCipher.doFinal();
encodedMessage = new String(encryptedMessage, java.nio.charset.StandardCharsets.UTF_16);
} catch (IllegalBlockSizeException | BadPaddingException | InvalidKeyException | NoSuchPaddingException |
NoSuchAlgorithmException e) {
throw new RuntimeException(e); throw new RuntimeException(e);
} }
return "BRNCrypt:" + encodedMessage; return '®' + encodedMessage + '®';
} }
@Override @Override