Key derivation in c++ and VB.NET
From: Howard Postley (HowardPostley_at_discussions.microsoft.com)
Date: 02/12/05
- Previous message: mcasthana: "ASP.NET web app, Win2003, & Active Directory"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ] [ attachment ]
Date: Fri, 11 Feb 2005 19:37:02 -0800
I am trying to encrypt data in c++ and decrypt in VB.NET. I find that the
System.Cryptography classes don't align exactly with the CryptoAPI
functionality that is easily accessed from c++.
I believe that the problem I'm having is deriving the same key on the .NET
side as on the c++ side.
The c++ code (which encrypts and decrypts fine) looks like:
// Get handle to user default provider.
if (CryptAcquireContext(&hProv, NULL, NULL, PROV_RSA_FULL, 0))
{
// Create hash object.
if (CryptCreateHash(hProv, CALG_SHA1, 0, 0, &hHash))
{
// Hash password string.
if (CryptHashData(hHash, pbKey, cbKey, 0))
{
// Create block cipher session key based on hash of the password.
if (CryptDeriveKey(hProv, CALG_3DES, hHash, CRYPT_EXPORTABLE, &hKey))
{
// Allocate memory.
pbBuffer = (LPBYTE)malloc(*pcbCipherText);
if (pbBuffer != NULL)
{
memcpy(pbBuffer, pbPlainText, cbPlainText);
cbBuffer = *pcbCipherText;
*pcbCipherText = cbPlainText;
// Encrypt data
if (CryptEncrypt(hKey, 0, TRUE, 0, pbBuffer, pcbCipherText,
cbBuffer))
{
The VP.NET code (where the problem is):
Dim ivNull() As Byte = {0, 0, 0, 0, 0, 0, 0, 0}
Dim csp As New TripleDESCryptoServiceProvider ' CSP context
Dim bHash As PasswordDeriveBytes
Dim outData As New MemoryStream
Dim Cipher As CryptoStream
Dim transform As ICryptoTransform
bHash = New PasswordDeriveBytes(ToString(Key), ivNull)
csp.Key = bHash.CryptDeriveKey("TripleDES", "SHA1", 0, ivNull)
csp.IV = ivNull
Cipher = New CryptoStream(outData, _
csp.CreateDecryptor(), _
CryptoStreamMode.Write)
Cipher.Write(inData, 0, inData.Length)
Cipher.Close()
The VB code throws an exception on the last (close) statement. I believe
that the reason is that the derived key for decryption doesn't match the one
that was used for encryption.
Any suggestions?
Thanks,
</hjp>
- Previous message: mcasthana: "ASP.NET web app, Win2003, & Active Directory"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ] [ attachment ]
Relevant Pages
|