Click here to Skip to main content
13,297,513 members (49,859 online)
Click here to Skip to main content


42 bookmarked
Posted 21 May 2002

Data encryption with DPAPI

A wrapper class for the Data Protection API
// (w)ritten by andreas saurwein 2002,
#pragma once
#include <wincrypt.h>
#pragma comment(lib, "Crypt32.lib")

#include <afxtempl.h>

class CProtectedData
	CProtectedData(BOOL bSilent=TRUE, BOOL bLocal=FALSE, BOOL bAudit=FALSE);
	virtual ~CProtectedData(void);

	void SetUI(HWND hWnd, LPCTSTR pPrompt);
	void SetAudit(BOOL bAudit=TRUE);
	void SetLocal(BOOL bLocal=TRUE);

	void SetData(LPBYTE pData, DWORD dwSize);

	const DATA_BLOB* ProtectData();										// no description, no entropy
	const DATA_BLOB* ProtectData(LPCTSTR pDesc);						// no entropy
	const DATA_BLOB* ProtectData(LPCTSTR pDesc, const CString& rString);// entropy is a CString
	const DATA_BLOB* ProtectData(LPCTSTR pDesc, LPCTSTR pString);		// entropy is a LPCTSTR
	const DATA_BLOB* ProtectData(LPCTSTR pDesc, LPBYTE pEntropy, DWORD dwEntropySize);	// returns encrypted data and the size
	void FreeProtectedData();						// free the associated datablock

	DATA_BLOB* UnprotectData();											// no description, no entropy
	DATA_BLOB* UnprotectData(LPTSTR* pDesc);								// no entropy
	DATA_BLOB* UnprotectData(LPTSTR* pDesc, const CString& rString);		// entropy is a CString
	DATA_BLOB* UnprotectData(LPTSTR* pDesc, LPCTSTR pString);			// entropy is a LPCTSTR
	DATA_BLOB* UnprotectData(LPTSTR* pDesc, LPBYTE pEntropy, DWORD dwEntropySize);	// returns decrypted data and the size
	void FreeUnprotectedData();						// free the associated datablock

	DATA_BLOB					m_ProtectedData;	// points to the protected data
	CRYPTPROTECT_PROMPTSTRUCT	m_Prompt;			// prompt data
	BOOL						m_bSilent;			// dont prompt
	BOOL						m_bLocal;			// use machine local encryption
	BOOL						m_bAudit;			// audit any encrypt/decrypt operation

	LPBYTE						m_pData;			// actual data
	DWORD						m_dwSize;			// actual size of data

class CUserProtectedData : public CProtectedData
	CUserProtectedData(BOOL bSilent=TRUE, BOOL bAudit=FALSE) : CProtectedData(bSilent, FALSE, bAudit) {}
	virtual ~CUserProtectedData(void) {};

class CMachineProtectedData : public CProtectedData
	CMachineProtectedData(BOOL bSilent=TRUE, BOOL bAudit=FALSE) : CProtectedData(bSilent, TRUE, bAudit) {}
	virtual ~CMachineProtectedData(void) {};

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

No Biography provided

You may also be interested in...

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.171207.1 | Last Updated 22 May 2002
Article Copyright 2002 by Andreas S. Franci Gonçalves
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid