Code examples

The best way to illustrate what cryptlib can do is with an example. The following code digitally signs a message and then encrypts it using a mixture of public-key and conventional encryption.

  /* Create an envelope for the message */
  cryptCreateEnvelope( &cryptEnvelope );

  /* Push in the message recipients name and our own signature key */
  cryptAddResourceNumeric( cryptEnvelope, CRYPT_ENVELOPE_RECIPIENT, recipientName );
  cryptAddResourceNumeric( cryptEnvelope, CRYPT_ENVELOPE_SIGNATURE, signatureKey );

  /* Push in the message data and pop out the signed and encrypted result */
  cryptPushData( cryptEnvelope, message, messageSize, &bytesIn );
  cryptPopData( cryptEnvelope, encryptedMessage, encryptedSize, &bytesOut );

  /* Clean up */
  cryptDestroyEnvelope( cryptEnvelope );

This performs the same task as a program like PGP, using just 6 function calls. All data management is handled automatically by cryptlib, so there's no need to worry about encryption modes and algorithms and keylengths and key types and initialisation vectors and other details (although cryptlib provides the ability to specify all this if you feel the need).

The code shown above results in cryptlib performing the following actions:

However unless you want to call cryptlib using the low-level interface, you never need to know about any of this. cryptlib will automatically know what to do with the data based on the resources you add to the envelope - if you add a signature key it will sign the data, if you add an encryption key it will encrypt the data, and so on.


cryptlib Information / Peter Gutmann / pgut001@cs.auckland.ac.nz
Information last updated 16 November 1998