/* * Decompiled with CFR 0.152. * * Could not load the following classes: * io.netty.buffer.ByteBuf */ package de.maxhenkel.voicechat.voice.common; import io.netty.buffer.ByteBuf; import java.security.InvalidAlgorithmParameterException; import java.security.InvalidKeyException; import java.security.Key; import java.security.NoSuchAlgorithmException; import java.security.SecureRandom; import java.util.Arrays; import javax.crypto.BadPaddingException; import javax.crypto.Cipher; import javax.crypto.IllegalBlockSizeException; import javax.crypto.NoSuchPaddingException; import javax.crypto.spec.GCMParameterSpec; import javax.crypto.spec.IvParameterSpec; import javax.crypto.spec.SecretKeySpec; public class Secret { public static final int SECRET_SIZE_BYTES = 16; public static final int IV_SIZE_BYTES = 12; public static final int TAG_LEN_BITS = 128; public static final String CIPHER = "AES/GCM/NoPadding"; private static final SecureRandom RANDOM = new SecureRandom(); private final byte[] secret; private final SecretKeySpec keySpec; private volatile boolean legacyProtocol; protected Secret(byte[] byArray) { if (byArray == null || byArray.length != 16) { throw new IllegalArgumentException("Voice chat secret must contain exactly 16 bytes"); } this.secret = (byte[])byArray.clone(); this.keySpec = new SecretKeySpec(this.secret, "AES"); } public static Secret generateNewRandomSecret() { byte[] byArray = new byte[16]; RANDOM.nextBytes(byArray); return new Secret(byArray); } public static Secret fromBytes(byte[] byArray) { return new Secret(byArray); } public static Secret fromBytes(ByteBuf byteBuf) { byte[] byArray = new byte[16]; byteBuf.readBytes(byArray); return Secret.fromBytes(byArray); } public void toBytes(ByteBuf byteBuf) { byteBuf.writeBytes(this.secret); } public byte[] getSecret() { return (byte[])this.secret.clone(); } public SecretKeySpec getKeySpec() { return this.keySpec; } public void setLegacyProtocol(boolean bl) { this.legacyProtocol = bl; } public boolean isLegacyProtocol() { return this.legacyProtocol; } public static byte[] generateIV() { byte[] byArray = new byte[12]; RANDOM.nextBytes(byArray); return byArray; } private static byte[] generateLegacyIV() { byte[] byArray = new byte[16]; RANDOM.nextBytes(byArray); return byArray; } public byte[] encrypt(byte[] byArray) throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidAlgorithmParameterException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException { if (this.legacyProtocol) { byte[] byArray2 = Secret.generateLegacyIV(); Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); cipher.init(1, (Key)this.keySpec, new IvParameterSpec(byArray2)); byte[] byArray3 = cipher.doFinal(byArray); byte[] byArray4 = new byte[byArray2.length + byArray3.length]; System.arraycopy(byArray2, 0, byArray4, 0, byArray2.length); System.arraycopy(byArray3, 0, byArray4, byArray2.length, byArray3.length); return byArray4; } byte[] byArray5 = Secret.generateIV(); Cipher cipher = Cipher.getInstance(CIPHER); cipher.init(1, (Key)this.keySpec, new GCMParameterSpec(128, byArray5)); byte[] byArray6 = cipher.doFinal(byArray); byte[] byArray7 = new byte[byArray5.length + byArray6.length]; System.arraycopy(byArray5, 0, byArray7, 0, byArray5.length); System.arraycopy(byArray6, 0, byArray7, byArray5.length, byArray6.length); return byArray7; } public byte[] decrypt(byte[] byArray) throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidAlgorithmParameterException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException { if (this.legacyProtocol) { if (byArray.length < 32 || (byArray.length - 16) % 16 != 0) { throw new IllegalBlockSizeException("Invalid legacy AES-CBC packet size"); } byte[] byArray2 = Arrays.copyOfRange(byArray, 0, 16); byte[] byArray3 = Arrays.copyOfRange(byArray, 16, byArray.length); Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); cipher.init(2, (Key)this.keySpec, new IvParameterSpec(byArray2)); return cipher.doFinal(byArray3); } if (byArray.length < 28) { throw new IllegalBlockSizeException("Invalid AES-GCM packet size"); } byte[] byArray4 = Arrays.copyOfRange(byArray, 0, 12); byte[] byArray5 = Arrays.copyOfRange(byArray, 12, byArray.length); Cipher cipher = Cipher.getInstance(CIPHER); cipher.init(2, (Key)this.keySpec, new GCMParameterSpec(128, byArray4)); return cipher.doFinal(byArray5); } public boolean equals(Object object) { return object instanceof Secret && Arrays.equals(this.secret, ((Secret)object).secret); } public int hashCode() { return Arrays.hashCode(this.secret); } }