Friday, April 04, 2008

Using Cryptor DLL Functions

If you use Cryptor from XiTech for encryption in VFP, you may be aware that its FLL is sensitive to the version of VFP you're using. For example, C40Fox80.FLL is for VFP 8, C40Fox70.FLL is for VFP 7, etc. Although Cryptor 5 has been available for some time, I haven't bothered to upgrade because version 4 does everything I need it to. Also, there are some installation issues with Cryptor 5 on Vista, since it's now a COM object, while version 4 doesn't need registration. One problem with version 4, though, is that it doesn't come with an FLL for VFP 9.

Fortunately, you don't need one. Cryptor comes with a DLL, XICrCore.DLL, that contains functions providing Cryptor features. I use a class called SFCryptor that's a wrapper for the DLL. You can download it from the Technical Papers page of my Web site.

Here are the declarations for these functions:

lcDLL = fullpath('XICrCore.dll', addbs(.cCryptorPath))
declare integer CRYIni_Initialize      in (lcDLL) ;
    integer LoadMode
declare integer CRYIni_InitializeEx    in (lcDLL) ;
    integer dwLoadMode, integer hModule, string strExclusionList
declare integer CRYIni_UnInitialize    in (lcDLL)
declare integer CRYUtl_EncodeString    in (lcDLL) ;
    string strSrc, string @ strDest, integer dwLength, ;
    string strPassword, integer dwMethod
declare integer CRYUtl_DecodeString    in (lcDLL) ;
    string strSrc, string @ strDest, integer dwLength, ;
    string strPassword, integer dwMethod
declare string  CRYUtl_GetErrorMessage in (lcDLL) ;
    integer errorCode
declare integer CRYUtl_Encode          in (lcDLL) ;
    string strFilename, string strPassword, string strBackupExt, ;
    integer bKeepBackup, integer dwMethod
declare integer CRYUtl_Decode          in (lcDLL) ;
    string strFilename, string strPassword, string strBackupExt, ;
    integer bKeepBackup, integer dwMethod
declare integer CRYMan_Register        in (lcDLL) ;
    string strFilename, string strPassword, integer dwFlags, ;
    integer dwMethod
declare integer CRYMan_Unregister      in (lcDLL) ;
    string strFilename
declare integer CRYMan_List            in (lcDLL) ;
    integer dwFunction, string @ strFilename, integer @ pdwFlags, ;
    integer @ pdwMethod, integer @ pdwCount


This code initializes Cryptor:



#define CRYPTOR_ERR_SUCCESS             0x00000000
#define CRYPTOR_ERR_ALREADY_INITIALIZED 0xFFFFFF02 - 0x100000000
if version(2) = 0
    declare integer GetModuleHandle in Win32API string ModuleName
    lnHookModule = GetModuleHandle('VFP' + ;
        left(transform(version(5)), 1) + 'R.DLL')
    if lnHookModule <> 0
        lnStatus = CRYIni_InitializeEx(2, lnHookModule, ';')
    else   
        lnStatus = -1
    endif lnHookModule <> 0
else
    lnStatus = CRYIni_InitializeEx(2, 0, ';')
endif version(2) = 0
if inlist(lnStatus, CRYPTOR_ERR_SUCCESS, ;
    CRYPTOR_ERR_ALREADY_INITIALIZED)
    llInitialized = .T.
else
    lcErrorMessage = 'Could not initialize Cryptor: status ' + ;
        'code ' + transform(lnStatus)
    llInitialized = .F.
