概述
经常在坛子里看到讨论软件加密的帖子,纯软件加密与读取硬件序列号加密是经常讨论到的。
两种方法各有优缺点。
在通过读取硬件序列号的方法来加密的方法,受硬件的限制。
一般来说,CPU和T-Flash可能存在序列号。今天研究了一下 Windows CE 6.0 下的读取 SD 卡(T-Flash)的方法,以下将自己的实现过程列出来,供有需要的朋友一起学习。
函数的声明,在.H文件文件中:
#define SD_PART_NAME L"DSK2:"
#define VALID_SD_SERIAL_1 L"A7DFB784"
源代码如下函数所示:
BOOL GetStorageID(TCHAR *ptcCardName,TCHAR *ptcManufactureID,TCHAR *ptcSerialNum)
{
DWORD dwSize = 0;
DWORD dwReqSize = 0;
STORAGE_IDENTIFICATION StoreInfo;
STORAGE_IDENTIFICATION StoreInfo2;
HANDLE hVolume = NULL;
BOOL bRet = FALSE;
BYTE *pucSerialNo = NULL;
BYTE *pucManuID = NULL;
int i = 0;
ZeroMemory(&StoreInfo,sizeof(STORAGE_IDENTIFICATION));
hVolume = CreateFile(ptcCardName,GENERIC_READ | GENERIC_WRITE,0,NULL,OPEN_EXISTING,0,NULL);
if(NULL == hVolume || INVALID_HANDLE_VALUE == hVolume)
{
// MessageBox(L"Open Partation failed!");
RETAILMSG(1,(L"Open Partation failed!rn"));
return FALSE;
}
bRet = DeviceIoControl(hVolume,IOCTL_DISK_GET_STORAGEID,
NULL,0,(LPVOID)&StoreInfo,/*sizeof(STORAGE_IDENTIFICATION)*/3000,&dwSize,NULL);
if(!bRet)
{
DWORD dwErr = GetLastError();
// TCHAR tcError[64];
// wsprintf(tcError,L"Device IO 1 failed: %d!",dwErr);
// MessageBox(tcError);
RETAILMSG(1,(L"Device IO 1 failed: %d!rn",dwErr));
CloseHandle(hVolume);
return FALSE;
}
dwReqSize = StoreInfo.dwSize;
ASSERT(dwReqSize > 0);
dwSize = 0;
StoreInfo2.dwSize = dwReqSize;
bRet = DeviceIoControl(hVolume,IOCTL_DISK_GET_STORAGEID,
NULL,0,(LPVOID)&StoreInfo,dwReqSize,&dwSize,NULL);
if(FALSE == bRet)
{
DWORD dwErr = GetLastError();
// TCHAR tcError[64];
// wsprintf(tcError,L"Device IO 2 failed: %d!",dwErr);
// MessageBox(tcError);
RETAILMSG(1,(L"Device IO 2 failed: %d!rn",dwErr));
CloseHandle(hVolume);
return FALSE;
}
pucSerialNo = (((BYTE *)&StoreInfo) + StoreInfo.dwSerialNumOffset);
pucManuID = (((BYTE *)&StoreInfo) + StoreInfo.dwManufactureIDOffset);
while(pucSerialNo[i] != 0 && i < 200 && i < (int)(dwSize - StoreInfo.dwSerialNumOffset))
{
ptcSerialNum[i] = pucSerialNo[i];
i++;
}
pucSerialNo[i] = '