G
Guest
well, I am using the cryptoapi to encrypt the data before sending it using
GPRS to backoffice in my Pocket PC application. And sometimes it throws an
Exception and Application crashes.
System Exception CryptAcquireContext Failed.
last error: 0x80000005
Error Message: N/A.
I am using the following code to encrypt the data.
static public byte[] Encrypt(string passphrase, byte[] data, int offset,
int count)
{
// holds encrypted 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 passphrase
hKey = GetCryptoKey(hProv, passphrase);
// determine how large of a buffer is required
// to hold the encrypted data
uint dataLength = (uint)count;
uint bufLength = (uint)count;
if (!WinApi.CryptEncrypt(hKey, IntPtr.Zero, true,
0, null, ref dataLength, bufLength))
Failed("CryptEncrypt");
// allocate and fill buffer with encrypted data
buffer = new byte[dataLength];
Buffer.BlockCopy(data, offset, buffer, 0, count);
dataLength = (uint)count;
bufLength = (uint)buffer.Length;
if (!WinApi.CryptEncrypt(hKey, IntPtr.Zero, true,
0, buffer, ref dataLength, bufLength))
Failed("CryptEncrypt");
}
finally
{
// release crypto handles
if (hKey != IntPtr.Zero)
WinApi.CryptDestroyKey(hKey);
if (hProv != IntPtr.Zero)
WinApi.CryptReleaseContext(hProv, 0);
}
return buffer;
}
GPRS to backoffice in my Pocket PC application. And sometimes it throws an
Exception and Application crashes.
System Exception CryptAcquireContext Failed.
last error: 0x80000005
Error Message: N/A.
I am using the following code to encrypt the data.
static public byte[] Encrypt(string passphrase, byte[] data, int offset,
int count)
{
// holds encrypted 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 passphrase
hKey = GetCryptoKey(hProv, passphrase);
// determine how large of a buffer is required
// to hold the encrypted data
uint dataLength = (uint)count;
uint bufLength = (uint)count;
if (!WinApi.CryptEncrypt(hKey, IntPtr.Zero, true,
0, null, ref dataLength, bufLength))
Failed("CryptEncrypt");
// allocate and fill buffer with encrypted data
buffer = new byte[dataLength];
Buffer.BlockCopy(data, offset, buffer, 0, count);
dataLength = (uint)count;
bufLength = (uint)buffer.Length;
if (!WinApi.CryptEncrypt(hKey, IntPtr.Zero, true,
0, buffer, ref dataLength, bufLength))
Failed("CryptEncrypt");
}
finally
{
// release crypto handles
if (hKey != IntPtr.Zero)
WinApi.CryptDestroyKey(hKey);
if (hProv != IntPtr.Zero)
WinApi.CryptReleaseContext(hProv, 0);
}
return buffer;
}