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

📄 aesencregkey.cpp

📁 AES算法
💻 CPP
📖 第 1 页 / 共 2 页
字号:
    if( ERROR_SUCCESS != lResult ) { goto FINISHED; }

    //
    // Query for needed buffer size
    lResult = RegQueryValueEx( hKey, _szValueName, 0, NULL, NULL, &dwSize );

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

    //
    // Add '+ sizeof(TCHAR)' in case the string is not
    //   stored in the registry with a trailing '\0'
    pcbData = new BYTE[ dwSize + sizeof(TCHAR) ];

    //
    // Sanity Check
    if( NULL == pcbData ) {        
        lResult = ERROR_NOT_ENOUGH_MEMORY;
        goto FINISHED;
    }

    //
    // Add the trailing '\0', taking into account UNICODEness
    reinterpret_cast<TCHAR*>(pcbData)[ dwSize/sizeof(TCHAR) - 1 ] = _T('\0'); 

    //
    // Query for the actual value
    lResult = RegQueryValueEx( hKey, _szValueName, 0, NULL, pcbData, &dwSize );

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

    //
    // Paydirt...
    szValue = reinterpret_cast<TCHAR*>(pcbData);

FINISHED:

    //
    // Cleanup...
    if( NULL != hKey ) { RegCloseKey( hKey ); }

    if( NULL != pcbData ) { delete[] pcbData; }

    return lResult;
}

LONG CAESEncRegKey::ReadEncString(CString &szValue) const
{
    LONG lResult = ERROR_SUCCESS;

    // Returned from the Registry
    DWORD    dwSize    = 0;
    BYTE*    pcbData = NULL;

    // Returned from DecryptData(...)
    DWORD    dwDecryptedSize     = 0;
    BYTE*   pcbDecryptedData = NULL;
  
    // Read From the Registry
    lResult = ReadData( &pcbData, &dwSize );

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

    //
    // Undo it...
    lResult = DecryptData( pcbData, dwSize, &pcbDecryptedData, &dwDecryptedSize );

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

    //
    // Paydirt...
    szValue = reinterpret_cast<const TCHAR*>( pcbDecryptedData );

FINISHED:

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

    if( NULL != pcbDecryptedData ) { delete[] pcbDecryptedData; }

    return lResult;
}

////////////////////////////////
//
// EncryptData
//
LONG CAESEncRegKey::EncryptData(const BYTE *pcbPlainText, DWORD dwPlainTextSize, BYTE **pcbEncryptedData, DWORD *pdwEncryptedSize) const
{
    LONG lResult = ERROR_SUCCESS;

    ASSERT( NULL != pcbPlainText );
    ASSERT( NULL != pcbEncryptedData );
    ASSERT( NULL != pdwEncryptedSize );

    if( NULL == pcbEncryptedData || NULL == pdwEncryptedSize ) {

        lResult = ERROR_INVALID_PARAMETER;
    }

    try {

        CryptoPP::AES::Encryption aesEncryption( _EncKey._cbKey, CryptoPP::AES::DEFAULT_KEYLENGTH);
        CryptoPP::CBC_Mode_ExternalCipher::Encryption cbcEncryption( aesEncryption, _EncIV._cbIV );

        std::string cipher;

        CryptoPP::StreamTransformationFilter stfEncryptor(cbcEncryption, new CryptoPP::StringSink( cipher ) );
        stfEncryptor.Put( reinterpret_cast<const BYTE*>( pcbPlainText ), dwPlainTextSize );
        stfEncryptor.MessageEnd();

        if( ERROR_SUCCESS == lResult ) {
        
            *pdwEncryptedSize = cipher.size();
        
            *pcbEncryptedData = new BYTE[ *pdwEncryptedSize ];

        
            ::memcpy( *pcbEncryptedData, reinterpret_cast<const BYTE*>( cipher.data() ), *pdwEncryptedSize );
        }

    } catch( CryptoPP::Exception& e ) {

        ::OutputDebugString( _T("Caught Crypto++ exception: '") );
        ::OutputDebugStringA( e.what() );
        ::OutputDebugString( _T("'\n") );

        lResult = ERROR_INVALID_DATA;
    
    } catch( ... ) {

        ::OutputDebugString( _T("Caught other exception.") );

        lResult = ERROR_INVALID_DATA;
    }

    return lResult;
}

