硬盘加密相关函数

1、 GetPrivateProfileString()

功能:为初始化文件中指定的条目取得字串
函数原型:

DWORD GetPrivateProfileString(
  LPCTSTR lpAppName,        // 指定想要查找条目的名称
  LPCTSTR lpKeyName,        // 指定想要获取的项名或条目名
  LPCTSTR lpDefault,        // 指定的条目没有时返回的默认值
  LPTSTR lpReturnedString,  // 指定一个字串缓冲区
  DWORD nSize,              // 指定缓冲区大小
  LPCTSTR lpFileName        // 初始化文件名字
);

使用:

GetPrivateProfileStringA("Locker","psw","hello",keys,256,".\\Config.ini");



2、 CreateFile()

具体关于Windows文件的操作可参考:Windows文件操作

功能:可打开或创建以下对象,并返回可访问的句柄:

  • 文件
  • 管道
  • 控制台
  • 通信资源
  • 目录(只读打开)
  • 磁盘驱动器
  • 邮槽

函数原型:

HANDLE CreateFile(
  LPCTSTR lpFileName,
  DWORD dwDesiredAccess,
  DWORD dwShareMode,
  LPSECURITY_ATTRIBUTES lpSecurityAttributes,
  DWORD dwCreationDisposition,
  DWORD dwFlagsAndAttributes,
  HANDLE hTemplateFile
);

使用:

hFile = CreateFileA("\\\\.\\\\physicaldrive0",
        GENERIC_READ,
        FILE_SHARE_READ|FILE_SHARE_WRITE,
        NULL,
        OPEN_EXISTING,
        FILE_ATTRIBUTE_NORMAL,
        NULL);



3、 SetFilePointer()

功能:说明在一个文件中设置当前的读取位置
返回值:返回一个新位置,采用从文件起始处开始算起的一个字节偏移量。

函数原型:

DWORD SetFilePointer(
  HANDLE hFile,          // handle of file
  LONG lDistanceToMove,  // number of bytes to move file pointer
  PLONG lpDistanceToMoveHigh,
                         // pointer to high-order DWORD of 
                         // distance to move
  DWORD dwMoveMethod     // how to move
);

使用:

HANDLE hFile = NULL;
int offset = 0;
hFile = CreateFileA("\\\\.\\\\physicaldrive0",
        GENERIC_READ,
        FILE_SHARE_READ|FILE_SHARE_WRITE,NULL,OPEN_EXISTING,
        FILE_ATTRIBUTE_NORMAL,NULL);
if (hFile==INVALID_HANDLE_VALUE)
    {
        MessageBoxA(NULL,"不能打开 \\\\.\\\\physicaldrive0..","Error",MB_OK | MB_ICONERROR);
        return 0;
    }
SetFilePointer(hFile,offset,0,0);



4、 ReadFile()

功能:从文件指针指向的位置开始将数据读出到一个文件中,且支持同步和异步操作。
与fread函数相比,这个函数要明显灵活的多。该函数能够操作通信设备、管道、套接字以及邮槽。

函数原型:

BOOL ReadFile(
  HANDLE hFile,                // 文件句柄
  LPVOID lpBuffer,             // 保存数据的缓冲区
  DWORD nNumberOfBytesToRead,  // 要读入的字节数
  LPDWORD lpNumberOfBytesRead, // 指向实际读取字节数的指针
  LPOVERLAPPED lpOverlapped    // pointer to structure for data
);



5、WriteFile()

功能:可以将一个数据写入一个文件
与fwrite相比,这个函数要明显灵活的多。该函数能够操作通信设备、管道、套接字以及邮槽。

函数原型:

BOOL WriteFile(
  HANDLE hFile,                    // 文件句柄
  LPCVOID lpBuffer,                // 指向将写入文件的数据缓冲区
  DWORD nNumberOfBytesToWrite,     // 要写入的字节数
  LPDWORD lpNumberOfBytesWritten,  // 实际写入文件的字节数
  LPOVERLAPPED lpOverlapped        // pointer to structure for overlapped I/O
);

实例代码:

