SDK(C)应用CRC32算法实现简单的软件注册机制。。。

发表于:2007-07-01来源:作者:点击数: 标签:
#include "s td afx.h" #include "resource.h" #include "stdlib.h" #include "string.h" HINSTANCE hInst; HICONhIcon; HWND hBtn; unsigned long Crc32Table[256]; //定义窗口消息处理过程 BOOL APIENTRY CrackMe1DlgProc(HWND hDlg,UINT message,WPARAM w


#include "stdafx.h"
#include "resource.h"
#include "stdlib.h"
#include "string.h"

HINSTANCE hInst;
HICON hIcon;
HWND  hBtn;
unsigned long Crc32Table[256];
//定义窗口消息处理过程
BOOL APIENTRY CrackMe1DlgProc(HWND hDlg,UINT message,WPARAM wParam,LPARAM lParam);
void MakeTable();
int GetCrc32(PCHAR csData, DWORD dwSize);

int APIENTRY WinMain(HINSTANCE hInstance,
                     HINSTANCE hPrevInstance,
                     LPSTR     lpCmdLine,
                     int       nCmdShow)
{
    hInst=GetModuleHandle(0);
 //显示对话框
 DialogBoxParam(hInst,MAKEINTRESOURCE(IDD_DIALOG1),0,(DLGPROC)CrackMe1DlgProc,0);
 return(TRUE);
}

void MakeTable()//动态生成CRC32的码表
{
   int i,j;
   unsigned long crc;
   for (i = 0; i < 256; i++)
    {
        crc = i;
        for (j = 0; j < 8; j++)
        {
            if (crc & 1)
                crc = (crc >> 1) ^ 0xEDB88320;
            else
                crc >>= 1;
        }
        Crc32Table[i] = crc;
    }
}

int GetCrc32(PCHAR csData, DWORD dwSize)//获取crc32值
{
 ULONG  crc(0xffffffff);
 int len;
 unsigned char* buffer;
 len = dwSize;
 buffer = (unsigned char*)(LPCTSTR)csData;
 while(len--)
  crc = (crc >> 8) ^ Crc32Table[(crc & 0xFF) ^ *buffer++];
 return crc^0xffffffff;
}

BOOL APIENTRY CrackMe1DlgProc(HWND hDlg,UINT message,WPARAM wParam,LPARAM lParam)
{
  char UserName[20];
  char RegCode[20];
  char CrcStr[20];
  hBtn=GetDlgItem(hDlg,ID_REGBUTTON);//取得"注册"按钮的句柄
  switch(message)
  {
  case WM_INITDIALOG://设置图标
  hIcon=LoadIcon(hInst,MAKEINTRESOURCE(IDI_ICON1));
  SendMessage(hDlg,WM_SETICON,TRUE,(WPARAM)hIcon);
  MakeTable();//初始化码表
  return(TRUE);
  case WM_CLOSE://关闭程序
  PostQuitMessage(0);
     return(TRUE);
  case WM_COMMAND:
  switch(LOWORD(wParam))
  {
  case ID_REGBUTTON:
         GetDlgItemText(hDlg,IDC_EDIT1,(LPSTR)UserName,20);//取得用户名
   GetDlgItemText(hDlg,IDC_EDIT2,(LPSTR)RegCode,20);//取得注册码
   if (strlen(UserName)!=0) //判断用户名是否为0
   {
    if (strlen(UserName)>5)
    {
     if (strlen(RegCode)!=0)
     {
                    int CrcInt=GetCrc32(UserName,strlen(UserName));//计算得到字符串的Crc32值
     char ch[20];
                    itoa(CrcInt,ch,16);//转换为字符形式
     if (strcmp(ch,RegCode)==0)//注册码是否相同
                    {
      EnableWindow(hBtn,FALSE);//注册码正确"注册"按钮变灰色
     }
     }
    }
   }
         return(TRUE);
  case ID_EXITBUTTON:
   PostQuitMessage(0);
   return(TRUE); 
  }
   return(TRUE);
  }
  return (FALSE);
}

实现了简单的注册机制,采用明码比较,大家在实际使用的过程中可以改变为非明码比较的,方式是是这样的:用户名=Decrypt(注册码),Decrypt采用对称算法,其它的反跟踪、程序自校验大家可以自己加上去,如果时间允许的话我都会发上来的。


原文转自:http://www.ltesting.net