Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version

Data encryption with DPAPI

, 21 May 2002 CPOL
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)

Share

About the Author

No Biography provided

| Advertise | Privacy | Mobile
Web01 | 2.8.141022.2 | Last Updated 22 May 2002
Article Copyright 2002 by Andreas S. Franci Gonçalves
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid