⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 aesencregkey.cpp

📁 AES算法
💻 CPP
📖 第 1 页 / 共 2 页
字号:
// AESEncRegKey.cpp : implementation file
//

#include "stdafx.h"
#include "Registry Encryption.h"
#include "AESEncRegKey.h"

// Crypto++ Includes
#pragma warning(push, 3)
#  include "modes.h"
#  include "aes.h"
#  include "filters.h"
#pragma warning(pop)

// Crypto++ Library
#ifdef _DEBUG
#  pragma comment ( lib, "cryptlibd" )
#else
#  pragma comment ( lib, "cryptlib" )
#endif

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CAESEncRegKey

CAESEncRegKey::CAESEncRegKey() : _hKey( NULL )
{
    CString szCompanyName = AfxGetAppName();

    CString szSubKey = _T("Software\\") + szCompanyName;

    SetSubKey( szSubKey );
}

CAESEncRegKey::CAESEncRegKey(const BYTE* cbKey, UINT nKeyLength, const BYTE* cbIV, UINT nIVLength) : _hKey( NULL )
{
    ASSERT( NULL != cbKey );
    ASSERT( NULL != cbIV );

    SetHKEY( NULL );

    SetKey( cbKey, nKeyLength );

    SetIV( cbIV, nIVLength );
}

CAESEncRegKey::CAESEncRegKey(HKEY hKey, LPCTSTR pszSubKey, LPCTSTR pszValueName)
{
    SetHKEY( hKey );

    SetSubKey( pszSubKey );

    SetValueName( pszValueName );
}

CAESEncRegKey::~CAESEncRegKey() { }

const BYTE* CAESEncRegKey::GetKey() const
{
    return _EncKey._cbKey;
}

const BYTE* CAESEncRegKey::GetIV() const
{
    return _EncIV._cbIV;
}

UINT CAESEncRegKey::GetIVLength() const
{
    return CryptoPP::AES::BLOCKSIZE;
}

UINT CAESEncRegKey::GetKeyLength() const
{
    return CryptoPP::AES::DEFAULT_KEYLENGTH;
}

const CString& CAESEncRegKey::GetSubKey() const
{
    return _szSubKey;
}

const CString& CAESEncRegKey::GetValueName() const
{
    return _szValueName;
}

BOOL CAESEncRegKey::SetHKEY(HKEY hKey) 
{
    _hKey = hKey;

    return TRUE;
}

BOOL CAESEncRegKey::SetSubKey(LPCTSTR pszSubKey)
{
    BOOL bResult = FALSE;

    ASSERT( NULL != pszSubKey );

    if( NULL != pszSubKey ) {

        CString szTemp = pszSubKey;

        // Strip leading slashes. Windows 9x can deal with them,
        //   but Windows NT/2000 cannot
        while( 0 < szTemp.GetLength() && _T('\\') == szTemp.Left( 1 ) ) {

            szTemp = szTemp.Right( szTemp.GetLength() - 1 );
        }
    
        _szSubKey = szTemp;

        bResult = TRUE;

        ::OutputDebugString( _T("_szSubKey set to ") );
        ::OutputDebugString( _szSubKey );
        ::OutputDebugString( _T("\n") );
    }

    return bResult;
}

BOOL CAESEncRegKey::SetValueName(LPCTSTR pszValueName)
{
    BOOL bResult = FALSE;

    ASSERT( NULL != pszValueName );

    if( NULL != pszValueName ) {
    
        _szValueName = pszValueName;

        bResult = TRUE;

        ::OutputDebugString( _T("_szValueName set to ") );
        ::OutputDebugString( _szValueName );
        ::OutputDebugString( _T("\n") );
    }

    return bResult;
}

BOOL CAESEncRegKey::SetIV(const BYTE *cbIV, UINT nLength)
{
    BOOL bResult = FALSE;

    ASSERT( CryptoPP::AES::BLOCKSIZE == nLength );
    ASSERT( NULL != cbIV );

    if( CryptoPP::AES::BLOCKSIZE == nLength && NULL != cbIV ) {
    
        _EncIV = cbIV;

        bResult = TRUE;
    }

    return bResult;
}

