The encoding is broken now
This commit is contained in:
parent
7d0e773196
commit
22a7d78152
@ -19,57 +19,45 @@ import javax.crypto.NoSuchPaddingException;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.security.*;
|
||||
import java.time.Instant;
|
||||
import java.util.Base64;
|
||||
import java.util.Arrays;
|
||||
|
||||
public class SecureChat implements ClientModInitializer {
|
||||
|
||||
private PublicKey publicKey;
|
||||
private PrivateKey privateKey;
|
||||
private Cipher encryptingCipher = null;
|
||||
private Cipher decryptingCipher = null;
|
||||
|
||||
private void initKeys() {
|
||||
KeyPairGenerator kpg = null;
|
||||
KeyPairGenerator kpg;
|
||||
try {
|
||||
kpg = KeyPairGenerator.getInstance("RSA");
|
||||
} catch (NoSuchAlgorithmException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
kpg.initialize(1024);
|
||||
kpg.initialize(512);
|
||||
KeyPair kp = kpg.genKeyPair();
|
||||
publicKey = kp.getPublic();
|
||||
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) {
|
||||
TranslatableTextContent content = (TranslatableTextContent) message.getContent();
|
||||
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 {
|
||||
String strippedMessage = message_content.replace("BRNCrypt:", "");
|
||||
byte[] decodedMessage = Base64.getDecoder().decode(strippedMessage);
|
||||
String decryptedMessage = new String(decryptingCipher.doFinal(decodedMessage));
|
||||
assert sender != null;
|
||||
String outputMessage = "{" + sender.getName() + "} " + decryptedMessage;
|
||||
String strippedMessage = message_content.replace("®", "");
|
||||
byte[] decodedMessage = strippedMessage.getBytes(java.nio.charset.StandardCharsets.UTF_16);
|
||||
byte[] unpaddedMessage = Arrays.copyOfRange(decodedMessage, 2, decodedMessage.length);
|
||||
Cipher decryptingCipher = Cipher.getInstance("RSA");
|
||||
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));
|
||||
return false;
|
||||
}
|
||||
catch (IllegalBlockSizeException | BadPaddingException e){
|
||||
catch (IllegalBlockSizeException | BadPaddingException | NoSuchAlgorithmException | NoSuchPaddingException |
|
||||
InvalidKeyException e){
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@ -77,14 +65,18 @@ public class SecureChat implements ClientModInitializer {
|
||||
}
|
||||
|
||||
private String encryptChatMessage(String message) {
|
||||
encryptingCipher.update(message.getBytes(StandardCharsets.UTF_8));
|
||||
String encodedMessage = null;
|
||||
String encodedMessage;
|
||||
try {
|
||||
encodedMessage = Base64.getEncoder().withoutPadding().encodeToString(encryptingCipher.doFinal());
|
||||
} catch (IllegalBlockSizeException | BadPaddingException e) {
|
||||
Cipher encryptingCipher = Cipher.getInstance("RSA");
|
||||
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);
|
||||
}
|
||||
return "BRNCrypt:" + encodedMessage;
|
||||
return '®' + encodedMessage + '®';
|
||||
}
|
||||
|
||||
@Override
|
||||
|
Loading…
Reference in New Issue
Block a user