BOOL ReadPasswd( byte *password)
{
HANDLE hFile = INVALID_HANDLE_VALUE;
DWORD NumberOfBytesWritten = KEY_LEN;
DWORD sizeLow = 0;
DWORD NumberOfBytesRead=KEY_LEN;
byte initpsd[KEY_LEN]; ///定义初始密码
// memset(m_readResultTemp,0,readBytes);
//打开已有的文件。
hFile = CreateFile(TEXT("My Documents\\key.txt"),GENERIC_WRITE|GENERIC_READ,0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_HIDDEN, NULL);
memcpy(initpsd,"123456780000",KEY_LEN);
//文件不存在,则创建初始文件,并向初始文件中写入初始数据。
if (INVALID_HANDLE_VALUE == hFile)
{
hFile = CreateFile(TEXT("My Documents\\key.txt"),GENERIC_WRITE|GENERIC_READ,0, NULL, CREATE_NEW, FILE_ATTRIBUTE_HIDDEN, NULL);
memcpy(initpsd,"123456780000",KEY_LEN);
memcpy(password,initpsd,KEY_LEN);
//数据加密
encrypt( TRUE, password, KEY_LEN );
encrypt( TRUE, (byte*)&ChangePassword, 1); //2008.7.17
//写入到文件
if( FALSE==WriteFile(hFile, password, NumberOfBytesWritten, &NumberOfBytesWritten, NULL) )
{
CloseHandle(hFile);
return FALSE;
}
NumberOfBytesWritten=1;//2008.7.17
if( FALSE==WriteFile(hFile, &ChangePassword, NumberOfBytesWritten, &NumberOfBytesWritten, NULL) )//2008.7.17
{
CloseHandle(hFile);
return FALSE;
}
if( FALSE==WriteFile(hFile, &number, NumberOfBytesWritten, &NumberOfBytesWritten, NULL) )//2008.7.17
{
CloseHandle(hFile);
return FALSE;
}
}
//读取文件中的数据。
// SetFilePointer(hFile,beginPointByteNum,NULL,FILE_BEGIN);
ChangePassword=false;
if(FALSE == ReadFile(hFile,password,KEY_LEN,&NumberOfBytesRead,NULL))
{
//操作失败。
CloseHandle(hFile);
return FALSE;
}
NumberOfBytesRead=1;//2008.7.17
if(FALSE == ReadFile(hFile,&ChangePassword,NumberOfBytesRead,&NumberOfBytesRead,NULL)) //2008.7.17
{
//操作失败。
CloseHandle(hFile);
return FALSE;
}
if(FALSE == ReadFile(hFile,&number,NumberOfBytesRead,&NumberOfBytesRead,NULL)) //2008.7.17
{
//操作失败。
CloseHandle(hFile);
return FALSE;
}
//解密读出的数据
encrypt( TRUE, password, KEY_LEN );
encrypt( TRUE, (byte *)&ChangePassword, 1 ); //2008.7.17
//关闭文件句柄。
CloseHandle(hFile);
hFile = INVALID_HANDLE_VALUE;
return TRUE;
}
BOOL WriteKey( byte *write, const byte writeposition, const byte writelen ) //2008.7.17
{
HANDLE hFile = INVALID_HANDLE_VALUE;
WORD error;
//打开已有文件
hFile = CreateFile(TEXT("My Documents\\key.txt"),GENERIC_WRITE|GENERIC_READ,0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_HIDDEN, NULL);
//打开失败,直接返回
if( hFile == INVALID_HANDLE_VALUE )
{
CloseHandle(hFile);
return FALSE;
}
//将文件指针移动到要操作的位置
SetFilePointer(hFile,writeposition,NULL,FILE_BEGIN);
//写入数据失败,直接返回
if( FALSE==WriteFile( hFile, write, writelen, (LPDWORD)writelen, NULL) )
{
//出错检查
/*error=GetLastError();*/
LPVOID lpMsgBuf;
FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS,
NULL,
GetLastError(),
0, // Default language
(LPTSTR) &lpMsgBuf,
0,
NULL
);
// Process any inserts in lpMsgBuf.
// ...
// Display the string.
MessageBox( NULL, (LPCTSTR)lpMsgBuf, NULL, MB_OK | MB_ICONERROR | MB_APPLMODAL | MB_TOPMOST );
// Free the buffer.
LocalFree( lpMsgBuf );
/*error=GetLastError();*/
CloseHandle(hFile);
return FALSE;
}
//写入数据成功,返回
CloseHandle(hFile);
hFile = INVALID_HANDLE_VALUE;
return TRUE;
}
BOOL ReadData( void *data, const byte readposition, const byte len ) ///2008.7.21
{
HANDLE hFile = INVALID_HANDLE_VALUE;
//打开已有文件
hFile = CreateFile(TEXT("My Documents\\key.txt"),GENERIC_WRITE|GENERIC_READ,0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_HIDDEN, NULL);
//打开文件失败,直接返回
if( hFile == INVALID_HANDLE_VALUE )
{
CloseHandle( hFile );
return FALSE;
}
//将文件指针移动到要操作的位置。
SetFilePointer( hFile,readposition,NULL,FILE_BEGIN);
//读文件数据
if( FALSE==ReadFile( hFile, data, len, (LPDWORD)len, NULL) )
{
//读取数据失败,直接返回
CloseHandle(hFile);
return FALSE;
}
//操作完毕,返回
CloseHandle(hFile);
hFile = INVALID_HANDLE_VALUE;
return TRUE;
}