////////////////////////////////
//
// DecryptData
//
LONG CAESEncRegKey::DecryptData(const BYTE *pcbEncryptedData, DWORD dwEncryptedSize, BYTE **pcbDecryptedData, DWORD *pdwDecryptedSize) const
{
    LONG lResult = ERROR_SUCCESS;

    *pcbDecryptedData = NULL;
    *pdwDecryptedSize = 0;

    try {

        std::string decrypted;
        CryptoPP::AES::Decryption aesDecryption( _EncKey._cbKey, CryptoPP::AES::DEFAULT_KEYLENGTH);
        CryptoPP::CBC_Mode_ExternalCipher::Decryption cbcDecryption( aesDecryption, _EncIV._cbIV );
        CryptoPP::StreamTransformationFilter stfDecryptor(cbcDecryption, new CryptoPP::StringSink( decrypted ) );

        //
        // Push our blob into the decryptor
        stfDecryptor.Put( pcbEncryptedData, dwEncryptedSize );
        stfDecryptor.MessageEnd();

        *pcbDecryptedData = new BYTE[ decrypted.size() ];

        if( NULL != *pcbDecryptedData ) {

            *pdwDecryptedSize = decrypted.size();

            ::memcpy( *pcbDecryptedData, decrypted.data(), decrypted.size() );
        }
    }

    catch( CryptoPP::Exception& e ) {

        lResult = ERROR_INVALID_DATA;

        ::OutputDebugString( _T("Caught Crypto++ exception: '") );
        ::OutputDebugStringA( e.what() );
        ::OutputDebugString( _T("'\n") );
    }

    catch( ... ) {

        lResult = ERROR_INVALID_DATA;

        ::OutputDebugString( _T("Caught other exception") );
    }

    return lResult;
}

LONG CAESEncRegKey::ReadData(BYTE **pcbData, DWORD *pdwSize) const
{
    LONG    lResult = ERROR_SUCCESS;
    HKEY    hKey    = NULL;

    ASSERT( NULL != pcbData && NULL != pdwSize );

    //
    // Sanity Check
    if( NULL == pcbData || NULL == pdwSize ) {        
        lResult = ERROR_INVALID_PARAMETER;
        goto FINISHED;
    }

    //
    // Open the Key
    lResult = RegOpenKeyEx( _hKey, _szSubKey, 0, KEY_READ, &hKey );

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

    //
    // Query for needed buffer size
    lResult = RegQueryValueEx( hKey, _szValueName, 0, NULL, NULL, pdwSize );

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

    //
    // Allocate a buffer
    *pcbData = new BYTE[ *pdwSize + sizeof(TCHAR) ];

    //
    // Sanity Check
    if( NULL == *pcbData ) {
        
        lResult = ERROR_NOT_ENOUGH_MEMORY;
        goto FINISHED;
    }

    //
    // Query for the actual value
    lResult = RegQueryValueEx( hKey, _szValueName, 0, NULL, *pcbData, pdwSize );

FINISHED:

    if( NULL != hKey ) { RegCloseKey( hKey ); }

    return lResult;
}

LONG CAESEncRegKey::ReadEncDWORD(DWORD &dwValue) const
{
    LONG lResult = ERROR_SUCCESS;

    // Returned from the Registry by way of ReadData(...)
    DWORD    dwSize    = 0;
    BYTE*    pcbData = NULL;

    // Returned from DecryptData(...)
    DWORD    dwDecryptedSize     = 0;
    BYTE*   pcbDecryptedData = NULL;
  
    // Read From the Registry
    lResult = ReadData( &pcbData, &dwSize );

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

    //
    // Undo it...
    lResult = DecryptData( pcbData, dwSize, &pcbDecryptedData, &dwDecryptedSize );

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

    //
    // Paydirt...
    dwValue = *( reinterpret_cast<DWORD*>( pcbDecryptedData ) );

FINISHED:

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

    if( NULL != pcbDecryptedData ) { delete[] pcbDecryptedData; }

    return lResult;
}

LONG CAESEncRegKey::ReadNonEncDWORD(DWORD& dwValue) const
{
    LONG    lResult = ERROR_SUCCESS;
    HKEY    hKey    = NULL;
    DWORD   dwSize  = sizeof( DWORD );

    //
    // Open the HKey
    lResult = RegOpenKeyEx( _hKey, _szSubKey, 0, KEY_READ, &hKey );

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

    //
    // Query for the actual value
    lResult = RegQueryValueEx( hKey, _szValueName, 0, NULL, 
                               reinterpret_cast<BYTE*>(&dwValue), &dwSize );

FINISHED:

    //
    // Cleanup...
    if( NULL != hKey ) { RegCloseKey( hKey ); }

    return lResult;
}

