Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

abdelrahman

BMP encrytion decryption

This topic is 6416 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Advertisement
encrypt your graphics' data? it is easy ...

the following is the most easy way,though it is not-so well,it is enough for the game:

  

VOID Encrypt(PBYTE pSrcBlock,DWORD dwLength,PBYTE pDesBlock,LPTSTR lpszPassword,BOOL bEncrypt)
{
DWORD dwLoopCount;
int nPassword=0;

for (dwLoopCount=0; ;++dwLoopCount=0)
{
if (lpszPassword[dwLoopCount]==0) break;
nPassword+=(int)lpszPassword[dwLoopCount];
}

for (dwLoopCount=0; dwLoopCount<dwLength; ++dwLoopCount)
{
if (bEncrypt) //encrypt the source data block

pDesBlock[dwLoopCount] = pSrcBlock[dwLoopCount] + nPassword;
else //decrypt the source data block

pDesBlock[dwLoopCount]= pSrcBlock[dwLoopCount] - nPassword;
}
}

[/source]

build a MIX file is easy too <img src="smile.gif" width=15 height=15 align=middle>,and the most easy way is:
you can place the all graphics data blocks,
they contain the maps,NPCs,houses,trees,etc. and the sound data blocks,they contain
the background sound,character's talk contents etc. in your game into one file(though that is not-so
good),but how you can read or write these blocks in the files? well,you can define a structures
for store the basic description of each data block,the structure you can define as the following:

[source]

typedef struct _BLOCK_INFO
{
WORD wID; //the unique ID number of the data block

WORD wType; //type of the block,such as bitmap,wav,mp3,avi,etc.

DWORD dwOffset; //the offset of hte block on the file

DWORD dwWidth; //width of the block(in bytes)

DWORD dwHeight; //height of the block(in bytes)

DWORD dwSize; //size of the block(in bytes)

... //the other stuffs

} BLOCK_INFO,*PBLOCK_INFO;

[/source]

and then define a array,its each member is a variable of the BLOCK_INFO structure,contains the description
of one data block,after fill the array with the actual info.,place it on the beginning of the file.In order to
easy for add or delete a block of data,you can limit the count of data in the file,and reserve the enough space on
the beginning of the file to store the array only. In additional,you must store the count of current blocks to the file,
in general,you can store it to the most beginning of the file,and continue to the array.
the demo code for write and read the data as the following:

[source]

#define MAX_BLOCK_COUNT 1000
...
BOOL WriteDataBlock(BLOCK_INFO BlockInfo,PBYTE pBuff)
{
DWORD dwCurCount;
FILE *fp;

fp=fopen("stuff.dat","rw");
if (fp==NULL) return FALSE;
if ( fscanf(fp,"%d",&dwCurCount)!=sizeof(DWORD) )
{
fclose(fp);
return FALSE;
}
++dwCurCount;
if (dwCurCount>MAX_BLOCK_COUNT) //out of maximum count!

{
fclose(fp);
return FALSE;
}

//write the block of data description...

fseek(fp,dwCurCount,SEEK_SET);
if ( fwrite(&BlockInfo,sizeof(BLOCK_INFO),1,fp)!=sizeof(BLOCK_INFO) )
{
fclose(fp);
return FALSE;
}

//write the actual content of the block data...

fseek(fp,BlockInfo.dwOffset,SEEK_SET);
if ( fwrite(pBuff,BlockInfo.dwSize,1,fp)!=BlockInfo.dwSize )
{
fclose(fp);
return FALSE;
}
fclose(fp);
return TRUE;
}

BOOL ReadDataBlock(WORD wID,PBYTE pBuff,PDWORD pdwSizeOfBuff)
{
BLOCK_INFO BlockInfo;

FILE *fp;
fp=fopen("stuff.dat","r");
if (fp==NULL) return FALSE;

fseek(fp,wID*sizeof(BLOCK_INFO),SEEK_SET);
if( fread(&BlockInfo,sizeof(BLOCK_INFO),1,fp)!=sizeof(BLOCK_INFO) )
{
fclose(fp);
return FALSE;
}

if (*pdwSizeOfBuff<BlockInfo.dwSize) //the size of the buffer is ont enough for store the data!

{
fclose(fp);
return FALSE;
}
*pdwSizeOfBuff = BlockInfo.dwSize;

fseek(fp,BlockInfo.dwOffset,SEEK_SET);
if( fread(pBuff,BlockInfo.dwSize,1,fp)!=BlockInfo.dwSize )
{
fclose(fp);
return FALSE;
}

fclose(fp);
return TRUE;
}



ps: the above functions I hvae not test yet,it must have some bugs in them,guys,if you find some,pls tell me.



Edited by - zhang_zhou on November 28, 2000 10:25:01 PM

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!