CryptoSys API Examples for CryptoSys API

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

Contents

VB Encryption Demo | Encryption Scheme using Triple DES in CBC mode | Encryption using a password | 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

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 package.

VB Encryption Demo

The Visual Basic project CryptoSys3.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, as well as the compiled executable CryptoSys3.exe. 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.

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.

This page last updated 7 July 2008