Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.codec.LengthFieldBasedFrameDecoder;
import io.netty.handler.codec.LengthFieldPrepender;
import java.security.spec.MGF1ParameterSpec;
import javax.crypto.spec.OAEPParameterSpec;
import javax.crypto.spec.PSource;
import net.minecraft.client.Minecraft;
import net.minecraft.client.User;
import net.minecraft.world.level.ChunkPos;
Expand Down Expand Up @@ -267,7 +270,7 @@ void setUpEncryption(ChannelHandlerContext ctx, ClientboundEncryptionRequestPack
encrypt(packet.publicKey, sharedSecret),
encrypt(packet.publicKey, packet.verifyToken)));
} catch (NoSuchAlgorithmException | InvalidKeyException | NoSuchPaddingException | BadPaddingException |
IllegalBlockSizeException e) {
IllegalBlockSizeException | InvalidAlgorithmParameterException e) {
shutDown();
throw new RuntimeException(e);
}
Expand All @@ -283,9 +286,15 @@ void setUpEncryption(ChannelHandlerContext ctx, ClientboundEncryptionRequestPack
}
}

private static byte[] encrypt(PublicKey key, byte[] data) throws NoSuchPaddingException, NoSuchAlgorithmException, BadPaddingException, IllegalBlockSizeException, InvalidKeyException {
Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
cipher.init(Cipher.ENCRYPT_MODE, key);
private static byte[] encrypt(PublicKey key, byte[] data) throws NoSuchPaddingException, NoSuchAlgorithmException, BadPaddingException, IllegalBlockSizeException, InvalidKeyException, InvalidAlgorithmParameterException {
Cipher cipher = Cipher.getInstance("RSA/ECB/OAEPWithSHA-256AndMGF1Padding");
// https://docs.openssl.org/master/man3/RSA_public_encrypt/#description
cipher.init(Cipher.ENCRYPT_MODE, key, new OAEPParameterSpec(
"SHA-256",
"MGF1",
new MGF1ParameterSpec("SHA-256"),
PSource.PSpecified.DEFAULT
));
return cipher.doFinal(data);
}
}
5 changes: 3 additions & 2 deletions server/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export class TcpServer {
server: net.Server;
clients: Record<number, TcpClient> = {};

keyPair = crypto.generateKeyPairSync("rsa", { modulusLength: 1024 });
keyPair = crypto.generateKeyPairSync("rsa", { modulusLength: 2048 });
// precomputed for networking
publicKeyBuffer = this.keyPair.publicKey.export({
type: "spki",
Expand Down Expand Up @@ -45,7 +45,8 @@ export class TcpServer {
return crypto.privateDecrypt(
{
key: this.keyPair.privateKey,
padding: crypto.constants.RSA_PKCS1_PADDING,
padding: crypto.constants.RSA_PKCS1_OAEP_PADDING,
oaepHash: "sha256"
},
buf,
);
Expand Down
Loading