Project Codename: Runge Kutta Source Code

Posted: December 29, 2010 by Jinkchak in Programming


The Source Code is available @ https://secure.filesanywhere.com/fs/v.aspx?v=8a6a68865f6173ab72a2

The instructions to run the program are available here: https://jinkchak.wordpress.com/2010/12/29/project-codename-runge-kutta/


I’m sorry, but I couldn’t resist it – This code is in encrypted form. You need to execute the program to decrypt it. Just download the file ENCRYPT.txt, and paste it in the folder containing the executable program. Run the program, and choose the Decode option. The Source Code will now be available in DECODE.txt. Please note that you won’t be able to compile this code, since my program is not case-sensitive, and data types like FILE, etc., will all be in lowercase and hence, will generate errors.

 

Here’s a brief outline of what this program does. If you ever cared to skim through that mammoth-sized Grimaldi book (for newbies: A textbook on Discrete Mathematics and Combinatorics), you would find a chapter dedicated to some algebraic structures called rings. Skip the first few sections, and you’d find a section titled, “Integer modulo n”.

Now, that’s where you stop and read. You begin to marvel over the magic of Mathematics– especially Discrete Mathematics. After reading through a few pages, you’ll come across something called the “Caesar Shift”, based on the Integer Modulo (which is totally related to the modulus operator ‘%’ in programming).

I won’t tell you what the Caesar Shift is, but you can get more info @ http://en.wikipedia.org/wiki/Caesar_cipher

What I will tell you, however, is that the integer modulo rings 26 and 10 (i.e., mod 26 and mod 10 have been used to encrypt and decrypt alphabets and digits respectively).

The following piece of code encrypts data entered at a console, which can be extended to a file as well

 

ch=getc(fp);

while(ch!=EOF)

{

printf(“\n%c”,ch);

if(isalpha(ch))

{

ench=tolower(ch)-97;

shift=(ench+enkey)%26;

shift=shift+97;

}

else

{

if(isdigit(ch))

{

ench=ch-48;

shift=(ench+enkey)%10;

shift=shift+48;

}

else

{

shift=ch;

}

}

ch=shift;

fprintf(write,”%c”,ch);

ch=getc(fp);

}

 

Looking through this code, you might have noticed the variable enkey. Enkey stores the value of the shift (the amount by which each character is shifted in the cipher). Now, a key is something that locks something up, right? So, the most obvious choice should be a random number.


So, now we come to the function called randomgenerator(). There are many ways to generate a random number, but I told you that we’d be using things we’ve learnt already. Random number generator? Now where have I heard that term before? Ah, yes! In the Digital Electronics Lab – it was called a Pseudo Random bit generator.


We obviously can’t implement the same experiment physically, just for this program. So, the next option is simulation. So, this function simulates the truth table of this generator virtually, shifting data and xoring the first and last bits. The xor function can be implemented using the integer modulo ring 2. The binary(x) function is used as an encoder. An array b[] acts as a temporary shift register. The decode() function converts from binary to decimal.


This generator requires an initial starting point (sort of like ignition). And the best way is to use system time. That’s where the function timing() comes in. The System time is copied into a temporary character array as a string, and only the digits (without the : and the day of the month) are extracted, using suitable statements and converted to floating value variable randnum.


This value now acts as an upper limit for a counter, which depends on randnum, which determines what the random number will be. See the code to get a clear picture. A delayloop is used so that you can see the movement of bits in real time. Remove the delayloop() and you’ll get the result in less than a second(!!).


Now, there’s another way to generate random numbers – Human Input. That’s where the oscillator() function steps in. A sequence of numbers are generated. As soon as the user hits the Enter key, the computation stops, and the number in the variable i at that time is taken to be the random number. We’ve used the concept of interrupts here – yes, the very same one we learnt in CO.


The function that interrupts computation in this case is the kbhit() function, available in the conio.h header file.


So, that’s the whole secret between the generation of enkey. Would you call it a Pseudo Random Number generator or a True Random Number Generator?


Moving on to decryption, you would say that it’s the opposite of encryption. True! But, you forgot one thing. The integer modulo ring 26 is a closed group in the set of positive integers (Z+). So, if you apply it to a negative number, it automatically gets converted to a number that lies within the ring. This is not the case in C. For eg., -2 mod 26 is 24 but in C, -2 % 26 is -2 (which is a negative number) and no ASCII character has a negative number. These conditions are taken care of.


The password() function is based on the escape sequence ‘\b’, the wherex() and wherey() functions that return the abscissa and the ordinate of the cursor’s current position. Wherex() and wherey() are part of the conio.h header file.


The logo of the 21st Century Me…yeah ME! Production Company is based on the equations of a line, the gotoxy(x,y) function in the conio.h header file (where x and y are positive numbers, but the screen resembles the fourth quadrant of the Cartesian system); the delayloop().


Hope you enjoyed my first documentation of a C program!

 

New Built-in Functions used:


kbhit()

wherex()

wherey()

gotoxy(x,y)

clrscr()

time()


Header files


stdio.h

conio.h

math.h

ctype.h

process.h

string.h

time.h


Disclaimer: This program is in no way, perfect. It was made for fun. 🙂


Brought to you by the 21st Century Me…yeah ME! Production Company in association with Jinkchak(TM) : Closing the gap between Human Intelligence and Artificial Intelligence

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s