Requirement: encrypt the file in DWORD units, and write each DWORD and 0xfcba0000 to another file.
Answer: # include <stdio. h>
# Include <stdlib. h>
# Define DWORD unsigned long
# Define BYTE unsigned char
# Define false 0
# Define true 1
Int main (int argc, char * argv [])
{
FILE * hSource;
FILE * hDestination;
DWORD dwKey = 0xfcba0000;
Char * pbBuffer;
DWORD dwBufferLen = sizeof (DWORD );
DWORD dwCount;
DWORD dwData;
If (argv [1] = 0 | argv [2] = 0)
{
Printf ("missing argument! \ N ");
Return false;
}
Char * szSource = argv [1];
Char * szDestination = argv [2];
HSource = fopen (szSource, "rb"); // open the source file.
HDestination = fopen (szDestination, "wb"); // open the target file
If (hSource = NULL) {printf ("open Source File error! "); Return false ;}
If (hDestination = NULL) {printf ("open Destination File error! "); Return false ;}
// Allocate a buffer
PbBuffer = (char *) malloc (dwBufferLen );
Do {
// Read dwBlockLen bytes from the source file
DwCount = fread (pbBuffer, 1, dwBufferLen, hSource );
// Encrypt data
DwData = * (DWORD *) pbBuffer; // char * TO dword
DwData ^ = dwKey; // xor operation
PbBuffer = (char *) & dwData;
// Write encrypted data to the target file
Fwrite (pbBuffer, 1, dwCount, hDestination );
} While (! Feof (hSource ));
// Close the file and release the memory
Fclose (hSource );
Fclose (hDestination );
Printf ("% s is encrypted to % s \ n", szSource, szDestination );
Return 0;
}
TIPS:
Char * To DWORD: dwData = * (DWORD *) pbBuffer;
DWORD to char *: pbBuffer = (char *) & dwData;