BOOL CAESEncRegKey::SetKey(const BYTE *cbKey, UINT nLength)
{
    BOOL bResult = FALSE;

    ASSERT( CryptoPP::AES::DEFAULT_KEYLENGTH == nLength );
    ASSERT( NULL != cbKey );

    if( CryptoPP::AES::DEFAULT_KEYLENGTH == nLength && NULL != cbKey ) {
    
        _EncKey = cbKey;

        bResult = TRUE;
    }

    return bResult;
}


LONG CAESEncRegKey::WriteString(LPCTSTR pszData, BOOL bEncrypt /*=FALSE*/) const
{
    LONG lResult = ERROR_SUCCESS;

    if( TRUE == bEncrypt ) {

        lResult = WriteEncString( pszData );

    } else {

        lResult = WriteNonEncString( pszData );
    }

    return lResult;
}

LONG CAESEncRegKey::WriteDWORD(DWORD dwValue, BOOL bEncrypt /*=FALSE*/) const
{
    LONG lResult = ERROR_SUCCESS;

    if( TRUE == bEncrypt ) {

        lResult = WriteEncDWORD( dwValue );

    } else {

        lResult = WriteNonEncDWORD( dwValue );
    }

    return lResult;
}

LONG CAESEncRegKey::WriteBinary(const BYTE *pcbData, UINT nSize, BOOL bEncrypt /*= FALSE*/) const
{
    LONG lResult = ERROR_SUCCESS;

    if( TRUE == bEncrypt ) {

        lResult = WriteEncBinary( pcbData, nSize );

    } else {

        lResult = WriteNonEncBinary( pcbData, nSize );
    }

    return lResult;
}

LONG CAESEncRegKey::WriteNonEncString(LPCTSTR pszData) const
{
    LONG lResult = ERROR_SUCCESS;

    lResult = WriteData( reinterpret_cast<const BYTE*>( pszData ),
                       ( ::lstrlen( pszData ) + 1 ) * sizeof( TCHAR ),
                       REG_SZ );

    return lResult;
}

LONG CAESEncRegKey::WriteNonEncDWORD(DWORD dwData) const
{
    LONG lResult = ERROR_SUCCESS;

    lResult = WriteData( reinterpret_cast<const BYTE*>( &dwData ),
                         sizeof( DWORD ), REG_DWORD );

    return lResult;
}


LONG CAESEncRegKey::WriteEncDWORD(DWORD dwData) const
{
    LONG lResult = ERROR_SUCCESS;

    // Returned from EncryptData()
    BYTE* pcbEncryptedData = NULL;
    DWORD dwEncryptedSize = 0;

    //
    // Anti-snoop it...
    lResult = EncryptData( reinterpret_cast<const BYTE*>(&dwData), sizeof( DWORD ), 
                           &pcbEncryptedData, &dwEncryptedSize );

    //
    // Sanity Check
    if( ERROR_SUCCESS != lResult ) { goto FINISHED; }

    //
    // Save it...
    lResult = WriteNonEncBinary( pcbEncryptedData, dwEncryptedSize );

FINISHED:

    //
    // Cleanup...
    if( NULL != pcbEncryptedData ) { delete[] pcbEncryptedData; }

    return lResult;
}

LONG CAESEncRegKey::WriteEncString(LPCTSTR pszData) const
{
    LONG lResult = ERROR_SUCCESS;

    // Returned from EncryptData()
    BYTE* pcbEncryptedData = NULL;
    DWORD dwEncryptedSize = 0;

    //
    // Anti-snoop it...
    lResult = EncryptData( reinterpret_cast<const BYTE*>(pszData),
                           ( ::lstrlen( pszData ) + 1 ) * sizeof( TCHAR ),
                           &pcbEncryptedData, &dwEncryptedSize );

    //
    // Sanity Check
    if( ERROR_SUCCESS != lResult ) { goto FINISHED; }

    //
    // Save it...
    lResult = WriteNonEncBinary( pcbEncryptedData, dwEncryptedSize );

FINISHED:

    //
    // Cleanup...
    if( NULL != pcbEncryptedData ) { delete[] pcbEncryptedData; }

    return lResult;
}

