TG-PlatformPlus/scripts/codec_加密算法1.pys

57 lines
1.2 KiB
Plaintext
Raw Normal View History

2026-03-02 14:29:58 +08:00
def encode(data):
ret = bytearray()
if not data:
return ret
l = len(data)
if l % 2 != 0:
return ret
for i in range(0, l, 2):
usTmp = data[i] << 8
usTmp += data[i + 1]
ucByte = ((usTmp >> 12) & 0xff)
if i >= l - 2:
ucByte |= ((1 << 5) & 0xff)
if i < 2:
ucByte |= ((1 << 6) & 0xff)
a = ((2 << 6) & 0xff)
a += ((usTmp >> 6) & 0x3f)
b = ((3 << 6) & 0xff)
b += (usTmp & 0x3f)
ret.append(ucByte)
ret.append(a)
ret.append(b)
return ret
def encodeLen(dataLen):
return (int)(dataLen / 2) * 3
def decode(data):
ret = bytearray()
if not data:
return ret
l = len(data)
if l % 3 != 0:
return ret
for i in range(0, l, 3):
val = data[i]
usTmp = (val & 0x0f)
usTmp <<= 6
val = data[i + 1]
a = (val & 0x3f)
usTmp += a
usTmp <<= 6
val = data[i + 2]
b = (val & 0x3f)
usTmp += b
a = (usTmp >> 8) & 0xff
b = (usTmp & 0xff)
ret.append(a)
ret.append(b)
return ret
def decodeLen(dataLen):
return int(dataLen / 3) * 2