Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Code obfuscation do not understand

Tags:

c

I encountered the following code

#include <stdio.h>

int main(void)
{
    long long P = 1,E = 2,T = 5,A = 61,L = 251,N = 3659,R = 271173410,G = 1479296389,
              x[] = { G * R * E * E * T , P * L * A * N * E * T };
    puts((char*)x);
    return 0;
}

The case is I do not quite understand how it works,It is very confusing to me. Can someone you please explain this in detail?

edit: One more thing, how to print "Hola mundo!" ("Hello world" in Spanish) analogically?

like image 410
Kevin Avatar asked Dec 20 '22 04:12

Kevin


2 Answers

Oh, this one is fun. Obviously you declare many long long variables, and one long long array of 2 cells. The array is therefore made of 16 bytes.

Given that each byte is one ASCII character, the array represents 16 characters (while the last one is probably zero). You can see that:

G * R * E * E * T = 1479296389 * 271173410 * 2 *2 * 5 = 8022916924116329800 = 
0x6F57206F6C6C6548

P * L * A * N * E * T = 1 * 251 * 61 * 3659 * 2 * 5 = 560229490 = 
0x21646C72

Given that your processor is Little Endian, the array's in-memory representation is:

48 65 6C 6C 6F 20 57 6F 72 6C 64 21 00 00 00 00

Which is Hello World!\x00\x00\x00\x00 in ASCII.

like image 102
Mark Segal Avatar answered Jan 09 '23 11:01

Mark Segal


Here's a Spanish variant:

int main(void)
{
    int T=1, E=2, R=2, A=31, Q=784, L=70684, I=6590711, U=1181881,
        x[] = { T*I*E*R*R*A, Q*U*E, T*A*L };

    puts((char *) x);

    return 0;
}
like image 40
M Oehm Avatar answered Jan 09 '23 12:01

M Oehm