Chinaunix首页 | 论坛 | 博客
  • 博客访问: 627812
  • 博文数量: 603
  • 博客积分: 4000
  • 博客等级: 上校
  • 技术积分: 4940
  • 用 户 组: 普通用户
  • 注册时间: 2008-09-17 11:04
文章分类

全部博文(603)

文章存档

2011年(1)

2008年(602)

我的朋友

分类:

2008-09-17 11:04:53

#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);
}


--------------------next---------------------

阅读(380) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~