LONG CAESEncRegKey::WriteEncBinary(const BYTE *pcbData, UINT nSize) const
{
    LONG lResult = ERROR_SUCCESS;

    // Returned from EncryptData()
    BYTE* pcbEncryptedData = NULL;
    DWORD dwEncryptedSize = 0;

    //
    // Anti-snoop it...
    lResult = EncryptData( pcbData, nSize, &pcbEncryptedData, &dwEncryptedSize );

    //
    // Sanity Check
    if( ERROR_SUCCESS != lResult ) { goto FINISHED; }

    //
    // Persit it...
    lResult = WriteNonEncBinary( pcbEncryptedData, dwEncryptedSize );

FINISHED:

    //
    // Cleanup...
    if( NULL != pcbEncryptedData ) { delete[] pcbEncryptedData; }

    return lResult;
}

LONG CAESEncRegKey::WriteNonEncBinary(const BYTE *pcbData, UINT nSize) const
{
    LONG lResult = ERROR_SUCCESS;

    lResult = WriteData( pcbData, nSize, REG_BINARY );

    return lResult;
}

LONG CAESEncRegKey::ReadDWORD(DWORD &dwValue, BOOL bEncrypted) const
{
    LONG lResult = ERROR_SUCCESS;

    if( TRUE == bEncrypted ) {

        lResult = ReadEncDWORD( dwValue );

    } else {

        lResult = ReadNonEncDWORD( dwValue );
    }

    return lResult;
}

LONG CAESEncRegKey::ReadString(CString &szValue, BOOL bEncrypted /*=FALSE*/) const
{
    LONG lResult = ERROR_SUCCESS;

    if( TRUE == bEncrypted ) {

        lResult = ReadEncString( szValue );

    } else {

        lResult = ReadNonEncString( szValue );
    }

    return lResult;
}

LONG CAESEncRegKey::ReadString(LPTSTR pszValue, DWORD *dwCharCount, BOOL bEncrypted /*=FALSE*/) const
{
    LONG    lResult = ERROR_SUCCESS;
    CString szTemp;
    DWORD   dwRequiredSize = 0;     // In TCHARs, not BYTEs

    //
    // Emulate the RegQueryValue(...) Function
    //   See http://msdn.microsoft.com/library/default.asp?
    //       url=/library/en-us/sysinfo/base/regqueryvalueex.asp
    //   It appears Caller is asking for dwType
    if( NULL == pszValue && NULL == dwCharCount ) {     
        lResult = ERROR_SUCCESS;
        goto FINISHED;
    }

    if( TRUE == bEncrypted ) {

        lResult = ReadEncString( szTemp );

    } else {

        lResult = ReadNonEncString( szTemp );
    }

    //
    // Sanity Check
    if( ERROR_SUCCESS != lResult ) { goto FINISHED; }

    //
    // '+ 1' to catch the trailing '\0'
    dwRequiredSize = szTemp.GetLength() + 1;

    //
    // Emulate the RegQueryValue(...) Function
    //   See http://msdn.microsoft.com/library/default.asp?
    //       url=/library/en-us/sysinfo/base/regqueryvalueex.asp
    //   It appears Caller is asking for size of pszValue
    if( *dwCharCount < dwRequiredSize && NULL != pszValue ) {

        lResult = ERROR_MORE_DATA;
        *dwCharCount = dwRequiredSize;

        goto FINISHED;
    }

    //
    // Emulate the RegQueryValue(...) Function
    //   See http://msdn.microsoft.com/library/default.asp?
    //       url=/library/en-us/sysinfo/base/regqueryvalueex.asp
    //   Caller is asking for size of pszValue
    if( *dwCharCount < dwRequiredSize && NULL != pszValue ) {

        lResult = ERROR_SUCCESS;
        *dwCharCount = dwRequiredSize;

        goto FINISHED;
    }

    *dwCharCount = dwRequiredSize;

    ::memcpy( pszValue, static_cast<LPCTSTR>( szTemp ), dwRequiredSize * sizeof( TCHAR ) );

FINISHED:

    return lResult;
}

LONG CAESEncRegKey::ReadNonEncString(CString &szValue) const
{
    LONG    lResult = ERROR_SUCCESS;
    HKEY    hKey    = NULL;
    DWORD    dwSize    = 0;
    BYTE*    pcbData = NULL;

    //
    // Open the key - do not create
    lResult = RegOpenKeyEx( _hKey, _szSubKey, 0, KEY_READ, &hKey );

    //
    // Sanity Check

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -