You are on page 1of 4

// KeyGen15.cpp : Defines the entry point for the console application.

//

#include "stdio.h"
#include "stdlib.h"
#include "string.h"
#include "math.h"
#include <ctype.h>

#define MAX_LEN 16
#define MAX_STR 1024

#ifndef WIN32
void strlwr(char *str) {
int n = strlen(str);
for (int i=0; i<n; i++) {
str[i] = tolower(str[i]);
}
}
#endif

class CRC6
{
const unsigned R;
const char W;
const char WBits;
const unsigned Mask;
unsigned* States;

public:
CRC6(char* Key, char* S, bool Encrypt=true, unsigned short Depth=3) :
R((Depth+2)<<2), W(32), WBits(5), Mask(0x1F), States(0)
{
if (!strcmp(S,"")) return;
States = (unsigned*) malloc((unsigned int)(W*(2*R+4)));
if (strlen(Key) > 255) Key[255] = 0;
if (Encrypt)
{
int Length = (strlen(S) + (4 * sizeof(unsigned)) - 1) & ~0xF;
Encode(Key, (unsigned*)S, Length);
}
}

virtual ~CRC6()
{
if (!States) return;
memset(States, rand(), (size_t)(W*(2*R+4)));
free(States);
}

private:
unsigned RotateL(unsigned x, unsigned Bits) {Bits &= Mask; return (x>>(W-
Bits)) | (x<<Bits);}
unsigned RotateR(unsigned x, unsigned Bits) {Bits &= Mask; return (x<<(W-
Bits)) | (x>>Bits);}

void Seed(const char* Key)


{
unsigned c = strlen(Key);
unsigned* L = new unsigned[c+1];
char* ptr = (char*)L;
const char* src = Key;
unsigned i;
for (i = c; i--; *ptr ++= *src++);
unsigned* dst = States;
unsigned LastState = *dst++ = 0xB7E15163;
unsigned iTop = 2 * R + 4;
for (i = iTop; --i; *dst ++= (LastState += 0x9E3779B9));
unsigned A = 0, B = 0, s = 3 * (c > iTop? c: iTop), j = i = 0, jTop
= c / (W>>3);
while (s--)
{
A = States[i] = RotateL(States[i] + A + B, 3);
B = L[j] = RotateL(L[j] + A + B, A + B);
if (++i >= iTop) i = 0;
if (++j >= jTop) j = 0;
}
delete[] L;
}

void Encode(const char* Key, unsigned* ptr, unsigned Length)


{
Seed(Key);
for (unsigned n = 0; n < Length; n += W>>1)
{
unsigned* State = States;
unsigned A = *ptr++;
unsigned B = *ptr++ + *State++;
unsigned C = *ptr++;
unsigned D = *ptr + *State++;
for (unsigned i = R; i--;)
{
unsigned t = RotateL(B*(2*B+1), WBits);
unsigned u = RotateL(D*(2*D+1), WBits);
A = RotateL(A^t, u) + *State++;
C = RotateL(C^u, t) + *State++;
unsigned temp = A; A = B; B = C; C = D; D = temp;
}
*ptr--=D; *ptr--=C+State[1]; *ptr--=B; *ptr=A+*State; ptr+=4;
}
}
};

void Data2ASCIIHex(char* src, char *dest)


{
if (!strcmp(src, "")) return;
for (int i = MAX_LEN; i--;)
{
unsigned char lo = *src++;
unsigned char hi = lo>>4;
lo &= 0x0F;
*dest++ = hi + (hi > 9 ? 'A'-10 : '0');
*dest++ = lo + (lo > 9 ? 'A'-10 : '0');
}
}
void HashString(char* str)
{
char csTemp[MAX_STR];
memset(csTemp, 0, sizeof(csTemp));
int len = strlen(str);
int start = 0;
int pos = 0;
for( int i = 0; i < len; i++ )
{
csTemp[i] = str[pos];
pos += 2;
if( pos >= len ) pos = ++start;
}
strcpy(str, csTemp);
}

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


{
if (argc < 2) {
printf("Cach su dung:\n KeyGen15 <Ten_truong>\nVi du:\n
KeyGen15 \"Truong THCS Nguyen Phong Sac\"\n");
return 0;
}

char Data[MAX_STR];
int i;
memset(Data, 0, sizeof(Data));
strcpy(Data, argv[1]);

strlwr(Data);
HashString(Data);

int L = strlen(Data);
if (L > MAX_LEN)
strcpy(Data, &Data[L-MAX_LEN]);
else
for (i = 0; i < MAX_LEN - L; i++) {
memmove(Data+1, Data, strlen(Data));
Data[0]=' ';
}

char Key[MAX_STR];
strcpy(Key, "violet14");
CRC6(Key, Data);

char Serial[MAX_STR];
memset(Serial, 0, sizeof(Serial));
Data2ASCIIHex(Data, Serial);

char sKey[MAX_STR];
memset(sKey, 0, sizeof(sKey));
int j = 0;
for (i = 0; i < (int)strlen(Serial); i++)
{
sKey[j++] = Serial[i];
if ((i % 4 == 3) && (i < (int)strlen(Serial)- 1)) sKey[j++] = '-';
}
//---------------
strcpy(Serial, sKey);

printf("1.5,%s\n", Serial);
return 0;
}

You might also like