ADVAPI32.DLL: CryptEncrypt Function 
Author Message
 ADVAPI32.DLL: CryptEncrypt Function

Hi All,

I am trying to learn about cryptography and have written a small program to
encrypt and decrypt files. The program works fine when I encrypt strings but it
crashes in advapi when I try to encrypt byte arrays. My feeling is that the
problem lies in one of the two areas:

1. I am not passing the Byte array appropriately to the Dll function CryptEncrypt
2. The CryptEncrypt function is expecting some specific way of Byte array (like
ending null characters etc.) that I am not appropriately massaging.

I have seen that the native API for CryptEncrypt function is:

BOOL Final,
DWORD dwFlags,
BYTE *pbData,     => You should be able to pass it a Byte Array
DWORD *pdwDataLen,
DWORD dwBufLen);

Any ideas are greatly appreciated.



Program Listing:

' When I change the arrBytes parameter to strBytes as string it works fine
Private Declare Function CryptEncrypt Lib "advapi32.dll" (ByVal hKey As Long, _
    ByVal hHash As Long, ByVal Final As Long, ByVal dwFlags As Long, arrBytes()
As Byte, pdwDataLen

As Long, ByVal dwBufLen As Long) As Long

Public Function Encrypt(ByVal sPassword As String) As Byte()

  Dim arrBytes(10) as Byte

  For Counter = 0 to 10
        arrBytes(Counter) = 65 + Counter  'loads up "ABCDEFGHIJ"
  next counter

  'Acquire Crypt Context using CryptAcquireContext
  'Create Hash Object using CryptCreateHash
  'Hash in password text using CryptHashData
  'Create a session key from the hash object using CryptDeriveKey
  'Destroy the hash object using CryptDestoryHash

   'Create a buffer for the CryptEncrypt function
   lCryptLength = UBound(arrBytes)
   lCryptBufLen = lCryptLength * 2

   ReDim arrCryptBytes(lCryptBufLen)
   arrCryptBytes = arrBytes

   'the program crashes when it goes into this function
   If Not CBool(CryptEncrypt(lHkey, 0, 1, 0, arrCryptBytes, lCryptLength,
lCryptBufLen)) Then
        LastError = "bytes required:" & CStr(lCryptLength) & vbCrLf
        LastError = LastError & "Error " & CStr(GetLastError) & " during
    End If

    ReDim arrOutputBytes(lCryptLength - 1)
    arrOutputBytes = arrCryptBytes

    Encrypt = arrOutputBytes

End Function

Sun, 26 Oct 2003 15:53:19 GMT  
 [ 1 post ] 

 Relevant Pages 

1. LSA functions in advAPI32.dll

2. Crypt functions in ADVAPI32.DLL

3. ADVAPI32.DLL Functions

4. Help Me Please ADVAPI32.DLL The RegSaveKey Function

5. LogonUser advapi32.dll function

6. Can't find DLL entry point OpenEventLog in advapi32.dll

7. 'regloadkey' function from advapi32

8. Advapi32.dll

9. Help using advapi32.dll

10. ADVAPI32.DLL 05-08-98 or newer

11. Accessing the registry using advapi32.dll

12. Accessing the registry using advapi32.dll


Powered by phpBB® Forum Software