LONG CAESEncRegKey::ReadBinary( BYTE* pcbData, DWORD* dwSize, BOOL bEncrypted /*=FALSE*/) const
{
    LONG lResult = ERROR_SUCCESS;

    if( TRUE == bEncrypted ) {

        lResult = ReadEncBinary( pcbData, dwSize );

    } else {

        lResult = ReadNonEncBinary( pcbData, dwSize );
    }

    return lResult;
}

LONG CAESEncRegKey::ReadNonEncBinary(BYTE* pcbData, DWORD* dwSize) const
{
    LONG    lResult = ERROR_SUCCESS;
    HKEY    hKey    = NULL;

    //
    // Open the HKey
    lResult = RegOpenKeyEx( _hKey, _szSubKey, 0, KEY_READ, &hKey );

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

    //
    // Query for the actual value
    lResult = RegQueryValueEx( hKey, _szValueName, 0, NULL, 
                               reinterpret_cast<BYTE*>(pcbData), dwSize );

FINISHED:

    //
    // Cleanup...
    if( NULL != hKey ) { RegCloseKey( hKey ); }

    return lResult;
}

LONG CAESEncRegKey::ReadEncBinary(BYTE* pcbData, DWORD* dwSize) const
{
    LONG lResult = ERROR_SUCCESS;

    //
    // Returned from ReadData(...)
    DWORD dwRegistrySize  = 0;
    BYTE* pcbRegistryData = NULL;

    //
    // Returned from DecryptData(...)
    DWORD dwDecryptedSize  = 0;
    BYTE* pcbDecryptedData = NULL;
  
    //
    // Read From the Registry
    lResult = ReadData( &pcbRegistryData, &dwRegistrySize );

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

    //
    // Just undo it...
    lResult = DecryptData( pcbRegistryData, dwRegistrySize, &pcbDecryptedData, &dwDecryptedSize );

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

    //
    // Emulate the RegQueryValueEx(...) Function
    //   See http://msdn.microsoft.com/library/default.asp?
    //       url=/library/en-us/sysinfo/base/regqueryvalueex.asp
    if( *dwSize < dwDecryptedSize && NULL != pcbData ) {

        lResult = ERROR_MORE_DATA;
        *dwSize = dwDecryptedSize;

        goto FINISHED;
    }

    //
    // Emulate the RegQueryValueEx(...) Function
    //   See http://msdn.microsoft.com/library/default.asp?
    //       url=/library/en-us/sysinfo/base/regqueryvalueex.asp
    if( *dwSize < dwDecryptedSize && NULL == pcbData ) {

        lResult = ERROR_SUCCESS;
        *dwSize = dwDecryptedSize;

        goto FINISHED;
    }

    //
    // Inform Caller of size of pcbData
    *dwSize = dwDecryptedSize;

    //
    // Copy the Data out
    ::memcpy( pcbData, pcbDecryptedData, dwDecryptedSize );

    lResult = ERROR_SUCCESS;

FINISHED:

    if( NULL != pcbRegistryData )  { delete[] pcbRegistryData; }

    if( NULL != pcbDecryptedData ) { delete[] pcbDecryptedData; }

    return lResult;
}


LONG CAESEncRegKey::WriteData( const BYTE *pcbData, UINT nSize, DWORD dwType /*=REG_BINARY*/) const
{
    LONG lResult = ERROR_SUCCESS;
    HKEY hKey    = NULL;

    //
    // When writing, create the Key if it does not exist
    lResult = RegCreateKeyEx( _hKey, _szSubKey, 0, NULL,
                              REG_OPTION_NON_VOLATILE,
                              KEY_WRITE, NULL, &hKey, NULL );

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

    //
    // Paydirt
    lResult = RegSetValueEx( hKey, _szValueName, 0, dwType, 
                             reinterpret_cast<const BYTE*>( pcbData ), nSize );

FINISHED:

    //
    // Cleanup...
    if( NULL != hKey ) { RegCloseKey( hKey ); }

    return lResult;
}

⌨️ 快捷键说明

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