⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 keygendlg.cpp

📁 一种基于keygen的加密算法
💻 CPP
字号:
// KeyGenDlg.cpp : implementation file
//

#include "stdafx.h"
#include "KeyGen.h"
#include "KeyGenDlg.h"

#include "aes.h"
#include "sha2.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CKeyGenDlg dialog

CKeyGenDlg::CKeyGenDlg(CWnd* pParent /*=NULL*/)
: CDialog(CKeyGenDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CKeyGenDlg)
	// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CKeyGenDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CKeyGenDlg)
	// NOTE: the ClassWizard will add DDX and DDV calls here
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CKeyGenDlg, CDialog)
//{{AFX_MSG_MAP(CKeyGenDlg)
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_GENERATE, OnGenerate)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CKeyGenDlg message handlers

BOOL CKeyGenDlg::OnInitDialog()
{
	CDialog::OnInitDialog();
	
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon
	
	// TODO: Add extra initialization here
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CKeyGenDlg::OnPaint() 
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting
		
		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
		
		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;
		
		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

HCURSOR CKeyGenDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}


void bytes2str( const unsigned char in[], unsigned int len, unsigned char *out )
{
	unsigned int i, x, y;
	for( i=0; i<len; i++)
	{
		y = in[i] >> 4;
		y += 0x30;
		if( y > 0x39 )  y += 0x07;
		x = in[i] & 0x0F;
		x += 0x30;
		if( x > 0x39 )  x += 0x07;
		out[ i << 1 ] = y;
		out[ (i << 1) + 1 ] = x;
	}
	out[ len << 1 ] = 0x00;
}

void tea_decrypt( unsigned char *data, unsigned char *key, unsigned char *out )
{
	int i,j;
	unsigned int y=0,z=0,a,b,c,d;
	unsigned int sum = 0x2bed3a00;
	unsigned delta = 0xb8afb4e8;
	unsigned int k[4] = {0};

	for (i=0; i<4; i++)
	{
		for(j=3; j>-1; j--)
		{
			k[i] |= key[i*4+j];
			if (j!=0)
				k[i] <<= 8;
		}
	}

	for (i=3; i>-1; i--)
	{
		y |= data[i];
		z |= data[i+4];
		if (i!=0)
		{
			y <<= 8;
			z <<= 8;
		}
	}

	a = k[0]; b =k[1]; c = k[2]; d = k[3];

	for(i=0; i<64; i++)
	{
		z -= (y<<4) + c^y + sum^(y>>5) + d;
		y -= (z<<4) + a^z + sum^(z>>5) + b;
		sum -= delta;	
	}

	for (i=0; i<4; i++)
	{
		out[i] = (y >> (8*i)) & 0xFF;
		out[i+4] = (z >> (8*i)) & 0xFF;
	}
}

typedef struct RC4_key_st
{
	unsigned char S[256];
} RC4_KEY;

void rc4_set_key( RC4_KEY *rc4_key, unsigned char *key, unsigned int len )
{
	unsigned char  K[256];
	
	unsigned int  i=0, j=0;
	for( i=0; i<256; i++ )	K[i] = key[ i%len ];
	for( i=0; i<256; i++ )  rc4_key->S[i] = i;
	for( i=0; i<256; i++ )
	{
		j = ( j + rc4_key->S[i] + K[i] ) % 256;

		unsigned char  c = rc4_key->S[i];
		rc4_key->S[i] = rc4_key->S[j];
		rc4_key->S[j] = c;
	}
}

void rc4( RC4_KEY *rc4_key, unsigned char *in, unsigned int len, unsigned char *out )
{
	unsigned int  i=0, j=0, k;
	for( unsigned int n=0; n<len; n++ )
	{
		i = ( i + 1 ) % 256;
		j = ( j + rc4_key->S[i] ) % 256;

		unsigned char  c = rc4_key->S[i];
		rc4_key->S[i] = rc4_key->S[j];
		rc4_key->S[j] = c;

		k = ( rc4_key->S[i] + rc4_key->S[j] ) % 256;
		c = rc4_key->S[k];
		out[n] = in[n] ^ c;	
	}
}

typedef unsigned long   u4byte;

#define rotr(x,n)   (((x) >> ((int)(n))) | ((x) << (32 - (int)(n))))
#define rotl(x,n)   (((x) << ((int)(n))) | ((x) >> (32 - (int)(n))))

#define f_rnd(i,a,b,c,d)                   \
        u = rotl(d * (d + d + 1), 5);      \
        t = rotl(b * (b + b + 1), 5);      \
        a = rotl(a ^ t, u)  + l_key[i];    \
        c = rotl(c ^ u , t)  + l_key[i + 1] 

#define i_rnd(i,a,b,c,d)                   \
        u = rotl(d * (d + d + 1), 5);      \
        t = rotl(b * (b + b + 1), 5);      \
        c = rotr(c - l_key[i + 1], t) ^ u; \
        a = rotr(a - l_key[i], u) ^ t      

u4byte  l_key[44];  

