CryptEncrypt忽略HASH?

时间:2014-11-08 01:12:01

标签: c windows cryptography

我正在尝试创建一个使用AES 256加密的应用程序。不幸的是,我无法让它发挥作用。也许我没有完全理解crpytographic逻辑。

所以它正在工作,但据我所知,哈希包含密码。但如果我更改密码输出是相同的。所以似乎CryptEncrypt忽略了哈希。

有人可以帮助我吗?

提前致谢!

这是我的代码:

 if ( !CryptAcquireContext( &hProv, NULL, MS_ENH_RSA_AES_PROV, PROV_RSA_AES, CRYPT_VERIFYCONTEXT ) )
 {   
            printf( "\nCryptAcquireContext failed %d", GetLastError() );   
            return;    
 }
if ( !CryptImportKey( hProv, ( const LPBYTE )&keyBlob, sizeof( keyBlob ), 0, CRYPT_EXPORTABLE, &hPubKey ) )
{
    printf( "\nCryptImportKey failed %d", GetLastError() );
    return;
}

if ( !CryptCreateHash( hProv, CALG_SHA_256, 0, 0, &hHash ) )
{
    printf( "Error %x during CryptCreateHash!\n", GetLastError() );
    return;
}

if ( !CryptHashData( hHash, ( PBYTE )wszPassword, cbPassword, 0 ) )
{
    printf( "Error %x during CryptHashData!\n", GetLastError() );
    return;
}

//if ( !CryptDeriveKey( hProv, CALG_AES_256, hHash, CRYPT_EXPORTABLE, &hKey ) )//hKey
//{
//  printf( "Error %x during CryptDeriveKey!\n", GetLastError() );
//  return;
//}

if ( !CryptSetKeyParam( hPubKey, KP_IV, ivData, 0 ) )
{
    printf( "\nCryptSetKeyParam failed %d", GetLastError() );
    return;
}

DWORD size = ( DWORD )strlen( StringToEncrypt ) / sizeof( char );
//printf( "\nLength of string to encrypt = %d\n\n", size );
if ( !CryptEncrypt( hPubKey, hHash, TRUE, 0, ( LPBYTE )StringToEncrypt, &size, ( size / 16 + 1 ) * 16 ) )
{
    printf( "\nCryptEncrypt failed %d", GetLastError() );
    int a = GetLastError();
    switch ( a )
    {
        case NTE_BAD_HASH_STATE: printf( "NTE_BAD_HASH_STATE" ); break;
        case NTE_BAD_HASH: printf( "NTE_BAD_HASH" ); break;
    }
    return;
}

1 个答案:

答案 0 :(得分:0)

"密码" - 通常称为密钥 - 位于hPubKey中。 hHash用于在加密完成的同时生成哈希。查看here了解更多信息。