Posts Tagged c

Quotes

Great quotes need recognition. My brother came up with this last night – (and as all good younger brothers do, I stole it :) )

C# ninja by day, Java shodan by night. Coffee with VS, dessert with Eclipse. Anything but Perl baby.

Of course, mine would read -

“C ninja by day, Java shodan by night. Cofee with VS, dessert with NetBeans. This is how I roll. “

Tags: , , ,

No Comments

Threads

I’m starting to like my classes again and mainly because it’s the end of the quarter and I get to work on my mega coding projects. I spent this past weekend cracking a password in MD5 hash. It was a simple dictionary attack but it was fun coding it up. Here’s a code snippet in C# that would return an md5 hash of a string.

// return MD5 hash of a string
public static string MD5(string password)
{
byte[] textBytes = System.Text.Encoding.Default.GetBytes(password);
System.Security.Cryptography.MD5CryptoServiceProvider cryptHandler;
cryptHandler = new System.Security.Cryptography.MD5CryptoServiceProvider();
byte[] hash = cryptHandler.ComputeHash(textBytes);
string ret = “”;
foreach (byte a in hash)
{
ret += a.ToString(“x2″);
}
return ret;
}

I wrote an earlier implementation in Java but I’m really surprised by how much simpler it is in C#. My password cracker was also multi-threaded. Here’s a snippet that spawns worker threads and waits on any one thread to signal a match.

static void Main(string[] args)
{
ResetEvents = new ManualResetEvent[NumThreads];
Console.WriteLine(DateTime.Now + ” ” + DateTime.Now.Millisecond + “ms”);
ReadFile();
for (int thread = 0; thread < NumThreads; thread++)
{
ResetEvents[thread] = new ManualResetEvent(false);
ThreadPool.QueueUserWorkItem(new WaitCallback(CheckList), (object)thread);
}

Console.WriteLine(“Computing and check hash…”);
WaitHandle.WaitAny(ResetEvents);
Console.WriteLine(“Password found!! Press any key to continue…”);
Console.ReadLine();
}

public static void CheckList(object o)
{
int threadID = (int)o;
string current;
string check;
string currentHash;

while (index < Words.Count)
{

….

if (currentHash.CompareTo(PasswordHash) == 0)
{
Console.WriteLine(“You password is : ” + check);
Console.WriteLine(DateTime.Now);
ResetEvents[threadID].Set();

}

}

}

I hope these examples help those of you that are looking for help. :)

Tags: , , , ,

1 Comment

Sockets

TCP sockets and C programming aren’t for the weak of heart. I just came back from 15hour coding run that started Friday afternoon and lasted up until now. It took me about 6 hours to figure out that I had been adding a 1 to a char* while trying to print it out, which caused Firefox to go crazy when it talked to my web server. I hate projects that are due on Friday at 11:59PM. It ruins the whole day, the night and the Saturday as well while I recover from the long coding runs.

It’s 2:30AM (and dinner time!!!..) and I am so far from been done for the weekend that it’s not even funny.

Tags: , , ,

1 Comment