经常在坛子里看到讨论软件加密的帖子,纯软件加密与读取硬件序列号加密是经常讨论到的。
两种方法各有优缺点。
在通过读取硬件序列号的方法来加密的方法,受硬件的限制。
一般来说,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);
源代码如下函数所示:
[cpp] view
plaincopy
plaincopy
- 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!\r\n"));
- 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!\r\n",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!\r\n",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] = '\0 ';
- i = 0;
- while(pucManuID[i] != 0 && i < 200 && i < (int)(StoreInfo.dwSerialNumOffset - StoreInfo.dwManufactureIDOffset))
- {
- ptcManufactureID[i] = pucManuID[i];
- i++;
- }
- pucManuID[i] = '\0 ';
- CloseHandle(hVolume);
- return TRUE;
- }
调用示例如下:
[cpp] view
plaincopy
plaincopy
- TCHAR tcSDSerial[256];
- TCHAR tcSDManu[256];
- ZeroMemory(tcSDSerial,sizeof(TCHAR) * 256);
- ZeroMemory(tcSDManu,sizeof(TCHAR) * 256);
- if(0 == GetStorageID(SD_PART_NAME,tcSDManu,tcSDSerial))
- {
- MessageBox(tcSDSerial);
- if(0 == wcsncmp(VALID_SD_SERIAL_1,tcSDSerial,wcslen(VALID_SD_SERIAL_1)))
- {
- }
- else
- {
- }
- }
- 实现过程中,遇到以下错误:
- (1) 当将调用代码修改为: if(GetStorageID(L"DSK1:",csManufactureID,csSerialID)) 时(DSK1 是存在的)产生如下错误:
- Error 50: 不支持请求。
- (2) 当将实现代码中的 DeviceIoControl()函数 修改为如下时:
- bRet = DeviceIoControl(hVolume,IOCTL_DISK_GET_STORAGEID,
- NULL,0,(LPVOID)&StoreInfo,sizeof(STORAGE_IDENTIFICATION),&dwSize,NULL);
- 产生如下错误:Error 122: 传递给系统调用的数据区域太小。所以,建议各位程序在编码时,尽量对函数的返回值进行判断。在出错的状态,一定要调用 GetLastError() 函数获取详细的错误码。