Skip to content

Instantly share code, notes, and snippets.

@esterTion
Last active February 20, 2024 18:49
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save esterTion/26e8500dbec9956fc89eef7daeb97e81 to your computer and use it in GitHub Desktop.
Save esterTion/26e8500dbec9956fc89eef7daeb97e81 to your computer and use it in GitHub Desktop.
using System;
class LightWeightEncryptor
{
private readonly byte[] swizzleBytes; // 0x10
private readonly int Coef; // 0x18
private readonly int Offset; // 0x1C
public LightWeightEncryptor(uint seed, int initSpin, int tableSize)
{
var xorShift = new XorShift();
xorShift.Init(seed);
if (initSpin >= 1)
{
do
{
xorShift.Next();
--initSpin;
} while (initSpin > 0);
}
swizzleBytes = new byte[tableSize];
xorShift.MakeSwizzleTable(swizzleBytes);
Coef = (int)(xorShift.Next() & 0xF) + 3;
Offset = (int)(xorShift.Next() & 0x1F) + 1;
}
public void Modify(byte[] dataBytes, int offset, int count, long streamOffset, int salt)
{
uint v13 = 0;
uint v14 = (uint)swizzleBytes.Length - 1;
do {
byte v18 = dataBytes[offset + v13];
uint v20 = (uint)(Offset + (streamOffset + offset + salt + v13) * Coef) & v14;
if (v20 >= swizzleBytes.Length)
{
throw new IndexOutOfRangeException();
}
dataBytes[offset + v13] = (byte)(swizzleBytes[v20] ^ v18);
++v13;
} while (count != v13);
}
//public byte[] Transform(byte[] inputBytes, int inputOffset, int inputCount) { }
}
class XorShift
{
private readonly object lockObject; // 0x10
private uint seed; // 0x18
private uint y; // 0x1C
public XorShift() { }
public void Init(uint seed) {
this.seed = seed;
y = seed;
}
public uint Next() {
uint v3 = y ^ (y << 13);
uint v4 = v3 ^ (v3 >> 17) ^ (32 * (v3 ^ (v3 >> 17)));
y = v4;
return v4;
}
public void MakeSwizzleTable(byte[] dest) {
if (dest.Length >= 1)
{
uint v4 = 0;
uint v5 = (uint)dest.Length;
do
{
uint result = this.Next();
dest[v4++] = (byte)((uint)result >> 3);
} while (v5 != v4);
}
}
public static void SwizzleBytes(byte[] target, byte[] swizzle, int coef, int offset, int start) { }
//public int Range(int min, int max) { }
//public float NextFloat() { }
}
<?php
class LightWeightEncryptor {
private $swizzleBytes; // byte[] 0x10
private $Coef = 1; // int 0x18
private $offset = 0; // int 0x1c
function __construct($seed, $initSpin, $tableSize)
{
$XorShift = new XorShift();
$XorShift->Init($seed & 0x7fffffff);
if ($initSpin >= 1) {
do {
$XorShift->Next();
--$initSpin;
} while ($initSpin);
}
$this->swizzleBytes = str_repeat(chr(0), $tableSize);
$XorShift->MakeSwizzleTable($this->swizzleBytes);
$this->Coef = ($XorShift->Next() & 0xf) + 3;
$result = $XorShift->Next();
$this->offset = ($result & 0x1f) + 1;
return $this;
}
function Modify(string &$dataBytes, $offset, $count, $streamOffset, $salt) {
$v13 = 0;
$v14 = strlen($this->swizzleBytes) - 1;
do {
$v20 = ($this->offset + ($streamOffset + $offset + $salt + $v13) * $this->Coef) & $v14;
$dataBytes[$offset + $v13] = $this->swizzleBytes[$v20] ^ $dataBytes[$offset + $v13];
++$v13;
} while ($count != $v13);
}
function Transform($inputBytes, $inputOffset, $inputCount) {
throw new BadMethodCallException();
}
}
class RijndaelEncryptor {
private $key;
private $iv;
private $isDecrypt;
function __construct($pw, $salt, bool $isDecrypt)
{
$rijnIter = 117;
$buf = openssl_pbkdf2($pw, $salt, 32, $rijnIter);
$this->key = substr($buf, 0, 16);
$this->iv = substr($buf, 16, 16);
$this->isDecrypt = $isDecrypt;
}
function Modify(string &$dataBytes, $offset, $count, $streamOffset, $salt) {
throw new BadMethodCallException();
}
function Transform($inputBytes, $inputOffset, $inputCount) {
$data = substr($inputBytes, $inputOffset, $inputCount);
if ($this->isDecrypt) {
return openssl_decrypt($data, 'AES-128-CBC', $this->key, OPENSSL_RAW_DATA+OPENSSL_ZERO_PADDING, $this->iv);
} else {
return openssl_encrypt($data, 'AES-128-CBC', $this->key, OPENSSL_RAW_DATA+OPENSSL_ZERO_PADDING, $this->iv);
}
}
}
class XorShift {
private $lockObject; // object 0x10
private $seed; // uint 0x18
private $y; //uint 0x1c
function __construct()
{
return $this;
}
function Init($seed) {
$this->seed = $seed;
$this->y = $seed;
}
function Next() {
$v3 = $this->y ^ (($this->y << 13) & 0xffffffff);
$v4 = $v3 ^ ($v3 >> 17) ^ (32 * ($v3 ^ ($v3 >> 17))) & 0xffffffff;
$this->y = $v4;
return $v4;
}
function MakeSwizzleTable(string &$dest) {
$v5 = strlen($dest);
if ($v5 >= 1) {
$v4 = 0;
do {
$result = $this->Next();
$dest[$v4++] = chr(($result >> 3) & 0xff);
} while ($v5 != $v4);
}
}
static function SwizzleBytes(&$target, $swizzle, $coef, $offset, $start) {
$count = strlen($target);
if ($count >= 1) {
$mask = strlen($swizzle) - 1;
$k = $count + $start;
for ($i = 0; $i < $count; ) {
$k = ($offset + $k * $coef) & 0xffffffff;
$target[$i] = $target[$i] ^ $swizzle[$k & $mask];
$i++;
}
}
}
function Range($min, $max) {
//
}
function NextFloat() {
//
}
}
@chrrox
Copy link

chrrox commented Aug 19, 2021

Nice job.
0x75 CD B2 CF 19 DB 08 3F

0xB5 40 92 3C 7E 45 E8 B2

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