如提,能否发一份读硬盘序列号源代码的实例给我。(不要dll)
还有听说过共享软件是通过把硬盘序列号通过MD5加密算法成的,能否顺便把有关的加密函数给我,如果可以的话,顺便贴上基本原理。谢谢
还有听说过共享软件是通过把硬盘序列号通过MD5加密算法成的,能否顺便把有关的加密函数给我,如果可以的话,顺便贴上基本原理。谢谢
解决方案 »
- 转载:苹果+谷歌=全球最恐怖的监控系统?
- Socket Send Receive 问题
- 小问题
- _RecordsetPtr->Open("SQL语句"...)异常(失败)怎么办???
- 求救,我想读取摄像头中图像,请问该从什么地方下手啊,请高手们指点!
- 救命,十万火急!!!能否给一个用VC修改not null改为null的例子啊?
- 求助,我用mscomm控件进行串口通信,调试了几次,发现少了一个类!
- 简单问题:怎么从“std::string”转换为“LPTSTR”
- 调试?????(在线等,请知道的网友快点答复,在此万分感谢!!!)
- 一个大数据的吞吐转发程序,利用线程池到底能提高多大效率
- 如何得到程序的变量类表??
- 工具栏位图用什么软件做?(超菜问题):)
MD5 是信息摘要算法,相当于对你提供的数据信息进行摘要后生成一个对应的16位字串来代替原件——但不可能从这16位字串中恢复出摘要前的信息。
信息在传递过程中只会减少不会增加地。MD5 在 Windows2000/XP/2003下实现比较简单,下面是部分源代码。
-------------------------------------------------------
HCRYPTPROV hCryptProv;
HCRYPTHASH hHash;
BYTE bHash[0x7f];
DWORD dwHashLen= 16; // The MD5 algorithm always returns 16 bytes.
DWORD cbContent= csBuffer.GetLength();
BYTE* pbContent= (BYTE*)csBuffer.GetBuffer(cbContent);
if(CryptAcquireContext(&hCryptProv,
NULL, NULL, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT | CRYPT_MACHINE_KEYSET))
{ if(CryptCreateHash(hCryptProv,
CALG_MD5, // algorithm identifier definitions see: wincrypt.h
0, 0, &hHash))
{
if(CryptHashData(hHash, pbContent, cbContent, 0))
{ if(CryptGetHashParam(hHash, HP_HASHVAL, bHash, &dwHashLen, 0))
{
// Make a string version of the numeric digest value
csDigest.Empty();
CString tmp;
for (int i = 0; i<16; i++)
{
tmp.Format("%02x", bHash[i]);
csDigest+=tmp;
} }
else csDigest=_T("Error getting hash param."); }
else csDigest=_T("Error hashing data.");
}
else csDigest=_T("Error creating hash."); }
else csDigest=_T("Error acquiring context"); CryptDestroyHash(hHash);
CryptReleaseContext(hCryptProv, 0);
csBuffer.ReleaseBuffer();
return csDigest;
-------------------------------------------------------
需要引入 wincrypt.h,并连接 Cryptapi.lib
char strTemp[500];char driSerialNum[256];#define PRINTING_TO_CONSOLE_ALLOWED
#include <stdlib.h>
#include <stdio.h>
#include <windows.h>
#pragma pack(1)#define MAX_IDE_DRIVES 4
#define IDENTIFY_BUFFER_SIZE 512
// IOCTL commands
#define DFP_GET_VERSION 0x00074080
#define DFP_SEND_DRIVE_COMMAND 0x0007c084
#define DFP_RECEIVE_DRIVE_DATA 0x0007c088#define FILE_DEVICE_SCSI 0x0000001b
#define IOCTL_SCSI_MINIPORT_IDENTIFY ((FILE_DEVICE_SCSI << 16) + 0x0501)
#define IOCTL_SCSI_MINIPORT 0x0004D008 // see NTDDSCSI.H for definitiontypedef struct _GETVERSIONOUTPARAMS
{
BYTE bVersion; // Binary driver version.
BYTE bRevision; // Binary driver revision.
BYTE bReserved; // Not used.
BYTE bIDEDeviceMap; // Bit map of IDE devices.
DWORD fCapabilities; // Bit mask of driver capabilities.
DWORD dwReserved[4]; // For future use.
} GETVERSIONOUTPARAMS, *PGETVERSIONOUTPARAMS, *LPGETVERSIONOUTPARAMS;#define CAP_IDE_ID_FUNCTION 1 // ATA ID command supported
#define CAP_IDE_ATAPI_ID 2 // ATAPI ID command supported
#define CAP_IDE_EXECUTE_SMART_FUNCTION 4 // SMART commannds supportedtypedef struct _IDEREGS
{
BYTE bFeaturesReg; // Used for specifying SMART "commands".
BYTE bSectorCountReg; // IDE sector count register
BYTE bSectorNumberReg; // IDE sector number register
BYTE bCylLowReg; // IDE low order cylinder value
BYTE bCylHighReg; // IDE high order cylinder value
BYTE bDriveHeadReg; // IDE drive/head register
BYTE bCommandReg; // Actual IDE command.
BYTE bReserved; // reserved for future use. Must be zero.
} IDEREGS, *PIDEREGS, *LPIDEREGS;typedef struct _SENDCMDINPARAMS
{
DWORD cBufferSize; // Buffer size in bytes
IDEREGS irDriveRegs; // Structure with drive register values.
BYTE bDriveNumber; // Physical drive number to send
// command to (0,1,2,3).
BYTE bReserved[3]; // Reserved for future expansion.
DWORD dwReserved[4]; // For future use.
BYTE bBuffer[1]; // Input buffer.
} SENDCMDINPARAMS, *PSENDCMDINPARAMS, *LPSENDCMDINPARAMS;#define IDE_ATAPI_IDENTIFY 0xA1 // Returns ID sector for ATAPI.
#define IDE_ATA_IDENTIFY 0xEC // Returns ID sector for ATA.
// Status returned from driver
typedef struct _DRIVERSTATUS
{
BYTE bDriverError; // Error code from driver, or 0 if no error.
BYTE bIDEStatus; // Contents of IDE Error register.
// Only valid when bDriverError is SMART_IDE_ERROR.
BYTE bReserved[2]; // Reserved for future expansion.
DWORD dwReserved[2]; // Reserved for future expansion.
} DRIVERSTATUS, *PDRIVERSTATUS, *LPDRIVERSTATUS;
// Structure returned by PhysicalDrive IOCTL for several commands
typedef struct _SENDCMDOUTPARAMS
{
DWORD cBufferSize; // Size of bBuffer in bytes
DRIVERSTATUS DriverStatus; // Driver status structure.
BYTE bBuffer[1]; // Buffer of arbitrary length in which to store the data read from the // drive.
} SENDCMDOUTPARAMS, *PSENDCMDOUTPARAMS, *LPSENDCMDOUTPARAMS;
typedef struct _IDSECTOR
{
USHORT wGenConfig;
USHORT wNumCyls;
USHORT wReserved;
USHORT wNumHeads;
USHORT wBytesPerTrack;
USHORT wBytesPerSector;
USHORT wSectorsPerTrack;
USHORT wVendorUnique[3];
CHAR sSerialNumber[20];
USHORT wBufferType;
USHORT wBufferSize;
USHORT wECCSize;
CHAR sFirmwareRev[8];
CHAR sModelNumber[40];
USHORT wMoreVendorUnique;
USHORT wDoubleWordIO;
USHORT wCapabilities;
USHORT wReserved1;
USHORT wPIOTiming;
USHORT wDMATiming;
USHORT wBS;
USHORT wNumCurrentCyls;
USHORT wNumCurrentHeads;
USHORT wNumCurrentSectorsPerTrack;
ULONG ulCurrentSectorCapacity;
USHORT wMultSectorStuff;
ULONG ulTotalAddressableSectors;
USHORT wSingleWordDMA;
USHORT wMultiWordDMA;
BYTE bReserved[128];
} IDSECTOR, *PIDSECTOR;
typedef struct _SRB_IO_CONTROL
{
ULONG HeaderLength;
UCHAR Signature[8];
ULONG Timeout;
ULONG ControlCode;
ULONG ReturnCode;
ULONG Length;
} SRB_IO_CONTROL, *PSRB_IO_CONTROL;
// Define global buffers.
BYTE IdOutCmd [sizeof (SENDCMDOUTPARAMS) + IDENTIFY_BUFFER_SIZE - 1];
char *ConvertToString (DWORD diskdata [256], int firstIndex, int lastIndex);
void PrintIdeInfo (int drive, DWORD diskdata [256]);
BOOL DoIDENTIFY (HANDLE, PSENDCMDINPARAMS, PSENDCMDOUTPARAMS, BYTE, BYTE,
PDWORD);
int ReadPhysicalDriveInNT (void)
{
int done = FALSE;
int drive = 0; for (drive = 0; drive < MAX_IDE_DRIVES; drive++)
{
HANDLE hPhysicalDriveIOCTL = 0; // Try to get a handle to PhysicalDrive IOCTL, report failure
// and exit if can't.
char driveName [256]; sprintf (driveName, "\\\\.\\PhysicalDrive%d", drive); // Windows NT, Windows 2000, must have admin rights
hPhysicalDriveIOCTL = CreateFile (driveName,
GENERIC_READ | GENERIC_WRITE,
FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
OPEN_EXISTING, 0, NULL);
// if (hPhysicalDriveIOCTL == INVALID_HANDLE_VALUE)
// printf ("Unable to open physical drive %d, error code: 0x%lX\n",
// drive, GetLastError ()); if (hPhysicalDriveIOCTL != INVALID_HANDLE_VALUE)
{
GETVERSIONOUTPARAMS VersionParams;
DWORD cbBytesReturned = 0; // Get the version, etc of PhysicalDrive IOCTL
memset ((void*) &VersionParams, 0, sizeof(VersionParams)); if ( ! DeviceIoControl (hPhysicalDriveIOCTL, DFP_GET_VERSION,
NULL,
0,
&VersionParams,
sizeof(VersionParams),
&cbBytesReturned, NULL) )
{
// printf ("DFP_GET_VERSION failed for drive %d\n", i);
// continue;
} // If there is a IDE device at number "i" issue commands
// to the device
if (VersionParams.bIDEDeviceMap > 0)
{
BYTE bIDCmd = 0; // IDE or ATAPI IDENTIFY cmd
SENDCMDINPARAMS scip;
//SENDCMDOUTPARAMS OutCmd; // Now, get the ID sector for all IDE devices in the system.
// If the device is ATAPI use the IDE_ATAPI_IDENTIFY command,
// otherwise use the IDE_ATA_IDENTIFY command
bIDCmd = (VersionParams.bIDEDeviceMap >> drive & 0x10) ? \
IDE_ATAPI_IDENTIFY : IDE_ATA_IDENTIFY; memset (&scip, 0, sizeof(scip));
memset (IdOutCmd, 0, sizeof(IdOutCmd)); if ( DoIDENTIFY (hPhysicalDriveIOCTL,
&scip,
(PSENDCMDOUTPARAMS)&IdOutCmd,
(BYTE) bIDCmd,
(BYTE) drive,
&cbBytesReturned))
{
DWORD diskdata [256];
int ijk = 0;
USHORT *pIdSector = (USHORT *)
((PSENDCMDOUTPARAMS) IdOutCmd) -> bBuffer; for (ijk = 0; ijk < 256; ijk++)
diskdata [ijk] = pIdSector [ijk]; PrintIdeInfo (drive, diskdata); done = TRUE;
}
} CloseHandle (hPhysicalDriveIOCTL);
}
} return done;
}
PSENDCMDOUTPARAMS pSCOP, BYTE bIDCmd, BYTE bDriveNum,
PDWORD lpcbBytesReturned)
{
// Set up data structures for IDENTIFY command.
pSCIP -> cBufferSize = IDENTIFY_BUFFER_SIZE;
pSCIP -> irDriveRegs.bFeaturesReg = 0;
pSCIP -> irDriveRegs.bSectorCountReg = 1;
pSCIP -> irDriveRegs.bSectorNumberReg = 1;
pSCIP -> irDriveRegs.bCylLowReg = 0;
pSCIP -> irDriveRegs.bCylHighReg = 0; // Compute the drive number.
pSCIP -> irDriveRegs.bDriveHeadReg = 0xA0 | ((bDriveNum & 1) << 4); // The command can either be IDE identify or ATAPI identify.
pSCIP -> irDriveRegs.bCommandReg = bIDCmd;
pSCIP -> bDriveNumber = bDriveNum;
pSCIP -> cBufferSize = IDENTIFY_BUFFER_SIZE; return ( DeviceIoControl (hPhysicalDriveIOCTL, DFP_RECEIVE_DRIVE_DATA,
(LPVOID) pSCIP,
sizeof(SENDCMDINPARAMS) - 1,
(LPVOID) pSCOP,
sizeof(SENDCMDOUTPARAMS) + IDENTIFY_BUFFER_SIZE - 1,
lpcbBytesReturned, NULL) );
}BOOL LoadDeviceDriver( const TCHAR * Name, const TCHAR * Path, HANDLE * lphDevice );
BOOL UnloadDeviceDriver( const TCHAR * Name );HANDLE hDriver;
bool IsNT;
bool IsWinIoInitialized = false;
bool IsWinNT()
{
OSVERSIONINFO OSVersionInfo; OSVersionInfo.dwOSVersionInfoSize = sizeof(OSVERSIONINFO); GetVersionEx(&OSVersionInfo); return OSVersionInfo.dwPlatformId == VER_PLATFORM_WIN32_NT;
}
bool InitializeWinIo()
{
char szExePath[MAX_PATH];
PSTR pszSlash; IsNT = IsWinNT(); if (IsNT)
{
if (!GetModuleFileName(GetModuleHandle(NULL), szExePath, sizeof(szExePath)))
return false; pszSlash = strrchr(szExePath, '\\'); if (pszSlash)
pszSlash[1] = 0;
else
return false; strcat(szExePath, "winio.sys");// UnloadDeviceDriver("WINIO");// if (!LoadDeviceDriver("WINIO", szExePath, &hDriver))
// return false;
}
IsWinIoInitialized = true; return true;
}
void ShutdownWinIo()
{
// if (IsNT)
// UnloadDeviceDriver("WINIO");
}
// ------------------------------------------------ //
// Port32 v3.0 //
// Direct Port Access Under Windows 9x/NT/2000 //
// Copyright 1998-2000 Yariv Kaplan //
// http://www.internals.com //
// ------------------------------------------------ ////#include <windows.h>
#include <winioctl.h>
#include "port32.h"
#include "winio.h"
//#include "general.h"// These are our ring 0 functions responsible for tinkering with the hardware ports.
// They have a similar privilege to a Windows VxD and are therefore free to access
// protected system resources (such as the page tables) and even place calls to
// exported VxD services.__declspec(naked) void Ring0GetPortVal()
{
_asm
{
Cmp CL, 1
Je ByteVal
Cmp CL, 2
Je WordVal
Cmp CL, 4
Je DWordValByteVal: In AL, DX
Mov [EBX], AL
RetfWordVal: In AX, DX
Mov [EBX], AX
RetfDWordVal: In EAX, DX
Mov [EBX], EAX
Retf
}
}
__declspec(naked) void Ring0SetPortVal()
{
_asm
{
Cmp CL, 1
Je ByteVal
Cmp CL, 2
Je WordVal
Cmp CL, 4
Je DWordValByteVal: Mov AL, [EBX]
Out DX, AL
RetfWordVal: Mov AX, [EBX]
Out DX, AX
RetfDWordVal: Mov EAX, [EBX]
Out DX, EAX
Retf
}
}
// This function makes it possible to call ring 0 code from a ring 3
// application.bool CallRing0(PVOID pvRing0FuncAddr, WORD wPortAddr, PDWORD pdwPortVal, BYTE bSize)
{ struct GDT_DESCRIPTOR *pGDTDescriptor;
struct GDTR gdtr;
WORD CallgateAddr[3];
WORD wGDTIndex = 1; _asm Sgdt [gdtr] // Skip the null descriptor pGDTDescriptor = (struct GDT_DESCRIPTOR *)(gdtr.dwGDTBase + 8); // Search for a free GDT descriptor for (wGDTIndex = 1; wGDTIndex < (gdtr.wGDTLimit / 8); wGDTIndex++)
{
if (pGDTDescriptor->Type == 0 &&
pGDTDescriptor->System == 0 &&
pGDTDescriptor->DPL == 0 &&
pGDTDescriptor->Present == 0)
{
// Found one !
// Now we need to transform this descriptor into a callgate.
// Note that we're using selector 0x28 since it corresponds
// to a ring 0 segment which spans the entire linear address
// space of the processor (0-4GB). struct CALLGATE_DESCRIPTOR *pCallgate; pCallgate = (struct CALLGATE_DESCRIPTOR *) pGDTDescriptor;
pCallgate->Offset_0_15 = LOWORD(pvRing0FuncAddr);
pCallgate->Selector = 0x28;
pCallgate->ParamCount = 0;
pCallgate->Unused = 0;
pCallgate->Type = 0xc;
pCallgate->System = 0;
pCallgate->DPL = 3;
pCallgate->Present = 1;
pCallgate->Offset_16_31 = HIWORD(pvRing0FuncAddr); // Prepare the far call parameters CallgateAddr[0] = 0x0;
CallgateAddr[1] = 0x0;
CallgateAddr[2] = (wGDTIndex << 3) | 3; // Please fasten your seat belts!
// We're about to make a hyperspace jump into RING 0. _asm Mov DX, [wPortAddr]
_asm Mov EBX, [pdwPortVal]
_asm Mov CL, [bSize]
_asm Call FWORD PTR [CallgateAddr] // We have made it !
// Now free the GDT descriptor memset(pGDTDescriptor, 0, 8); // Our journey was successful. Seeya. return true;
} // Advance to the next GDT descriptor pGDTDescriptor++;
} // Whoops, the GDT is full return false;
}
bool GetPortVal(WORD wPortAddr, PDWORD pdwPortVal, BYTE bSize)
{
bool Result;
DWORD dwBytesReturned;
struct tagPort32Struct Port32Struct; if (IsNT)
{
if (!IsWinIoInitialized)
return false; Port32Struct.wPortAddr = wPortAddr;
Port32Struct.bSize = bSize; if (!DeviceIoControl(hDriver, IOCTL_WINIO_READPORT, &Port32Struct,
sizeof(struct tagPort32Struct), &Port32Struct,
sizeof(struct tagPort32Struct),
&dwBytesReturned, NULL))
return false;
else
*pdwPortVal = Port32Struct.dwPortVal;
}
else
{
Result = CallRing0((PVOID)Ring0GetPortVal, wPortAddr, pdwPortVal, bSize); if (Result == false)
return false;
} return true;
}
今天到公司上网才发现楼主等了这么久。真是对不住,让你等这么久了。
这里有个磁盘类,也就是kwiner(最爱编程) 贴的这个
http://www.vckbase.com/code/listcode.asp?mclsid=15&sclsid=1503
一个获得硬盘物理信息的类 详细信息 < 驱动器与磁盘信息 >
http://www.vckbase.com/code/listcode.asp?mclsid=15&sclsid=1503