.Net Crypto InterOp with Win32 CryptoAPI

C

Cory Baker

Hi all!

I am trying to encrypt data with C# and decrypt the same data using VC++
6, both using a password. I believe that I am very close on this one
but am currently stuck and am receiving error 2148073477 on the VC++
CryptDecrypt() call. My keys generated by C# and VC++ are the same, so
that seems to be okay.

My C# code is as follows:

// C# Begin *********************************
string s = "Hello";
RC2CryptoServiceProvider sp;
MemoryStream ms;
byte[] toEncrypt;
byte[] fromEncrypt;
byte[] IV = new byte[8];
byte[] key;


sp = new RC2CryptoServiceProvider();
sp.Mode = CipherMode.CBC;
sp.KeySize = 40;
sp.EffectiveKeySize = 40;

ms = new MemoryStream();

// Create the key
PasswordDeriveBytes pdb = new PasswordDeriveBytes(
"ABCD-1234-XXXX-0000", IV );
key = pdb.CryptDeriveKey( "RC2", "MD5", 40, IV );
sp.Key = key;

// Create the crypto stream
CryptoStream cs = new CryptoStream( ms, sp.CreateEncryptor(),
CryptoStreamMode.Write );

// Encrypt the data
toEncrypt = Encoding.UTF8.GetBytes( s );
cs.Write( toEncrypt, 0, toEncrypt.Length );
cs.FlushFinalBlock();
ms.Position = 0;
fromEncrypt = new byte[ms.Length];
ms.Read( fromEncrypt, 0, (int)ms.Length );

// Cleanup
cs.Close();
ms.Close();

// C# End *********************************

My VC is as follows:

The VC code contains a byte array that I populate after running the C#
code and stepping through.
It contains a call to CryptEncrypt() and CryptDecrypt() to prove that
encryption is working with the generated key.
I also exported the key to ensure that it was identical to the C# key.

// VC Begin *********************************
HCRYPTPROV hCryptProv;
HCRYPTKEY hKey;
HCRYPTHASH hHash;
CHAR szPassword[] = "ABCD-1234-XXXX-0000";
CHAR szMessage[512] = "";
DWORD dwLength;
BOOL b;
BYTE* pbKeyBlob;
BYTE bTest[] = {0x1e, 0x03, 0x91, 0xbe, 0xef, 0x2b, 0xdf, 0x91};

strcpy( szMessage, "Hello" );

b = CryptAcquireContext( &hCryptProv, NULL, MS_DEF_PROV, PROV_RSA_FULL,
0);
b = CryptCreateHash( hCryptProv, CALG_MD5, 0, 0, &hHash);

dwLength = strlen( szPassword );
b = CryptHashData( hHash, (BYTE*)szPassword, dwLength, 0);
b = CryptDeriveKey( hCryptProv, CALG_RC2, hHash, CRYPT_EXPORTABLE,
&hKey);

dwLength = strlen( szMessage );
b = CryptEncrypt( hKey, 0, TRUE, 0, (BYTE*)szMessage, &dwLength, 512 );
b = CryptDecrypt( hKey, 0, TRUE, 0, (BYTE*)szMessage, &dwLength );

// The call that FAILS!
dwLength = 8;
b = CryptDecrypt( hKey, 0, TRUE, 0, bTest, &dwLength );
DWORD dw = GetLastError();

b = CryptExportKey( hKey, NULL, PLAINTEXTKEYBLOB, 0, NULL, &dwLength);
pbKeyBlob = (BYTE*)malloc(dwLength);
b = CryptExportKey( hKey, NULL, PLAINTEXTKEYBLOB, 0, pbKeyBlob,
&dwLength);
free( pbKeyBlob );

b = CryptDestroyHash( hHash );
b = CryptDestroyKey( hKey );
b = CryptReleaseContext( hCryptProv, 0 );

// VC End *********************************


Is there something I'm missing?

Thanks
Cory Baker
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top