We use software that hashes passwords with the algorithm below but I have
been unable to duplicate the results with VB.NET, I'm not even sure that it
is possible. I can get a consistent hash back but it's not the same as what
this code produces (I'm told it is Microsoft C++ using standard MS SDK
wincrypt calls). I need to achieve with vb.net the same result as the code
below which encodes a password, I don't need to decrypt it.
example: SPICE66 becomes 73325957415a9c28e6885a72133a21fa
the code that was originally used was:
#include <stdio.h>
#include <windows.h>
#include <wincrypt.h>
DWORD encode( char *pString, char *pcResult )
{
HCRYPTPROV hProv;
BYTE *pbBuffer= (BYTE *)pString;
DWORD dwBufferLen = strlen( (char *)pbBuffer )+1;
HCRYPTHASH hHash;
DWORD result;
//--------------------------------------------------------------------
// Acquire a cryptographic provider context handle.
result = getDefaultProvider( &hProv );
if ( result != NO_ERROR )
{
return( result );
}
//--------------------------------------------------------------------
// Create the hash object.
if ( CryptCreateHash( hProv, CALG_MD5, 0, 0, &hHash ) == FALSE )
{
//errmsg = "Error during CryptCreateHash.";
return( GetLastError() );
}
//--------------------------------------------------------------------
// Compute the cryptographic hash of the buffer.
if ( CryptHashData( hHash, pbBuffer, dwBufferLen, 0 ) == TRUE )
{
result = getReadableHex( hHash, pcResult );
if ( result != NO_ERROR )
{
return( result );
}
}
else
{
//errmsg = "Error during CryptHashData.";
return( GetLastError() );
}
//--------------------------------------------------------------------
// Destroy the hash object.
if ( hHash )
{
CryptDestroyHash( hHash );
}
//--------------------------------------------------------------------
// Release the provider handle.
if ( hProv )
{
CryptReleaseContext( hProv, 0 );
}
return( NO_ERROR );
}
DWORD getDefaultProvider( HCRYPTPROV * phCryptProv )
{
//--------------------------------------------------------------------
// Declare and initialize variables.
HCRYPTKEY hKey; // public/private key handle
// CHAR szUserName[100]; // buffer to hold the name
// of the key container
// DWORD dwUserNameLen = 100; // length of the buffer
// LPCSTR UserName = "Ascend"; // optionally enter the user's
name here
// to be used as the key container
// name (limited to 100 characters)
//--------------------------------------------------------------------
// Begin processing. Attempt to acquire a context with a default
key
// container.
// To create a new key container, substitute a string for the
// NULL second parameter here and in the next call to
// CryptAcquireContext.
// if ( CryptAcquireContext(
// phCryptProv, // handle to the CSP
// UserName, // container name
// NULL, // use the default provider
// PROV_RSA_FULL, // provider type
// 0 ) == FALSE ) // flag values
if ( CryptAcquireContext(
phCryptProv, // handle to the CSP
NULL, // container name
NULL, // use the default provider
PROV_RSA_FULL, // provider type
CRYPT_VERIFYCONTEXT ) == FALSE ) // flag
values
{
return( GetLastError() );
//--------------------------------------------------------------------
// Some sort of error occurred in acquiring the
context.
// Create a new default key container.
// if ( CryptAcquireContext(
// phCryptProv,
// UserName,
// NULL,
// PROV_RSA_FULL,
// CRYPT_NEWKEYSET ) == FALSE )
// {
// //errmsg = "Could not create a new key
container.\n";
// return( GetLastError() );
// }
}
//--------------------------------------------------------------------
// A cryptographic context with a key container is available.
Get the
// name of the key container.
// if ( CryptGetProvParam(
// *phCryptProv, // handle to the CSP
// PP_CONTAINER, // get the key
container name
// (BYTE *)szUserName, // pointer to the key
container name
// &dwUserNameLen, // length of name,
preset to 100
// 0 ) == FALSE )
// {
// // An error occurred while getting the key container
name.
// //errmsg = "A context was acquired or created, but
an error occurred getting the key container name.\n";
// return( GetLastError() );
// }
return( NO_ERROR );
}
bradbury9 - 31 Oct 2005 17:26 GMT
CryptCreateHash( hProv, CALG_MD5, 0, 0, &hHash )
I think the algoritm you want to use to obtain the hash is MD5. You
should take a look at MD5CryptoServiceProvider class. There are other
algorithms but I have not used them, should be a easy thing.
It accepts and returns byte arrays that can ce converted using
System.Text.Encoding.XXX.getBytes(string str) methods.
Hope it helps.