Skip to content

Instantly share code, notes, and snippets.

@Lightczx
Last active November 22, 2023 03:51
Show Gist options
  • Save Lightczx/4a83fbb5ec469c9f420caa86ef37205f to your computer and use it in GitHub Desktop.
Save Lightczx/4a83fbb5ec469c9f420caa86ef37205f to your computer and use it in GitHub Desktop.
using System;
using System.Buffers.Binary;
using System.Text;
namespace GeniusInvokationEncoding;
/// <summary>
/// https://www.bilibili.com/video/av278125720
/// </summary>
internal class Program
{
static unsafe void Main(string[] args)
{
// 51 bytes obfuscated data
byte[] bytes = Convert.FromBase64String("BCHBwxQNAYERyVANCJGBynkOCZER2pgOCrFx8poQChGR9bYQDEGB9rkQDFKRD7oRDeEB");
fixed(byte* ptr = bytes)
{
// Reinterpret as 50 byte actual data and 1 deobfuscate key byte
EncodedData* data = (EncodedData*)ptr;
byte* dataPtr = data->Data;
// Prepare rearranged buffer
RearrangedData rearranged = default;
byte* pFirst = rearranged.First;
byte* pSecond = rearranged.Second;
for (int i = 0; i < 50; i++)
{
// Determine which half are we going to insert
byte** ppTarget = i % 2 == 0 ? &pFirst : &pSecond;
// (actual data = data - key) and save it directly to the target half
**ppTarget = unchecked((byte)(dataPtr[i] - data->DeobfuscateKey));
(*ppTarget)++;
}
// Prepare decoded data result buffer
DecodedData decoded = default;
ushort* pDecoded = decoded.Data;
// reinterpret as DecodeGroup for each 3 bytes
DecodeGroup* pRearranged = (DecodeGroup*)&rearranged;
for (int i = 0; i < 17; i++)
{
(ushort first, ushort second) = pRearranged->GetData();
*pDecoded = first;
*(pDecoded + 1) = second;
pDecoded += 2;
pRearranged++;
}
StringBuilder stringBuilder = new();
for (int i = 0; i < 33; i++)
{
stringBuilder.AppendFormat("{0,3}", decoded.Data[i]).Append(',');
if (i % 3 == 2)
{
stringBuilder.Append('\n');
}
}
Console.WriteLine(stringBuilder.ToString(0, stringBuilder.Length - 1));
}
}
}
public struct EncodedData
{
public unsafe fixed byte Data[50];
public byte DeobfuscateKey;
}
public struct RearrangedData
{
public unsafe fixed byte First[25];
public unsafe fixed byte Second[25];
// Make RearrangedData 51 bytes
// allow to be group as 17 DecodeGroups
public byte padding;
}
public struct DecodeGroup
{
public unsafe fixed byte Data[3];
public unsafe (ushort First, ushort Second) GetData()
{
fixed (byte* ptr = Data)
{
uint value = BinaryPrimitives.ReverseEndianness((*(uint*)ptr) & 0x00FFFFFF) >> 8; // keep low 24 bits only
return ((ushort)((value >> 12) & 0x0FFF), (ushort)(value & 0x0FFF));
}
}
}
public struct DecodedData
{
public unsafe fixed ushort Data[33];
}
@Lightczx
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment