Problem CryptDecrypt

G

Guest

Hi,
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;

try
{
// 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,
WinApi.PROV_RSA_FULL, WinApi.CRYPT_VERIFYCONTEXT))
Failed("CryptAcquireContext");

// 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))
Failed("CryptDecrypt");

// 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);
}

finally
{
// release crypto handles
if (hKey != IntPtr.Zero)
WinApi.CryptDestroyKey(hKey);
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
point:
if (!WinApi.CryptDecrypt(hKey, IntPtr.Zero, 1, 0, dataCopy, ref
dataLength))
This same order works with the normal framework as well as in a eVC version.
Can anyone help?
Kind Regards,
Steffan Wullems
 
S

S.Wullems

Hi,
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

Top