From 567ed059ade5b1ee5ddfcab156684f85d09c265c Mon Sep 17 00:00:00 2001 From: Azalea <22280294+hykilpikonna@users.noreply.github.com> Date: Sat, 25 Oct 2025 04:46:18 +0800 Subject: [PATCH] [-] ByteBufUtil --- .../aqua/sega/aimedb/AimeDbEncryption.kt | 25 ++++++-- .../icu/samnyan/aqua/sega/allnet/AllNet.kt | 4 +- .../samnyan/aqua/sega/util/ByteBufUtil.java | 59 ------------------- 3 files changed, 22 insertions(+), 66 deletions(-) delete mode 100644 src/main/java/icu/samnyan/aqua/sega/util/ByteBufUtil.java diff --git a/src/main/java/icu/samnyan/aqua/sega/aimedb/AimeDbEncryption.kt b/src/main/java/icu/samnyan/aqua/sega/aimedb/AimeDbEncryption.kt index b7f17735..ad8a2bf4 100644 --- a/src/main/java/icu/samnyan/aqua/sega/aimedb/AimeDbEncryption.kt +++ b/src/main/java/icu/samnyan/aqua/sega/aimedb/AimeDbEncryption.kt @@ -1,12 +1,30 @@ package icu.samnyan.aqua.sega.aimedb -import icu.samnyan.aqua.sega.util.ByteBufUtil import io.netty.buffer.ByteBuf import io.netty.buffer.Unpooled.copiedBuffer import java.nio.charset.StandardCharsets import javax.crypto.Cipher import javax.crypto.spec.SecretKeySpec +fun ByteBuf.toBytes(): ByteArray { + val readerPos = readerIndex() + resetReaderIndex() + val result = ByteArray(readableBytes()) + readBytes(result) + readerIndex(readerPos) + return result +} + +fun ByteBuf.toAllBytes(): ByteArray { + val readerPos = readerIndex() + resetReaderIndex() + writerIndex(capacity()) + val result = ByteArray(capacity()) + readBytes(result) + readerIndex(readerPos) + return result +} + /** * @author samnyan (privateamusement@protonmail.com) */ @@ -15,7 +33,6 @@ object AimeDbEncryption { val enc = Cipher.getInstance("AES/ECB/NoPadding").apply { init(Cipher.ENCRYPT_MODE, KEY) } val dec = Cipher.getInstance("AES/ECB/NoPadding").apply { init(Cipher.DECRYPT_MODE, KEY) } - fun decrypt(src: ByteBuf) = copiedBuffer(dec.doFinal(ByteBufUtil.toBytes(src))) - - fun encrypt(src: ByteBuf) = copiedBuffer(enc.doFinal(ByteBufUtil.toAllBytes(src))) + fun decrypt(src: ByteBuf) = copiedBuffer(dec.doFinal(src.toBytes())) + fun encrypt(src: ByteBuf) = copiedBuffer(enc.doFinal(src.toAllBytes())) } diff --git a/src/main/java/icu/samnyan/aqua/sega/allnet/AllNet.kt b/src/main/java/icu/samnyan/aqua/sega/allnet/AllNet.kt index c0c6a6b4..2240772f 100644 --- a/src/main/java/icu/samnyan/aqua/sega/allnet/AllNet.kt +++ b/src/main/java/icu/samnyan/aqua/sega/allnet/AllNet.kt @@ -3,7 +3,6 @@ package icu.samnyan.aqua.sega.allnet import ext.* import icu.samnyan.aqua.net.db.AquaNetUserRepo import icu.samnyan.aqua.sega.allnet.AllNetBillingDecoder.decodeAllNet -import icu.samnyan.aqua.sega.util.AquaConst import jakarta.servlet.http.HttpServletRequest import jakarta.servlet.http.HttpServletResponse import org.springframework.boot.context.properties.ConfigurationProperties @@ -14,7 +13,6 @@ import java.io.InputStream import java.nio.charset.StandardCharsets import java.time.Instant import java.time.LocalDateTime -import java.util.* @Configuration @ConfigurationProperties(prefix = "allnet.server") @@ -84,7 +82,7 @@ class AllNet( logger.info("AllNet /DownloadOrder : $reqMap") - val serial = reqMap["serial"] ?: AquaConst.DEFAULT_KEYCHIP_ID + val serial = reqMap["serial"] ?: "A69E01A8888" val resp = mapOf( "stat" to "1", "serial" to serial diff --git a/src/main/java/icu/samnyan/aqua/sega/util/ByteBufUtil.java b/src/main/java/icu/samnyan/aqua/sega/util/ByteBufUtil.java deleted file mode 100644 index 557e64c6..00000000 --- a/src/main/java/icu/samnyan/aqua/sega/util/ByteBufUtil.java +++ /dev/null @@ -1,59 +0,0 @@ -package icu.samnyan.aqua.sega.util; - -import io.netty.buffer.ByteBuf; - -/** - * @author samnyan (privateamusement@protonmail.com) - */ -public class ByteBufUtil { - - private static final char[] HEX_ARRAY = "0123456789ABCDEF".toCharArray(); - - /** - * Read bytes from start to current writer index - * Not modifying the reader index - * - * @param byteBuf The input buffer - * @return bytes - */ - public static byte[] toBytes(ByteBuf byteBuf) { - - int readerPos = byteBuf.readerIndex(); - byteBuf.resetReaderIndex(); - byte[] result = new byte[byteBuf.readableBytes()]; - byteBuf.readBytes(result); - - byteBuf.readerIndex(readerPos); - return result; - } - - /** - * Move writer index to the capacity and read all the byte to the end - * Not modifying the reader index - * - * @param byteBuf The input buffer - * @return bytes - */ - public static byte[] toAllBytes(ByteBuf byteBuf) { - - int readerPos = byteBuf.readerIndex(); - byteBuf.resetReaderIndex(); - byteBuf.writerIndex(byteBuf.capacity()); - byte[] result = new byte[byteBuf.capacity()]; - byteBuf.readBytes(result); - - byteBuf.readerIndex(readerPos); - return result; - } - - public static String bytesToHex(byte[] bytes) { - char[] hexChars = new char[bytes.length * 2]; - for (int j = 0; j < bytes.length; j++) { - int v = bytes[j] & 0xFF; - hexChars[j * 2] = HEX_ARRAY[v >>> 4]; - hexChars[j * 2 + 1] = HEX_ARRAY[v & 0x0F]; - } - return new String(hexChars); - } - -}