I am getting the error: CryptAcquireContext failed. 0x80000005

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;
}
 
G

Guest

This happens to me on Windows Mobile 5.0. Any body have a solution.

I am using the sample that was provided at:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnnetcomp/html/PPCSignatureApp.asp

my guess is the dll being used has changed or the service is available only
for signed applications. Can any one confirm or deny this?

Thanks much,
DPC

Sergey Bogdanov said:
Is it Pocket PC 2002 or Pocket PC 2003?


--
Sergey Bogdanov [.NET CF MVP, MCSD]
http://www.sergeybogdanov.com

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;
}
 
G

Guest

To give a little more context. The same code was working fine on PPC2000,
2002 and 2003 versions but stopped working with Windows Mobile 5.

Relevant sections of the code are below.

Private Const CryptDll As String = "coredll.dll"


<DllImport(CryptDll)> _
Public Shared Function CryptAcquireContext( _
ByRef phProv As IntPtr, ByVal pszContainer As String, _
ByVal pszProvider As String, ByVal dwProvType As Integer, _
ByVal dwFlags As Integer) As Boolean
End Function

If Not WinApi.CryptAcquireContext(hProv, Nothing, WinApi.MS_DEF_PROV,
WinApi.PROV_RSA_FULL, WinApi.CRYPT_VERIFYCONTEXT) Then
Failed("CryptAcquireContext")
End If

Thanks for any help or insight someone can provide
DPC

dpcbabu said:
This happens to me on Windows Mobile 5.0. Any body have a solution.

I am using the sample that was provided at:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnnetcomp/html/PPCSignatureApp.asp

my guess is the dll being used has changed or the service is available only
for signed applications. Can any one confirm or deny this?

Thanks much,
DPC

Sergey Bogdanov said:
Is it Pocket PC 2002 or Pocket PC 2003?


--
Sergey Bogdanov [.NET CF MVP, MCSD]
http://www.sergeybogdanov.com

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

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