/*
 MSJ Kracking Challenge ’10 – Challenge #1 Keygen
  
 This code is for educational/learning purposes. Do not use it for piracy :)
 I'm not responsable whatsoever by any utilization of this code.
  
 (c) fG! 2010 - http://reverse.put.as
 
 Compile with gcc -o keygen keygen.c -lcrypto
 
 Dedicated to all true knowledge seekers ;-)
  
*/

/* standard stuff */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <getopt.h>
/* openssl includes */
#include <openssl/err.h>
#include <openssl/md5.h>

#define VERSION "0.1"

void usage (void);

void usage()
{
	printf(
	"MSJ Kracking Challenge ’10 – Challenge #1 Keygen v%s (c) fG! 2010\n"
	"------------------------------------------------------------------\n", VERSION
	);
}

main(int argc, char *argv[])
{

	int debug = 0;
	unsigned long n;
	// 128bits + 1 byte
	unsigned char md[4*4+1];	
	int i, err;
	char name[256], *pname;

	usage();
 
	// Get input data
	printf("Insert name:\n");
	fflush(stdout);
	fgets(name, 256, stdin);
	if ((pname = strchr(name, '\n')) != NULL)
	{
		*pname = '\0';
	}
	
/* generate MD5 hash for our name
 #include <openssl/md5.h>
 unsigned char *MD5(const unsigned char *d, unsigned long n, unsigned char *md);

 MD2(), MD4(), and MD5() compute the MD2, MD4, and MD5 message digest of the n bytes at d and place it in md (which must have space for
 MD2_DIGEST_LENGTH == MD4_DIGEST_LENGTH == MD5_DIGEST_LENGTH == 16 bytes of output). If md is NULL, the digest is placed in a static array.
 
*/
	MD5(name, strlen(name), md);
/*
	for (i=0;i<16; i++)
	{
	printf("%x", md[i]);	
	}
	printf("\n");
*/	
	/* print the first part which is fixed and corresponded to the plaintext of the hash found in the binary (66EAD6FE7CBE7987B7C4B1A1EED0E5A5)*/
	printf("The valid serial for your name is: KRACK-");
	/* Print the first 3 bytes of the hash*/
	for (i=0;i<=2; i++)
	{
		printf("%X", md[i]);
	}
	/* Only 7 chars of the hash are printed so the 4th byte must be modified*/
	printf("%X", md[3]>>4);
	/* The last 3 chars are fixed*/
	printf("FBC\n");
	printf("\nThat's it! Have fun...\n");
}
