Sie sind auf Seite 1von 1

The Joy of

Programming
Writing a One-line, Useful and S.G. Ganesh

Obfuscated Program!
In this column, we’ll see an obfuscated code and then discover how to make sense of the program.
You’ll be surprised that this one-line obfuscated program provides a very useful function.

I
n March 2007, we covered the basics of code Step 1: From the rightmost digit, take every even digit and
obfuscation. For those who missed reading it, multiply that digit by 2. If the resulting number is greater
obfuscation is, “The art of concealing the meaning of than 9 (that is, a double digit), add the two digits and
communication by making it more confusing and harder store the result back in that digit’s place. Step 2: Add all
to interpret.” Here is an obfuscated (almost) one-line the digits. Step 3: Check if the last digit of the resulting
program. Can you decipher it and find out what it does? sum is 0 (i.e., is it divisible by 10). If so, the given number
has a valid checksum. Try out an example to see how it
main(int c,char**v){c=0;int n,i=(strlen(v[1])- works or refer to en.wikipedia.org/wiki/Luhn_algorithm
1);while(i>=0){n=v[1][i]-’0’;if(!(i%2))n=(n>4)?(n*2%10)+1: for more details.
n*2;c+=n;i--;}return((c%10)==0);} The following is the de-obfuscated code for this
program:
Okay, it is difficult, so let me help you and explain
what it does. This program checks if your credit card int main(int argc, char**argv) {
number is valid or not! No, I am not kidding, it is true; int argc = 0;
just give your credit card number as the argument to the const char *str = argv[1];
executable and if it returns 1, the given number is valid, for(int i = (strlen(str) -1); i >= 0; i--) {
else it isn’t. Assume that the file name of the program int curr_digit = str[i] - ‘0’;
is obfus.oneline.c. Compile it using your favourite C if((i%2) == 0) { /* Step I */
compiler. Run it and give your credit card number as the curr_digit *= 2;
argument. If the program returns 1, the card number is if(curr_digit > 9)
valid, else the credit card number is fake (invalid). The curr_digit = (curr_digit % 10)
following is an example: + 1;
}
bash-2.05$ cc -w obfus.oneline.c sum += curr_digit; /* Step II */
bash-2.05$ ./a.out 4483591407021598; echo $? }
0 return ((sum % 10) == 0); /* Step III */
bash-2.05$ ./a.out 4483591407021597; echo $? }
1
bash-2.05$ The program is simple and self-explanatory; note that
this program does not have error-checking and makes
The number 4483591407021598 is not a credit card assumptions such as—an argument is always passed to the
number; the number 4483591407021597 is possibly a program, arg is a number, etc. If you can retrace the steps
correct card number. Now, how does this program work? from this program and reduce it to as small as possible,
This program implements the Luhn algorithm for you’ll get the one-line program that does the same thing. I
checking the checksum of a given number. This is the hope you’ll enjoy trying out this program!
algorithm used by credit card and other numbers given
by government organisations for first level validity
checks. This initial check is to weed out any randomly-
S.G. Ganesh is a research engineer at Siemens (Corporate
generated numbers and do further processing on Technology). His latest book is “60 Tips on Object Oriented
numbers that are valid. Programming”, published by Tata McGraw-Hill in December
The algorithm is actually simple. It has three steps. last year. You can reach him at sgganesh@gmail.com.

110 may 2008 | LINUX For You | www.openITis.com

Das könnte Ihnen auch gefallen