[-] ByteBufUtil

This commit is contained in:
Azalea 2025-10-25 04:46:18 +08:00
parent 6d99beafe9
commit 567ed059ad
3 changed files with 22 additions and 66 deletions

View File

@ -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()))
}

View File

@ -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

View File

@ -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);
}
}