Given an obfuscated code that generates the map of India, explain its working. The following code when executed generates the map of India.
An obfuscated code is a code that has been deliberately made difficult to understand or difficult to read, typically for the purpose of hiding its logic or making it harder for someone to reverse-engineer it. Obfuscated code is often used to protect proprietary algorithms or intellectual property, or to make it more difficult for attackers to understand and exploit security vulnerabilities in software.
However, obfuscation is not a foolproof way to secure software, as determined attackers can often reverse-engineer the code to understand its logic. Additionally, obfuscation can make code more difficult to maintain and debug, and can also slow down the performance of the code.
#include "stdio.h"
int main()
ding Posts
int a, b, c;
for (b-c-18; a="Hello! Welcome to Geeks ForGeeks.\
TFy!QJu ROo TNn (ROO) SLq SLq ULO+
UHS UJq TNn*RPn/QPbEWS_JSWQAIJO^\
NBELPEHBFHT}TnALVIBLOFAKHFOUFETp\
HCSTHAUFAgcEAelclcn^r^r\\tZvYXXy\
T|S~Pn SPM SOn TNn ULOGULO#ULO-W\
er menu
et
Hq!WFs XDt!" [b+++21]; )
ents 850
dot menu
gs
for (; a-- > 64; )
putchar (++C == 'Z' ? c = c/ 9:33^b&1);
ar Ads Widget
ub menu
et
return 0;
se menu
}
The above code is a typical example of obfuscated code i.e. code that is difficult for humans to understand.
How does it work?
Basically, the string is a run-length encoding of the map of India. Alternating characters in the string store how many times to draw space, and how many times to draw an exclamation mark consecutively. Here is an analysis of the different elements of this program –
The encoded string
"Hello!Welcome to GeeksForGeeks."
"TFy!QJu ROo TNn(ROo)SLq SLq ULo+UHs UJq TNn*RPn/QPbEWS_JSWQAIJO^NBELPeHBFHT}TnALVlBL"
"OFAkHFOuFETpHCStHAUFAgcEAelclcn^r^r\\tZvYxXyT|S~Pn SPm SOn TNn ULo0ULo#ULo-WHq!WFs XDt!";
Notice [b+++21] at the end of the encoded string. As b+++21 is equivalent to (b++ + 21) which will evaluate to 31 (10 + 21), the first 31 characters of this string are ignored and do not contribute to anything. The remaining encoded string contains instructions for drawing the map. The individual characters determine how many spaces or exclamation marks to draw consecutively.
Outer for loop: This loop goes over the characters in the string. Each iteration increases the value of b by one and assigns the next character in the string to a.
Inner for loop: This loop draws individual characters, and a new line whenever it reaches the end of the line. Consider this putchar statement
putchar(++c=='Z' ? c = c/9 : 33^b&1);
As ‘Z’ represents the number 90 in ASCII, 90/9 will give us 10 which is a newline character. Decimal 33 is ASCII for ‘!’. Toggling the low-order bit of 33 gives you 32, which is ASCII for a space. This causes! to be printed if b is odd, and a blank space to be printed if b is even.
Below is a less obfuscated version of the above code:
C++
#include <iostream>
using namespace std;
int main()
{
int a = 10, b = 0, c = 10;
char * str = "TFy!QJu ROo TNn(ROo)SLq SLq ULo+UHs UJq "
"TNn*RPn/QPbEWS_JSWQAIJO^NBELPeHBFHT}TnALVlBL"
"OFAkHFOuFETpHCStHAUFAgcEAelclcn^r^r\\tZvYxXyT|S~Pn SPm "
"SOn TNn ULo0ULo#ULo-WHq!WFs XDt!" ;
while (a != 0)
{
a = str[b++];
while (a-- > 64)
{
if (++c == 90)
{
c = 10;
putchar ( '\n' );
}
else
{
if (b % 2 == 0)
putchar ( '!' );
else
putchar ( ' ' );
}
}
}
return 0;
}
|
C
#include <stdio.h>
int main()
{
int a = 10, b = 0, c = 10;
char * str = "TFy!QJu ROo TNn(ROo)SLq SLq ULo+UHs UJq "
"TNn*RPn/QPbEWS_JSWQAIJO^NBELPeHBFHT}TnALVlBL"
"OFAkHFOuFETpHCStHAUFAgcEAelclcn^r^r\\tZvYxXyT|S~Pn SPm "
"SOn TNn ULo0ULo#ULo-WHq!WFs XDt!" ;
while (a != 0)
{
a = str[b++];
while (a-- > 64)
{
if (++c == 90)
{
c = 10;
putchar ( '\n' );
}
else
{
if (b % 2 == 0)
putchar ( '!' );
else
putchar ( ' ' );
}
}
}
return 0;
}
|
Java
class GFG
{
public static void main(String[] args)
{
int a = 10 , b = 0 , c = 10 ;
String s1= "TFy!QJu ROo TNn(ROo)SLq SLq ULo+UHs UJq TNn*RPn/QP,\n"
+ "bEWS_JSWQAIJO^NBELPeHBFHT}TnALVlBLOFAkHFOuFETpHCStHAUFAgcEAelc,\n"
+ "lcn^r^r\\tZvYxXyT|S~Pn SPm SOn TNn ULo0ULo#ULo-WHq!WFs XDt!" ;
a=s1.charAt(b);
while (a != 0 )
{
if (b < 170 )
{
a = s1.charAt(b);
b++;
while (a-- > 64 )
{
if (++c== 'Z' )
{
c/= 9 ;
System.out.print(( char )(c));
}
else
System.out.print(( char )( 33 ^ (b & 0x01 )));
}
}
else
break ;
}
}
}
|
Python3
a = 10
b = 0
c = 10
s = ( "TFy!QJu ROo TNn(ROo)SLq SLq ULo+UHs"
" UJq TNn*RPn/QPbEWS_JSWQAIJO^NBELPe"
"HBFHT}TnALVlBLOFAkHFOuFETpHCStHAUFA"
"gcEAelclcn^r^r\\tZvYxXyT|S~Pn SPm S"
"On TNn ULo0ULo#ULo-WHq!WFs XDt!" )
a = ord (s[b])
while a ! = 0 :
if b < 170 :
a = ord (s[b])
b + = 1
while a > 64 :
a - = 1
c + = 1
if c = = 90 :
c = c / / 9
print (end = chr (c))
else :
print ( chr ( 33 ^ (b & 0X01 )), end = '')
else :
break
|
C#
using System;
class GFG
{
public static void Main()
{
int a = 10, b = 0, c = 10;
string s1 = "TFy!QJu ROo TNn(ROo)SLq SLq ULo+UHs UJq TNn*RPn/QP,\n"
+ "bEWS_JSWQAIJO^NBELPeHBFHT}TnALVlBLOFAkHFOuFETpHCStHAUFAgcEAelc,\n"
+ "lcn^r^r\\tZvYxXyT|S~Pn SPm SOn TNn ULo0ULo#ULo-WHq!WFs XDt!" ;
a = s1[b];
while (a != 0)
{
if (b < 170)
{
a = s1[b];
b++;
while (a-- > 64)
{
if (++c == 'Z' )
{
c/=9;
Console.Write(( char )(c));
}
else
Console.Write(( char )(33 ^ (b & 0x01)));
}
}
else
break ;
}
}
}
|
PHP
<?php
$a = 10;
$b = 0;
$c = 10;
$s1 = "TFy!QJu ROo TNn(ROo)SLq SLq ULo+UHs UJq " .
"TNn*RPn/QPbEWS_JSWQAIJO^NBELPeHBFHT}TnALVlBL" .
"OFAkHFOuFETpHCStHAUFAgcEAelclcn^r^r\\tZvYxXyT|S~Pn SPm " .
"SOn TNn ULo0ULo#ULo-WHq!WFs XDt!" ;
$a =ord( $s1 [ $b ]);
while ( $a != 0)
{
if ( $b < 170)
{
$a = ord( $s1 [ $b ]);
$b ++;
while ( $a -- > 64)
{
if (++ $c ==90)
{
$c = floor ( $c /9);
echo chr ( $c );
}
else
printf( chr (33 ^ ( $b & 0x01)));
}
}
else
break ;
}
?>
|
Output:

The algorithm implemented by this code is as follows:
The algorithm implemented by this code is as follows:
1.Initialize three variables a, b, and c with values 10, 0, and 10 respectively.
2.Declare a character array str with a string of characters encoded by removing the first 31 characters of the original string.
3.Start a while loop that continues until a is 0.
a. Read the next character in str by incrementing the value of b and assigning the character at the new index of str to a.
b. Start another while loop that continues until a is less than or equal to 64.
i. If c is equal to 90, reset c to 10 and print a newline character.
ii. If c is not equal to 90, check if b is even or odd.
4.If b is even, print an exclamation mark.
5.If b is odd, print a space.
6.Increment c.
7.Decrement a.
8.Return 0.
This algorithm decodes the encoded string in str and draws a map of India using spaces and exclamation marks.
If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks. Please write comments if you find anything incorrect, or if you want to share more information about the topic discussed above.
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!