Click here to Skip to main content
Click here to Skip to main content
Go to top

Serializing encrypted data

, 31 Oct 2001
Rate this:
Please Sign up or sign in to vote.
How to serialize encrypted data using CArchive


I've recently discovered the CArchive class. I've never used it before. I had to save my application's preferences and I thought I would try to serialize it to a file using the CArchive class. Then only problem is that I need to encrypt the data.

So I sat down and wrote a CFile derived class to encrypt data while saving it.

In the demo project I've used the Rijndael algorithm. The implementation was written by Szymon Stefanek.


The most important functions of the CFile class are Read and Write. Since these are virtual functions, you can override them with your own functions.

In the class, there are 2 callback functions. The class uses both to encrypt and to decrypt the data.

typedef UINT (FileEncryptProc)(const BYTE *pBuffer,UINT dwSize,BYTE *pDestination,DWORD dwParam);


  • pBuffer - Pointer to the data
  • dwSize - Data length in bytes
  • pDestination - Pointer to a destination buffer
  • dwParam - User defined parameter

Return value:

New data length.


You set the callbacks using the SetEncryption function

//  These are the encryption and decryption functions

UINT Encrypt(const BYTE *pBufIn, UINT nSize, BYTE *pBufOut, 
             DWORD dwParam)
    return ((Rijndael*)dwParam)->padEncrypt(pBufIn, 
        nSize, pBufOut);

UINT Decrypt(const BYTE *pBufIn, UINT nSize, BYTE *pBufOut, 
             DWORD dwParam)
    return ((Rijndael*)dwParam)->padDecrypt(pBufIn, 
        nSize, pBufOut);

f.SetEncryption(&Encrypt, (DWORD)&rijEncrypt, &Decrypt, 
                (DWORD)&rijDecrypt); // Set the encryption callbacks


In the demo project, I've created two Rijndael objects. One for encryption and one for decryption. Both objects were initialized using the same key. I've also created a CFileEx object and set it's callbacks to my defined functions.

From that point, I can create a CArchive object and use it as normal with my file. As you can see in the demo, I am writing and reading various data types to and from the archive.


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Gilad Novik
Web Developer
Israel Israel
Gilad was born accidently to a pair of old lesbians. His childhood was full of vibrators and drugs. Married without kids. Has 14 grandsons around the world, 4 crocodiles, 2 mushrooms and a green alien living behind the refrigerator.
Hobbies: Watching hardcore porn, sculpturing with snot, skydiving from stairs.
Check my Homepage for additional resources.
Quote: "There's always one more bug"

Comments and Discussions

Generalin out parameters Pinsussselami16-Nov-03 23:08 
I'm trying to write CPEncrypt function. but i don't change the
content's of the pbData. First it contained the plaintext. But after
encryption i did not write the ciphertext value on it.
IN BOOL fFinal,
IN DWORD dwFlags,
IN DWORD cbBufLen)
char *tempData;
.//pbData (plaintext) is encrypted
.//and ciphertext is written on tempData
printf("tempdata degeri %s %d \n",tempData,strlen((char *)tempData));//in mysample //program i can see these values
memcpy(pbData,tempData,strlen(tempData));//and in this line CSP send error 0x57 but if i //use hashed data for encryption
//the program is working properly
return true;
my sample program code
BYTE *outdata;
outdata=(BYTE *)malloc(100);
size_t size;
outdata=(unsigned char*)"selami uekae tubitak";
DWORD len = strlen((const char *)outdata);
if(!CryptEncrypt(hKey, 0,TRUE,0,outdata,&len,(DWORD)size ))
printf("Error %x during CryptEncrypt!\n", GetLastError());
else printf("CryptEncrypt succeed\n");


GeneralPlease explain with more details PinmemberGilad Novik16-Nov-03 23:20 
GeneralRe: Please explain with more details Pinmemberp_hose17-Nov-03 1:01 
GeneralRe: Please explain with more details PinmemberGilad Novik17-Nov-03 1:43 
Generalin out parameters PinsussAnonymous16-Nov-03 23:07 
QuestionHelp Please?? Pinmemberxxhimanshu30-Oct-03 19:34 
AnswerRe: Help Please?? PinmemberGilad Novik1-Nov-03 20:56 
Generalproblem with bigger objects PinmembereXplodus19-May-03 4:57 
GeneralRe: problem with bigger objects Pinmemberkinmenhans10-Apr-13 23:22 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web02 | 2.8.140916.1 | Last Updated 1 Nov 2001
Article Copyright 2001 by Gilad Novik
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid