#include "stdafx.h"
#include "CEncrypt.h"
#include <stdio.h>
//*******************************************************************************
// temporary
//*******************************************************************************
#include "CryptErr.h"
//*******************************************************************************
// Constructor[s] and Destructor
//*******************************************************************************
CEncrypt::CEncrypt(LPSTR pCryptContainer, LPSTR pCryptProvider, DWORD dwProviderType, DWORD dwFlags)
{
BOOL bResult;
// set last error flag to success value
m_dwLastError = 0L;
// set initialization flag
m_bInitialized = TRUE;
// get specified context provider
bResult = ::CryptAcquireContext(
&this->m_hCryptProvider, // save the returned handle of CSP provider
pCryptContainer, // the appoint key container
pCryptProvider, // the CSP provider
dwProviderType, // the type of the CSP
dwFlags); // conduct a new key container
// if cannot do so then error
if (!bResult)
{
this->m_dwLastError = GetLastError();
this->m_bInitialized = FALSE;
WhatIsError(TRUE, 0, "CryptAcquireContext Error");
}
}
CEncrypt::~CEncrypt()
{
// release context provider if one acquired earlier
if (m_bInitialized)
{
::CryptReleaseContext(this->m_hCryptProvider, 0);
}
}
//*******************************************************************************
// User Initialization & Destruction
//*******************************************************************************
int CEncrypt::AddUser()
{
HCRYPTPROV hProv;
HCRYPTKEY hKey;
CHAR szUserName[100];
DWORD dwUserNameLen = 100;
int iReturnCode = ENCRYPT_SUCCESS;
// return error if not initialized
if (!m_bInitialized)
{
return(ENCRYPT_NOTINITIALIZED);
}
// Attempt to acquire a handle to the default key container.
if (!::CryptAcquireContext(
&hProv,
NULL,
MS_DEF_PROV,
PROV_RSA_FULL,
0))
{
// Some sort of error occured.
// Create default key container.
if(!::CryptAcquireContext(
&hProv,
NULL,
MS_DEF_PROV,
PROV_RSA_FULL,
CRYPT_NEWKEYSET))
{
this->m_dwLastError = GetLastError();
WhatIsError(TRUE, 0, "Error Creating key container");
return(ENCRYPT_CRYPTAPIERROR);
}
// Get name of default key container.
if(!::CryptGetProvParam(
hProv,
PP_CONTAINER,
(BYTE *)szUserName,
&dwUserNameLen,
0))
{
// Error getting key container name.
szUserName[0] = 0;
}
}
// Attempt to get handle to signature key.
if(!::CryptGetUserKey(hProv, AT_SIGNATURE, &hKey))
{
if(GetLastError() == NTE_NO_KEY)
{
// Create signature key pair.
if(!::CryptGenKey(hProv,AT_SIGNATURE,0,&hKey))
{
this->m_dwLastError = GetLastError();
WhatIsError(TRUE, 0, "CryptGenKey Error");
return(ENCRYPT_CRYPTAPIERROR);
}
else
{
::CryptDestroyKey(hKey);
}
}
else
{
this->m_dwLastError = GetLastError();
WhatIsError(TRUE, 0, "CryptGetUserKey Error");
return(ENCRYPT_CRYPTAPIERROR);
}
}
// Attempt to get handle to exchange key.
if(!::CryptGetUserKey(hProv,AT_KEYEXCHANGE,&hKey))
{
if(GetLastError()==NTE_NO_KEY)
{
// Create key exchange key pair.
if(!::CryptGenKey(hProv,AT_KEYEXCHANGE,0,&hKey))
{
this->m_dwLastError = GetLastError();
WhatIsError(TRUE, 0, "CryptGenKey Error");
return(ENCRYPT_CRYPTAPIERROR);
}
else
{
::CryptDestroyKey(hKey);
}
}
else
{
this->m_dwLastError = GetLastError();
WhatIsError(TRUE, 0, "CryptGetUserKey Error");
return(ENCRYPT_CRYPTAPIERROR);
}
}
::CryptReleaseContext(hProv,0);
return(iReturnCode);
}
BOOL CEncrypt::RemoveUser()
{
HCRYPTPROV hProv;
int iReturnCode = ENCRYPT_SUCCESS;
// Attempt to delete key set
if (!::CryptAcquireContext(
&hProv,
NULL,
MS_DEF_PROV,
PROV_RSA_FULL,
CRYPT_DELETEKEYSET))
{
this->m_dwLastError = GetLastError();
WhatIsError(TRUE, 0, "Error deleting key set");
iReturnCode = ENCRYPT_CRYPTAPIERROR;
}
return(iReturnCode);
}
//*******************************************************************************
// Encryption & decryption member functions
//*******************************************************************************
DWORD CEncrypt::RequiredBufferSize(DWORD dwBufferSize)
{
DWORD dwBufferLen = 0;
return(dwBufferLen);
}
int CEncrypt::EncryptBuffer(LPSTR pBuffer, DWORD dwSize, LPSTR pPassword)
{
int iReturnCode = ENCRYPT_SUCCESS;
// return error if not initialized
if (!m_bInitialized)
{
return(ENCRYPT_NOTINITIALIZED);
}
return(iReturnCode);
}
int CEncrypt::DecryptBuffer(LPSTR pBuffer, DWORD dwSize, LPSTR pPassword)
{
int iReturnCode = ENCRYPT_SUCCESS;
// return error if not initialized
if (!m_bInitialized)
{
return(ENCRYPT_NOTINITIALIZED);
}
return(iReturnCode);
}
int CEncrypt::EncryptFile(LPSTR pInputFile, LPSTR pOutputFile, LPSTR pPassword)
{
int iReturnCode = ENCRYPT_SUCCESS;
FILE *hSource = NULL;
FILE *hDestination = NULL;
HCRYPTKEY hKey = 0;
// return error if not initialized
if (!m_bInitialized)
{
return(ENCRYPT_NOTINITIALIZED);
}
// set last error flag to success value
this->m_dwLastError = 0L;
// Open source and destination files
if (this->OpenSrcAndDstFiles(pInputFile, &hSource, pOutputFile, &hDestination) == ENCRYPT_SUCCESS)
{
// see if password was supplied
if (pPassword == NULL)
{
// Create a random session key.
::CryptGenKey(this->m_hCryptProvider, ENCRYPT_ALGORITHM, CRYPT_EXPORTABLE, &hKey);
// export key to file
this->PutKeyBlobToFile(hDestination, hKey);
}
else
{
// Derive a session key from the hashed password
hKey = this->GetKeyFromHashedPassword(this->m_hCryptProvider, pPassword);
}
// encrypt source file into destionation file
ProcessSrcAndDstFiles(hKey, FALSE, &hSource, &hDestination);
}
// Close files.
CloseSrcAndDstFiles(&hSource, &hDestination);
// Destroy session key.
::CryptDestroyKey(hKey);
return(iReturnCode);
}
int CEncrypt::DecryptFile(LPSTR pInputFile, LPSTR pOutputFile, LPSTR pPassword)
{
int iReturnCode = ENCRYPT_SUCCESS;
FILE *hSource = NULL;
FILE *hDestination = NULL;
HCRYPTKEY hKey = 0;
// return error if not initialized
if (!m_bInitialized)
{
return(ENCRYPT_NOTINITIALIZED);
}
// set last error flag to success value
this->m_dwLastError = 0L;
// Open source and destination files
if (this->OpenSrcAndDstFiles(pInputFile, &hSource, pOutputFile, &hDestination) == ENCRYPT_SUCCESS)
{
// see if password was supplied
if(pPassword == NULL)
{
// Decrypt the file with the saved session key.
hKey = this->GetKeyBlobFromFile(hSource);
}
else
{
// Decrypt the file with a session key derived from a password.
hKey = this->GetKeyFromHashedPassword(this->m_hCryptProvider, pPassword);
}
// decrypt source file into destionation file
ProcessSrcAndDstFiles(hKey, FALSE, &hSource, &hDestination);
}
// Close files.
CloseSrcAndDstFiles(&hSource, &hDestination);
// Destroy session key.
::CryptDestroyKey(hKey);
return(iReturnCode);
}
//*******************************************************************************
// Key & Password member functions
//*******************************************************************************
HCRYPTKEY CEncrypt::GetKeyFromHashedPassword(HCRYPTPROV hProv, LPSTR pPassword)
{
HCRYPTKEY hKey = (HCRYPTKEY)NULL;
HCRYPTKEY hHash;
// Create a hash object.
if (::CryptCreateHash(hProv, CALG_MD5, 0, 0, &hHas
- 1
- 2
- 3
前往页