#include <windows.h>
#include "bin.h"
#define cryptFlag 445

int ReadDisk(int Id,int num,unsigned char *buffer);
int WriteDisk(int Id,int num,unsigned char* buffer);

int __stdcall WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd )
{
    char keys[256]={0};        //buffer,缓冲区,用来保存ini配置文件里面输入的密码
    unsigned char mbr[512]={0};    //用于保存从硬盘MBR里面读取出来的原始的MBR信息的
    unsigned char len;//255 
    int i = 0;

    GetPrivateProfileStringA("Locker","psw","left",keys,256,".\\Config.ini");
    len = strlen(keys);
    if (len==0 || len>=18)
    {
        MessageBoxA(NULL,"错误!密码长度建议控制在0-18内..","Error",MB_OK | MB_ICONERROR);
        exit(-1);
    }
    byte XResult=0;                //密码异或值
    for (i= 0; i < len; ++i)
        XResult^=keys[i];

    if(ReadDisk(0,1,mbr)==0)
    {
        MessageBoxA(NULL,"Read MBR failed!","Error",MB_OK | MB_ICONERROR);
        exit(-1);
    }

    if (mbr[cryptFlag] == 0x16)
    {
        MessageBoxA(NULL,"错误!硬盘已加锁,请不要重复加锁操作..","Error",MB_OK | MB_ICONERROR);
        exit(-1);
    }
    mbr[cryptFlag] = 0x16;
    for (i = 0; i < 64; ++i)
        mbr[446+i]^=XResult;
    WriteDisk(2,1,mbr);//将原始引导程序保存到第3扇区
    memcpy(lock_disk+cryptFlag,mbr+cryptFlag,67);
    WriteDisk(0,1,lock_disk);//写入加密的MBR
    //MessageBoxA(NULL,"恭喜你加锁成功了!!!\n请不要重复加锁!\n","Success",MB_OK | MB_ICONINFORMATION);
    return 0;
}

int ReadDisk(int Id,int num,unsigned char *buffer)
{
    /*
    读取扇区
    id = ID号
    num = 读取数量
    成功返回读取字节数
    */
    HANDLE hFile=NULL;
    int offset=0;
    int ReadSize=0;
    DWORD Readed=0;
    offset = Id*512;
    ReadSize = num*512;
    if (buffer==NULL)
    {
        return ReadSize;
    }
    hFile = CreateFileA("\\\\.\\\\physicaldrive0",
        GENERIC_READ,
        FILE_SHARE_READ|FILE_SHARE_WRITE,NULL,OPEN_EXISTING,
        FILE_ATTRIBUTE_NORMAL,NULL);
    if (hFile==INVALID_HANDLE_VALUE)
    {
        MessageBoxA(NULL,"不能打开 \\\\.\\\\physicaldrive0..","Error",MB_OK | MB_ICONERROR);
        return 0;
    }
    SetFilePointer(hFile,offset,0,0);
    ReadFile(hFile,buffer,ReadSize,&Readed,NULL);
    CloseHandle(hFile);
    return Readed;
}

int WriteDisk(int Id,int num,unsigned char* buffer)
{
    /*
    写扇区
    */
    HANDLE hFile = NULL;
    int WriteSize = 0;
    int offset = 0;
    DWORD Writed = 0;
    offset = Id*512;
    WriteSize = num*512;
    if (buffer==NULL)
    {
        return WriteSize;
    }
    //Open it
    hFile = CreateFileA("\\\\.\\\\physicaldrive0",1073741824, 1, 0, 3, 128, 0);
    if (hFile==INVALID_HANDLE_VALUE)
    {
        MessageBoxA(NULL,"不能打开 \\\\.\\\\physicaldrive0..","Error",MB_OK | MB_ICONERROR);
        return 0;
    }
    SetFilePointer (hFile, offset, 0, 0);
    WriteFile(hFile,buffer,WriteSize,&Writed,0);
    CloseHandle(hFile);
    return WriteSize;
}

猜你喜欢

转载自www.cnblogs.com/coolcpp/p/hardware-encrypt.html