u4byte * rc6_set_key(const u4byte in_key[], const u4byte key_len)
{   
	u4byte  i, j, k, a, b, l[8], t;

    l_key[0] = 0xb8afb4e8;      //这里是修改过的地方
	//l_key[0] = 0xb7e15163;

    for(k = 1; k < 44; ++k)
	{
        l_key[k] = l_key[k - 1] - 0x61C86847;     //这里是修改过的地方
		//l_key[k] = l_key[k - 1] + 0x9e3779b9;
	}

    for(k = 0; k < key_len/32; ++k)
        l[k] = in_key[k];

    t = (key_len/32) - 1;
    a = b = i = j = 0;

    for(k = 0; k < 132; ++k)
    {   a = rotl(l_key[i] + a + b, 3); b += a;
        b = rotl(l[j] + b, b);
        l_key[i] = a; l[j] = b;
        i = (i == 43 ? 0 : i + 1);
        j = (j == t ? 0 : j + 1);
    }

    return l_key;
};

void rc6_decrypt(const u4byte in_blk[4], u4byte out_blk[4])
{   
	u4byte  a,b,c,d,t,u;

    d = in_blk[3]; c = in_blk[2] - l_key[43]; 
    b = in_blk[1]; a = in_blk[0] - l_key[42];

    //i_rnd(40,d,a,b,c); i_rnd(38,c,d,a,b);    //这里是修改过的地方
    //i_rnd(36,b,c,d,a); i_rnd(34,a,b,c,d);    //这里是修改过的地方
    i_rnd(32,d,a,b,c); i_rnd(30,c,d,a,b);
    i_rnd(28,b,c,d,a); i_rnd(26,a,b,c,d);
    i_rnd(24,d,a,b,c); i_rnd(22,c,d,a,b);
    i_rnd(20,b,c,d,a); i_rnd(18,a,b,c,d);
    i_rnd(16,d,a,b,c); i_rnd(14,c,d,a,b);
    i_rnd(12,b,c,d,a); i_rnd(10,a,b,c,d);
    i_rnd( 8,d,a,b,c); i_rnd( 6,c,d,a,b);
    i_rnd( 4,b,c,d,a); i_rnd( 2,a,b,c,d);

    out_blk[3] = d - l_key[1]; out_blk[2] = c; 
    out_blk[1] = b - l_key[0]; out_blk[0] = a; 
};

void CKeyGenDlg::OnGenerate() 
{
	char szName[128] = {0};
	char szSerial[256] = {0};

	int  len;
	len = GetDlgItemText( IDC_NAME, szName, 100 );
	if( len < 3 )
	{
		SetDlgItemText( IDC_SERIAL, "Name must be 3 chars at least ..." );
		return;
	}

	unsigned char key[] = {
		  0xBA, 0x78, 0x16, 0xBF, 0x8F, 0x01, 0xCF, 0xEA,
		  0x41, 0x41, 0x40, 0xDE, 0x5D, 0xAE, 0x22, 0x23,
		  0xB0, 0x03, 0x61, 0xA3, 0x96, 0x17, 0x7A, 0x9C,
		  0xB4, 0x10, 0xFF, 0x61, 0xF2, 0x00, 0x15, 0xAD,
		  0x24, 0x8D, 0x6A, 0x61, 0xD2, 0x06, 0x38, 0xB8,
		  0xE5, 0xC0, 0x26, 0x93, 0x0C, 0x3E, 0x60, 0x39,
		  0xA3, 0x3C, 0xE4, 0x59, 0x64, 0xFF, 0x21, 0x67,
		  0xF6, 0xEC, 0xED, 0xD4, 0x19, 0xDB, 0x06, 0xC1
	};

	int  i;
	char szTemp[256] = {0};
	char szBuf1[256] = {0};
	char szBuf2[256] = {0};
	char szBuf3[256] = {0};
	char szBuf4[256] = {0};

	len = strlen( szName );
	sha2_hmac( key, 32, (unsigned char *)szName, len, (unsigned char *)szBuf1 );

	RC4_KEY  rc4_key;
	rc4_set_key( &rc4_key, (unsigned char *)szBuf1, 32 );
	rc4( &rc4_key, key, 64, (unsigned char *)szBuf2 );

	for( i=0; i<4; i++ )
		tea_decrypt( (unsigned char *)(szBuf1+8*i), (unsigned char *)(szBuf2+16*i), (unsigned char *)(szBuf3+8*i) );

	__int64  R, S, T;
	R = 0;
	S = 0;
	for( i=0; i<16; i++ )
	{
		R += (unsigned int)szBuf1[i*2];
		S += (unsigned int)szBuf1[i*2+1];;
	}
	T = (S-0x1A)*0xb7c3a5e6 + (R+0x2B)*0x37f506de;
	unsigned char  iv[20];
	sprintf( (char *)iv, "%8x", T );
	sprintf( (char *)(iv+8), "%8x", T>>32 );

	aes_context  aes_ctx;
	aes_set_key( &aes_ctx, (unsigned char *)szBuf2, 256 );
	aes_cbc_decrypt( &aes_ctx, iv, (unsigned char *)szBuf3, (unsigned char *)szBuf4, 32 );

	rc6_set_key( (unsigned long *)szBuf2, 256 );
	for( i=0; i<2; i++ )
		rc6_decrypt( (u4byte*)(szBuf4+16*i), (u4byte*)(szTemp+16*i) );

	bytes2str( (unsigned char *)szTemp, 32, (unsigned char *)szSerial );
	SetDlgItemText( IDC_SERIAL, szSerial );
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -