Click here to Skip to main content
15,938,218 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have two programs that need to work together encrypting and decrypting text. One, which is in C# and runs in .NET, has a simple encryption and decryption scheme. The other, which cannot run in .NET, runs in Win32 and is written in C/C++. The Win32 piece needs to encrypt data so that the .NET piece can decrypt it. I have implemented the both the .NET code and the Win32 piece using the sample code provided by Microsoft, and they work fine encrypting and decrypting data by themselves. However, whenever I try to encrypt data from Win32, the .NET is unable to decrypt it, throwing an exception as "Bad data." I have written the .NET piece using the TripleDESCryptoServiceProvider class, and the Win32 using the CryptoAPI. I am not certain just what I need to do to get equivalent results. The .NET encryption code is simple and straightforward, but I am not certain just what the "magic" is underneath:

TripleDESCryptoServiceProvider _desProvider = new TripleDESCryptoServiceProvider();
//bytes for key and initialization vector
byte[] keyBytes;
byte[] vectorBytes;

FileStream fStream = File.Open(locationOfFile, FileMode.Create, FileAccess.Write);

CryptoStream cStream = new CryptoStream(fStream,
    _desProvider.CreateEncryptor(keyBytes, vectorBytes),
    CryptoStreamMode.Write);

// Create a StreamWriter using the CryptoStream.
BinaryWriter bWriter = new BinaryWriter(cStream);
//write out encrypted data
byte[] rawData;
bWriter.Write(rawData);

What would be the equivalent code using the Crypto API? Do I need to call CryptDeriveKey(), create a hash code with the key bytes, put something into the name of the provider, import a key, etc.?

I've tried something like:
//the contents of keyBytes and vectorBytes are identical to the C# code
byte[] keyBytes;
byte[] vectorBytes;
	if(CryptAcquireContext(&hProv, NULL, MS_ENHANCED_PROV, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT))
	{
		HCRYPTKEY hKey = NULL;
                //what do I do here?
		{
			//set the IV.
			if(CryptSetKeyParam(hKey, KP_IV, vectorbytes, 0))
			{
				//encrypt the data
				BYTE filebytes[2048];
				DWORD cbEncrypted, dwSize;
				if (CryptEncrypt(hKey, NULL, TRUE, 0, filebytes, &cbEncrypted, dwSize))
				{
				}
			}
			CryptDestroyKey(hKey);
			hKey = NULL;
		}
         }

I've looked at a couple of Microsoft sites for sample code (http://msdn.microsoft.com/en-us/library and http://msdn.microsoft.com/en-us/library/aa382358(VS.85).aspx[^]), but I haven't been able to get it to work with the .NET decryption. What am I doing wrong?
Posted
Updated 25-May-10 5:01am
v2
Comments
OriginalGriff 25-May-10 11:05am    
I wouldn't expect them to call each other - just to exist in a separate DLL. You can then access the encrypt from win32, and the decrypt from .NET
Member 3966780 25-May-10 13:55pm    
Again, that would be a great idea, but for reasons I won't bore you with here that unfortunately is not an option here.

1 solution

I would be tempted to take the safest route: write an encrypt / decyrpt method pair in C++ and contain it in a DLL. You can then access the matched routines from either managed or unmanaged code, knowing they will work.
 
Share this answer
 
Comments
Member 3966780 25-May-10 11:02am    
Alas, that would be a great idea, but I am unable to go down that path. The two methods have to be separate and cannot call each other.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900