BMP encrytion decryption

Started by
0 comments, last by abdelrahman 23 years, 5 months ago
Can anyone help me I want to encrypt my graphics How ? I would like to do a MIX file or something like it Thanks Bye 4 now System Failure
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 containthe background sound,character's talk contents etc. in your game into one file(though that is not-sogood),but how you can read or write these blocks in the files? well,you can define a structuresfor 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 toeasy for add or delete a block of data,you can limit the count of data in the file,and  reserve the enough space onthe 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
============================= Hey,I just wanna know WHY! =============================

This topic is closed to new replies.

Advertisement