473,396 Members | 1,891 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

.Net Crypto InterOp with Win32 CryptoAPI

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
*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 15 '05 #1
0 4688

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Gregory L Priem | last post by:
i have been unable to get any of the classes in the System.Security.Cryptography namespace to work on my w2k machine, so i created a vmware session starting from scratch, to remove any possible...
0
by: Kim HM | last post by:
I Have a .NET program sending mails using CDOEX. These mail I need to sign. I god a tip that i shoud use CAPICOM. That worked fine sending a mail with signature. BUT the problem is that I have...
4
by: Dave Bailey | last post by:
I have an application installed on a web server. When forst intalled it worked fine. The administrator rebooted the server and then when accessing the app the folowing error appears: CryptoAPI...
4
by: Sean Kelly | last post by:
The old one, not the .NET version. I don't see it listed, but since it's available under .NET I thought I'd ask. Also, there are some API calls which are supposedly not available under most...
3
by: Sygnosys | last post by:
Hi, I have a piece of code in .NET that encrypts a string. The .NET code is quite simple and through the last couple of days I've been trying to build it's equivalent in ATL playing arround...
4
by: john | last post by:
I'm having some Interop problems. I really need someone's help on this, i'm running out of ideas. I have upgraded my Data access controls to the latest ver. Now then i have a form with these...
9
by: Lloyd Dupont | last post by:
If I write a pure C# application, only using the standart publi API, no interop, the same binary should work well on32 bit, 64 bits and perhaps on the compact framework as well if I link against...
1
by: keanep | last post by:
Hi I'd appreciate some advice on how to access legacy C++/Win32 libraries from C#. We have a legacy Win32 application, mostly written in VC7.1, and we want to re-write the GUI and maybe some...
1
by: ropo | last post by:
I am using .NET 2.0 I have a C#.Net App, which uses a .NET Class Library, which accesses a COM object through interop. The C#.Net App also uses a mixed mode C++.Net Class library which uses an...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.