endif inlist(lnStatus ...


This code registers, unregisters, encrypts, or decrypts a file; pass it the filename, password, encryption method, and "register" to register the file, "unregister" to unregister it, "encrypt" to encrypt it, or "decrypt" to decrypt it. Error handling code was removed for brevity.



lparameters tcFileName, ;
    tcPassword, ;
    tnMethod, ;
    tcProcess
local laFiles[1], ;
    lnFiles, ;
    lcPath, ;
    lnI, ;
    lcFile, ;
    lnStatus, ;
    llReturn

* Get the file to process. Use ADIR() because we may a file skeleton
* like SOMETHING.*.

lnFiles = adir(laFiles, tcFileName)

* Try to process the file(s).

lcPath = addbs(justpath(tcFileName))
for lnI = 1 to lnFiles
lcFile = lower(lcPath + laFiles[lnI, 1])
do case
case tcProcess = 'encrypt'
         lnStatus = CRYUtl_Encode(lcFile, tcPassword, .NULL., 0, ;
             tnMethod)
      case tcProcess = 'decrypt'
         lnStatus = CRYUtl_Decode(lcFile, tcPassword, .NULL., 0, ;
              tnMethod)
      case tcProcess = 'register'
          lnStatus = CRYMan_Register(lcFile, tcPassword, 0, tnMethod)
      case tcProcess = 'unregister'
          lnStatus = CRYMan_Unregister(lcFile)
    endcase
    llReturn = lnStatus = CRYPTOR_ERR_SUCCESS
    if not llReturn
        lcErrorMessage = 'Could not ' + tcProcess + ' ' + lcFile + ;
            ': ' + CRYUtl_GetErrorMessage(lnStatus)
       exit
  endif not llReturn
next lnI
return llReturn

14 comments:

  1. Anonymous4:58 PM

    Hi Doug,

    great info, thanks alot, but there is actually a working C40Fox90.FLL for VFP9.

    We are using the cryptor for quite some time as version 4 and 5. The folks at ProLib.de where we licenced cryptor have made the FLL available to us so it was easy for us to accomodate the change from 8 to 9 without any problem.

    On the other hand, it's great to have one file less to install which is now possible with your provided informations. Thanks alot.

    ReplyDelete
  2. Hi Michael & Doug,

    We are having similar issues with VFP9 and Cryptor. Doug's notes are great but perhaps the c40fox90.dll would be better? How can we get/purchase this file?

    Bruce

    ReplyDelete
  3. Anonymous2:02 PM

    Yeah, been using the FLL for 9 for quite awhile. But I can't get Xitech to respond to support emails. I may have to look for another solution.

    ReplyDelete
  4. Anonymous9:52 AM

    Doug, I didn't see that wrapper for Cryptor on your website download page that you linked.

    ReplyDelete
  5. Hi Russell. It's the first item in the list of white papers/source code. You may have to clean out your browser cache to see it.

    ReplyDelete
  6. Anonymous10:37 AM

    Nope, it was there the whole time. For some reason, I read that as "Crystal", as in the report writer, and not Cryptor, then once I thought I knew what it said, I didn't pay enough attention again to realize my mistake. Sorry.

    ReplyDelete
  7. Anonymous11:20 PM

    Hi Doug:

    I´m using Cryptor 5 in Windows Vista but have the error registering "Error charging module CryptorDLL"

    I´m waiting a response from Oliver.

    Have you further information on this subject?

    I realize several people are using Cryptor 5 in Vista successfully.

    Thank you.

    ReplyDelete
  8. Hi Alejandro.

    Check http://fox.wikis.com/wc.dll?Wiki~CryptorOnWindowsVista~VFP for more information.

    Doug

    ReplyDelete
  9. Anonymous6:10 AM

    I tried many time to contact the xitech by email or phone but not answer. We are using cryptor 4 for last five years.

    I like this class but if you can supply a sample to use the class it will be great

    ReplyDelete
  10. Hi Doug!!

    Do you know if it does work with Windows 7? Because I encrypt the DBFs on the fly with a product Win 7 incompatible.

    I know I must to ask to Xitech, but I can't contact them. I'm calling every day for two weeks an nobody answer.

    Thanks a lot
    Gustavo

    ReplyDelete
  11. I haven't tried it in Windows 7 but believe it will work.

    ReplyDelete
  12. To run Cryptor at W2003 and others you must turn off DEP.

    “ bcdedit /set nx AlwaysOff “

    To run C40FOX60.FLL at VFP 7 and 9 you only need to change a BYTE at this FLL.

    There is a IF version() at the DLL. Only that.

    Now i'm trying to open these DBF Crypted files at .NET

    Xitech does not help. Any suggestion ???

    Thanks
    Moshe

    ReplyDelete
  13. Sorry, Moshe, I don't have any suggestions.

    ReplyDelete
  14. Horst Miller4:37 AM

    Is there any trial / evaluation-version available (Cryptor 4 or 5).

    On the manufacturer's webpage it was(?) possible to order one, but I cannot contact the developer, because all mails get rejected.

    Has someone of you a trial (or the manual for reading) ?

    ReplyDelete