Using Delphi with CryptoSys API, CryptoSys PKI and FirmaSAT


It is possible to call all the functions in our cryptographic products CryptoSys API, CryptoSys PKI and FirmaSAT from Delphi. This page explains what we know and gives some sample code.

FirmaSAT | CryptoSys API | CryptoSys PKI Toolkit | Notes for programmers | Acknowledgements | Disclaimer | Contact | Comments | Spanish Español

The Delphi code on this page works for us using Delphi Builder 2010 on a Windows XP machine. There may well be better or more elegant ways of calling the functions. We don't know how backwards compatible this code is. We'll keep updating the code on this page as we refine things and get feedback (well, at least until our 30-day trial period ends :-).

If anyone has any corrections or useful suggestions, we'd be delighted to hear from you. Please contact us or add a comment using the links at the bottom of this page. For more info on creating interfaces see Writing an interface in another programming language.

FirmaSAT

Here is a full Delphi interface to all the functions in FirmaSAT including a series of tests and some test files: diFirmaSat.pas.zip (16 kB. Last updated 2010-03-18). The Delphi interfaces to the FirmaSAT functions are provided in the file diFirmaSat.pas. (Use at your own risk. No warranties, etc. See the Disclaimer below.)

You need to have installed both FirmaSAT and the CryptoSys PKI Toolkit on your system. For more information see How to get started on the FirmaSAT page.

Note that the tests in TestFirmaSat require certain files to exist in a specified directory. The test files are provided in the zip file above. The directory is hardcoded in the code

const
  TEST_DIR = 'C:\Test\Muestra';

- you may need to edit this to suit your system.

For more information on the FirmaSAT function parameters and return values, see the C include file diFirmaSAT2.h.

CryptoSys API

Here is a Delphi interface to all the functions in CryptoSys API with a start on some typical tests: diCryptoSys.pas.zip (11 kB. Last updated 2010-03-18). The function declarations and associated constants are in the file diCryptoSys.pas.

For more information on the CryptoSys API function parameters and return values, see the Main manual for CryptoSys API.

CryptoSys PKI Toolkit

Here is a provisional Delphi interface to all the functions in CryptoSys PKI with a few sample tests: diCrPKI.pas.zip (7 kB. Last updated 2010-03-17). The function declarations and associated constants are in the file diCrPKI.pas.

For more information on the CryptoSys PKI function parameters and return values, see the Main manual for CryptoSys PKI.

Notes for programmers

Variable types used in the interface:

C++ TypeDelphi TypeDescription
long, int Integer A signed 32-bit integer, passed by value
char *sz AnsiString ANSI string: a string of single-byte non-zero characters terminated by a zero byte
unsigned char *lp Array of Byte Byte array: an ordered sequence of single-byte values of any value in the range [0,255]
  1. All integer variables passed to our DLLs (and return values from them) must be of type Integer, a 32-bit signed integer.
  2. All strings are of type AnsiString. Attempting to pass a "normal" Unicode or Wide string will not work.
  3. Byte arrays should be of type Array of Byte.
  4. Strings and byte arrays that receive output need to be pre-dimensioned to the correct length before calling a DLL function. Then pass a Pointer as the parameter.
    buf : AnsiString;
    //...
    buf := AnsiString(StringOfChar(#0,nchars));
    ret := FOO_StringFunction(Pointer(buf), nchars, ...);
    
    arrbytes : Array of Byte;
    //...
    SetLength(arrbytes, nbytes);
    ret := FOO_ByteFunction(Pointer(arrbytes), nbytes, ...);
    
    These output parameters are expected as PChar PAnsiChar and PByte respectively. [PChar also seemed to work OK for us in early tests and the compiler didn't complain either].
  5. Constant strings passed as input can be simply passed as an AnsiString type. Constant byte arrays passed as input still need to be passed as a pointer (AFAWK).
  6. To pass a null pointer to either an AnsiString or Array of Byte, use the Delphi NIL value, as in
    nchars := SAT_CompileTime(NIL, 0);
    
  7. Depending on how you've defined your byte arrays, you pass pointers to them differently,
    abKey : Array of Byte;
    tag   : Array[0..15] of Byte;
    //...
    ret := GCM_Encrypt(NIL, 0, @tag[0], Length(tag), NIL, 0,
      Pointer(abKey), Length(abKey),...);
    
  8. For a reason we haven't worked out yet, re-using a dynamic byte array variable after using it in a DLL function can give erroneous results unless you re-do the SetLength operation
    SetLength(abOut, Length(abIn));
    
  9. Strings used in the _UpdateHex functions which are used as both input and to receive output, need to be declared as global vars, not local.

Acknowledgements

Thanks to Sebastian Jäschke and Humberto Souza for their help in providing example Delphi interfaces to CryptoSys API, without which we would not have been able to get started, and to Cesar Ruiz for his feedback. Thanks, too, to Neil Moffatt for his excellent Delphi Basics site.

Disclaimer

Our CryptoSys products are offered primarily with interfaces using the C/C++, C#, VB6/VBA and VB.NET languages. We don't offer to support any other interfaces or languages. We will try and help if we can. The code on this page is offered for free in good faith but may contain nuts and/or errors. Please make whatever checks you need to before purchase of the main product.

Contact

For more information, please Email Us. To comment on this page, see below.

This page first published 13 March 2010. Last updated 20 August 2010

Comments

Post a comment on this page.

5 comments so far

Thanks for the topic. Please more of Delphi, thanks.

Ralph | Germany - Fri, Mar 19 2010 19:53 GMT

Great explanation.

Please, provide more useful examples and descriptions for Delphi.

Thanks.

Terry | Spain - Thu, Jul 8 2010 18:00 GMT

What's on this page (and in the linked files) is *all* the Delphi examples we've ever done. If someone else wants to post some more, please do. They should be relevant.

Dave | Moderator - Thu, Jul 15 2010 04:29 GMT

cuanto hay que pagar por usar esta librerias

adrian | - Tue, Aug 17 2010 20:39 GMT

Free trials available at

CryptoSys API: http://www.cryptosys.net/index.html#download

CryptoSys PKI: http://www.cryptosys.net/pki/index.html#download

FirmaSAT: http://www.cryptosys.net/fsa/index.html#download

Purchase at

CryptoSys API: http://www.cryptosys.net/purchase.html

CryptoSys PKI: http://www.cryptosys.net/pki/purchase.html

FirmaSAT: http://www.cryptosys.net/fsa/purchase.html

FirmaSAT needs CryptoSys PKI. All the trial versions are fully functional. Please test before buying.

Dave | Moderator - Thu, Aug 19 2010 21:39 GMT