Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixup client encryption handling #96

Closed
wants to merge 1 commit into from
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
@@ -1,31 +1,44 @@
package gjum.minecraft.mapsync.common.net.encryption;

import gjum.minecraft.mapsync.common.net.Packet;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToMessageDecoder;

import javax.crypto.Cipher;
import javax.crypto.ShortBufferException;
import javax.crypto.spec.IvParameterSpec;
import java.security.GeneralSecurityException;
import java.security.Key;
import java.util.List;
import javax.crypto.Cipher;
import javax.crypto.ShortBufferException;
import javax.crypto.spec.IvParameterSpec;
import org.apache.commons.lang3.ArrayUtils;
import org.jetbrains.annotations.NotNull;

public class EncryptionDecoder extends MessageToMessageDecoder<ByteBuf> {
private final EncryptionTranslator decryptionCodec;
private final Cipher cipher;

public EncryptionDecoder(Key key) {
public EncryptionDecoder(
final @NotNull Key key
) {
try {
Cipher cipher = Cipher.getInstance("AES/CFB8/NoPadding");
cipher.init(Cipher.DECRYPT_MODE, key, new IvParameterSpec(key.getEncoded()));
decryptionCodec = new EncryptionTranslator(cipher);
} catch (GeneralSecurityException e) {
throw new RuntimeException(e);
this.cipher = Cipher.getInstance("AES/CFB8/NoPadding");
this.cipher.init(Cipher.ENCRYPT_MODE, key, new IvParameterSpec(key.getEncoded()));
}
catch (final GeneralSecurityException thrown) {
throw new IllegalStateException(thrown);
}
}

@Override
protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws ShortBufferException {
out.add(decryptionCodec.decipher(ctx, in));
protected void decode(
final ChannelHandlerContext ctx,
final ByteBuf in,
final List<Object> out
) throws ShortBufferException {
final byte[] receivedBytes = Packet.readByteArrayOfSize(in, in.readableBytes());
final byte[] decryptedBytes = this.cipher.update(receivedBytes);
if (ArrayUtils.getLength(decryptedBytes) > 0) {
out.add(Unpooled.wrappedBuffer(decryptedBytes));
}
}
}
Original file line number Diff line number Diff line change
@@ -1,30 +1,42 @@
package gjum.minecraft.mapsync.common.net.encryption;

import gjum.minecraft.mapsync.common.net.Packet;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToByteEncoder;

import java.security.GeneralSecurityException;
import java.security.Key;
import javax.crypto.Cipher;
import javax.crypto.ShortBufferException;
import javax.crypto.spec.IvParameterSpec;
import java.security.GeneralSecurityException;
import java.security.Key;
import org.apache.commons.lang3.ArrayUtils;
import org.jetbrains.annotations.NotNull;

public class EncryptionEncoder extends MessageToByteEncoder<ByteBuf> {
private final EncryptionTranslator encryptionCodec;
private final Cipher cipher;

public EncryptionEncoder(Key key) {
public EncryptionEncoder(
final @NotNull Key key
) {
try {
Cipher cipher = Cipher.getInstance("AES/CFB8/NoPadding");
cipher.init(Cipher.ENCRYPT_MODE, key, new IvParameterSpec(key.getEncoded()));
encryptionCodec = new EncryptionTranslator(cipher);
} catch (GeneralSecurityException e) {
throw new RuntimeException(e);
this.cipher = Cipher.getInstance("AES/CFB8/NoPadding");
this.cipher.init(Cipher.ENCRYPT_MODE, key, new IvParameterSpec(key.getEncoded()));
}
}
catch (final GeneralSecurityException thrown) {
throw new IllegalStateException(thrown);
}
}

@Override
protected void encode(ChannelHandlerContext ctx, ByteBuf in, ByteBuf out) throws ShortBufferException {
encryptionCodec.encipher(in, out);
protected void encode(
final ChannelHandlerContext ctx,
final ByteBuf in,
final ByteBuf out
) throws ShortBufferException {
final byte[] sendingBytes = Packet.readByteArrayOfSize(in, in.readableBytes());
final byte[] encryptedBytes = this.cipher.update(sendingBytes);
if (ArrayUtils.getLength(encryptedBytes) > 0) {
out.writeBytes(encryptedBytes);
}
}
}

This file was deleted.