Examples for CryptoSys API


This page contains some examples showing how to use the functions in CryptoSys API.

Contents

VB6 Encryption Demo | CryptoSys API Examples VB6 to VB.NET | Encryption Scheme using Triple DES in CBC mode | Encryption using a password | Using with Microsoft Excel | htpasswd using SHA-1 | Compression and encryption | MySecret | More information and techniques | VB6/VBA Tests | C/C++ Tests | C# Tests | VB.NET Tests | ASP/VBScript| Comment

Source code listings and zipped projects

CAUTION: This page has links to various source code listings. These may contain HTML formatting errors or be out of date. Likewise, the zipped project files here may contain out-of-date interface files. Please always use the original source code distributed with the latest version of the package, as installed in C:\Program Files\CryptoSys.

Simple "Hello World" programs

The equivalent of the "Hello world" program for CryptoSys API is to call the API_Version function. A correct response demonstrates that the API is properly installed. See Hello World programs for some sample code.

CryptoSys API Examples VB6 to VB.NET

New 2009-07-24: CryptoSys API Examples VB6 to VB.NET is a conversion of most of the VB6 examples in the CryptoSys API manual to VB.NET. It shows how to convert VB6 code to the equivalent VB.NET code.

VB6 Encryption Demo

The Visual Basic (VB6) project CryptoSys4.vbp has a set of examples showing how to encrypt with various algorithms and modes. The source code for this project is in the file CryptoSysVBDemo.zip provided with the standard installation setup, which should be installed on your system in C:\Program Files\CryptoSys\VB6. The core functions are in CryptoSys.frm.

Encryption Scheme using Triple DES in CBC mode

The page Encryption Scheme using Triple DES in CBC mode shows how you could encrypt an arbitrary-length string using the functions in CryptoSys API. There is source code in C/C++ and VB6/VBA. The examples show how to encrypt an ANSI string in Byte mode, in Hex mode, and (for C/C++) how you could encrypt a string that consists of Unicode `wide' characters.

Encryption using a password

See a sample Visual Basic project showing how to encrypt variable-length strings 'properly' with a key derived from a text password using the PBKDF2 algorithm from PKCS #5 v2.0. The example uses AES-128 but could use any of the major encryption algorithms like Triple DES or Blowfish. There is sample code in both VB6 and VB.NET.

Using with Microsoft Excel

See Using CryptoSys API with Microsoft Excel for an example of how to call a function in CryptoSys API from an Excel spreadsheet.

htpasswd using SHA-1

If you need to create or verify passwords that use the Apache htpasswd {SHA} or {SSHA} format, you can use this VB htpasswd code.

Compression and encryption

For a method to use compression before encryption see Using Compression with CryptoSys.

MySecret with CryptoSys API

There is a demonstration VB6 project available that shows how you can use CryptoSys API to create and decipher data encrypted using the MySecret Blowfish Encryption Utility version 3 format. The techniques shown involve compression-before-encryption, encryption using Blowfish, key derivations from a password, and the use of a checksum to validate the decrypted data. More details on the MySecret with CryptoSys API page.

More information and techniques

For more information and discussion on how to use keys and padding in cryptography, as well as cross-platform and international character set issues, please refer to our pages:

VB6/VBA Tests

The Visual Basic project in APITests.VB6.zip provided with the installation download has a series of tests that call almost every function in the API and carry out all the examples in the manual. These projects are included in the standard installation setup and should be found in the folder

C:\Program Files\CryptoSys\VB6

unless you specified another location during the setup process.

C/C++ Tests

C/C++ examples are given in the source code files API_Examples.c and APICheck.c provided in the installation download.

C# Tests

See the test examples in TestAPIcsharp.cs provided in the installation download. This code is in the file TestAPIcsharp.zip which should be in the directory

C:\Program Files\CryptoSysAPI\DotNet

unless you specified a different folder during installation.

Here is some simple C# code showing how to encrypt and decrypt a string using Blowfish in ECB mode. (Warning: ECB mode has security weaknesses. We recommend you use CBC mode with a fresh, randomly-generated initialization vector (IV) each time.)

Console.WriteLine("CryptoSys API Version={0}", General.Version());
// ENCRYPTION.
// INPUT: data string, key in hex format
// OUTPUT: ciphertext in hex format
Console.WriteLine("ENCRYPTION:");
string keyHex = "FEDCBA9876543210";
string data = "Some text";
// 1. Convert string to hex format
string dataHex= Cnv.ToHex(data);
// 2. Pad it ready for encryption
string plainHex = Blowfish.Pad(dataHex);
Console.WriteLine("Key             ={0}", keyHex);
Console.WriteLine("Data            ='{0}'", data);
Console.WriteLine("In Hex          ={0}", dataHex);
Console.WriteLine("Input to cipher ={0}", plainHex);
// 3. Encrypt it
string cipherHex = Blowfish.Encrypt(plainHex, keyHex, Mode.ECB, null);
Console.WriteLine("Ciphertext      ={0}", cipherHex);

// DECRYPTION.
// INPUT: ciphertext in hex format, key in hex format
// OUTPUT: data string OR "decryption failed" error
Console.WriteLine("DECRYPTION:");
Console.WriteLine("Key             ={0}", keyHex);
Console.WriteLine("Ciphertext      ={0}", cipherHex);
// 1. Decrypt ciphertext
plainHex = Blowfish.Decrypt(cipherHex, keyHex, Mode.ECB, null);
Console.WriteLine("Output in hex   ={0}", plainHex);
// 2. Unpad it
dataHex = Blowfish.Unpad(plainHex);
Console.WriteLine("After unpadding ={0}", dataHex);
// 3. Check for error (see CryptoSysAPI.chm help)
if (dataHex.Length == plainHex.Length)
{
   Console.WriteLine("decyryption error");
   return;
}
// 4. Convert from hex back to string, if OK
data = Cnv.StringFromHex(dataHex);
Console.WriteLine("Data            ='{0}'", data);

This should result in the output:

CryptoSys API Version=400
ENCRYPTION:
Key             =FEDCBA9876543210
Data            ='Some text'
In Hex          =536F6D652074657874
Input to cipher =536F6D65207465787407070707070707
Ciphertext      =5D6216A08F6276896C843DF04052AAF9
DECRYPTION:
Key             =FEDCBA9876543210
Ciphertext      =5D6216A08F6276896C843DF04052AAF9
Output in hex   =536F6D65207465787407070707070707
After unpadding =536F6D652074657874
Data            ='Some text'

VB.NET Tests

See the test examples in TestAPIvbnet.vb provided in the installation download. This code is in the file TestAPIvbnetp.zip which should be in the directory

C:\Program Files\CryptoSysAPI\DotNet

unless you specified a different folder during installation. See also Module1.vb from the project CrSysAPIDemoEncrypt also provided in the installation download.

ASP/VBScript

See the ActiveX Interface page for more information and examples including this extract from apiocxtests.asp which shows the basic VBScript code.

Contact

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

This page last updated 1 January 2010

Comments

Post a comment on this page.

1 comments so far

It would be great to see an example where a file is encrypted (example: use PC1_File [Debug.Print "Testing PC1_File ..." http://cryptosys.org/apiv2n/apiv2n_PC1_File.html] to encrypt) along side a stream reader to decrypt the encrypted file. In this manner, one could distribute an encrypted file yet stream the data from the file in its encrypted state and release only the result in an unencrypted state.

Jamie | - Tue, May 18 2010 21:29 GMT