PC Review


Reply
Thread Tools Rate Thread

I am getting the error: CryptAcquireContext failed. 0x80000005

 
 
=?Utf-8?B?QW1pdA==?=
Guest
Posts: n/a
 
      23rd Sep 2005
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;
}

 
Reply With Quote
 
 
 
 
Sergey Bogdanov
Guest
Posts: n/a
 
      23rd Sep 2005
Is it Pocket PC 2002 or Pocket PC 2003?


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


Amit wrote:
> 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;
> }
>

 
Reply With Quote
 
=?Utf-8?B?ZHBjYmFidQ==?=
Guest
Posts: n/a
 
      3rd Oct 2005
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/de...gnatureApp.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" wrote:

> Is it Pocket PC 2002 or Pocket PC 2003?
>
>
> --
> Sergey Bogdanov [.NET CF MVP, MCSD]
> http://www.sergeybogdanov.com
>
>
> Amit wrote:
> > 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;
> > }
> >

>

 
Reply With Quote
 
=?Utf-8?B?ZHBjYmFidQ==?=
Guest
Posts: n/a
 
      3rd Oct 2005
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" wrote:

> 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/de...gnatureApp.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" wrote:
>
> > Is it Pocket PC 2002 or Pocket PC 2003?
> >
> >
> > --
> > Sergey Bogdanov [.NET CF MVP, MCSD]
> > http://www.sergeybogdanov.com
> >
> >
> > Amit wrote:
> > > 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;
> > > }
> > >

> >

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Http Post Error: A connection attempt failed because the connectedparty did not properly respond after a period of time, or establishedconnection failed because connected host has failed to respond. Deepu Microsoft Dot NET Framework 1 9th Jul 2009 05:09 PM
CryptAcquireContext fails with NTE_BAD_KEY_STATE bensonmp Microsoft Windows 2000 Security 0 28th Mar 2008 01:30 PM
CryptAcquireContext on PocketPC 2002 Emulator colin.mackay@gmail.com Microsoft Dot NET Compact Framework 1 15th Nov 2005 02:57 PM
Problem with CryptAcquireContext under Win 2003 Server schnitzell Microsoft C# .NET 1 12th May 2005 06:23 PM
CryptAcquireContext godders Windows XP Security 0 9th Jun 2004 09:50 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 07:33 AM.