Problem CryptDecrypt



I am using the CryptDecrypt function 3 times in a row. the result of the
first i use as input on the 3 CryptDecrypt as follows:
// Result1 decrypte
byte[] m_abResult1 = Crypto.Decrypt(m_strInnerKey, abEncResult1);
// Result2 decrypte
byte[] m_abResult1 = Crypto.Decrypt(m_strInnerKey, abEncResult2);
// Result1 decrypte
byte[] m_abResult3 = Crypto.Decrypt(m_strInnerKey2, m_abResult1);

The Function Called:
static public byte[] Decrypt(string passphrase, byte[] data)
// make a copy of the encrypted data
byte[] dataCopy = data.Clone() as byte[];

// holds the decrypted data
byte[] buffer = null;

// crypto handles
IntPtr hProv = IntPtr.Zero;
IntPtr hKey = IntPtr.Zero;

// get crypto provider, specify the provider (3rd argument)
// instead of using default to ensure the same provider is
// used on client and server
if (!WinApi.CryptAcquireContext(ref hProv, null, WinApi.MS_DEF_PROV,

// generate encryption key from the passphrase
hKey = GetCryptoKey(hProv, passphrase);
// decrypt the data
uint dataLength = (uint)dataCopy.Length;
if (!WinApi.CryptDecrypt(hKey, IntPtr.Zero, 1, 0, dataCopy, ref dataLength))

// copy to a buffer that is returned to the caller
// the decrypted data size might be less then
// the encrypted size
buffer = new byte[dataLength];
Buffer.BlockCopy(dataCopy, 0, buffer, 0, (int)dataLength);

// release crypto handles
if (hKey != IntPtr.Zero)
if (hProv != IntPtr.Zero)
WinApi.CryptReleaseContext(hProv, 0);

return buffer;

My problem is that what ever i try it crashes with a pointer error @ this
if (!WinApi.CryptDecrypt(hKey, IntPtr.Zero, 1, 0, dataCopy, ref
This same order works with the normal framework as well as in a eVC version.
Can anyone help?
Kind Regards,
Steffan Wullems


Thanks Mark and Geoff.
Well i was aware of the sample. And as long as i only encrypte or decrypt a
value 1 time it works, but every second encrypt or decrypte on a result will
cause a pointer error. Due to a specific needs i need to encrypt/decrypt the
result again.So how do i solve it.
In eVC it works but i want it in C#.
Kind Regards

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