Click here to Skip to main content
15,889,096 members
Articles / Desktop Programming / MFC

Data encryption with DPAPI

Rate me:
Please Sign up or sign in to vote.
3.73/5 (6 votes)
21 May 2002CPOL4 min read 179K   3.9K   44  
A wrapper class for the Data Protection API
//
// (w)ritten by andreas saurwein 2002, mailto:saurwein@uniwares.com
//
#pragma once
#include <wincrypt.h>
#pragma comment(lib, "Crypt32.lib")

#include <afxtempl.h>

class CProtectedData
{
public:
	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

private:
	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
{
public:
	CUserProtectedData(BOOL bSilent=TRUE, BOOL bAudit=FALSE) : CProtectedData(bSilent, FALSE, bAudit) {}
	virtual ~CUserProtectedData(void) {};
};

class CMachineProtectedData : public CProtectedData
{
public:
	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.

License

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


Written By
Software Developer (Senior)
Portugal Portugal
Software Smith, Blacksmith, Repeat Founder, Austrian, Asgardian.

Comments and Discussions