Encryption and Decryption


John Smith

Operating System: PocketPC 2003 (Emulator)
Language: VB.NET

I have downloaded the "Pocket PC Signature Application Sample" from
etcomp/html/PPCSignatureApp.asp) and have gotten it to work successfully. I
am trying to rip-off the Encryption and Decryption capability from the
sample application and put it into another application that contains
Encrypted Data in it.

My problem is that when I try to decrypt the encrypted value I receive and
error. First, I encrypt the plain text value then convert it from a byte to
a string and store it in my database. Then when I want to view the
encrypted data I call the encrypted string and convert it into a byte array
and call "Crypto.Decrypt()" and pass its parameters. When I do that, then
it fails. I did not change the vb class files whatsoever. I have provided
sample code below.

I have been pulling my hair out on this for a couple of days now.
Any ideas? Thanx in Advance. (Sample Code Below)


An unhandled exception of type 'System.SystemException' occurred in

Additional information: CryptDecrypt failed.
Last error - 0x80000005.
Error message - N/A.

'Code with Error: (Crypto.vb - Decrypt Function)

'Decrypt data. Use passphrase to generate the encryption key.
' Returns a byte array that contains the decrypted data.
Public Shared Function Decrypt( _
ByVal passphrase As String, _
ByVal data() As Byte) _
As Byte()

' make a copy of the encrypted data
Dim dataCopy As Byte() = CType(data.Clone(), Byte())

' holds the decrypted data
Dim buffer As Byte() = Nothing

' crypto handles
Dim hProv As IntPtr = IntPtr.Zero
Dim hKey As IntPtr = 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 Not WinApi.CryptAcquireContext(hProv, Nothing, _
End If

' generate encryption key from the passphrase
hKey = GetCryptoKey(hProv, passphrase)

' decrypt the data
Dim dataLength As Integer = dataCopy.Length

' This next line generated the error.

If Not WinApi.CryptDecrypt(hKey, IntPtr.Zero, True, 0, dataCopy,
dataLength) Then
End If

'**** Etc... (Code)

'Sample Code to call function:

Private Function DecryptedValue( _
ByVal EncryptedValue As String) _
As String

Dim arrByteEncryptedValue As Byte()

' Get bytes of initialization vector.
If (EncryptedValue Is Nothing) Then
arrByteEncryptedValue = New Byte() {}
arrByteEncryptedValue = Encoding.ASCII.GetBytes(EncryptedValue)
End If

' decrypt the signature data
Dim data As Byte() = Crypto.Decrypt( _
Global.Settings.GetString(SettingKeys.CryptPassphrase), _

' update the decrypted view
Dim strDecryptedValue As String
strDecryptedValue = Encoding.ASCII.GetString(data, 0, data.Length)

data = Nothing
arrByteEncryptedValue = Nothing

Return strDecryptedValue

End Function

Thanx in Advance,
John Smith

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