This page contains some examples showing how to use the functions in CryptoSys API.
Simple "Hello World" programs | Test modules in the distribution | VB.NET/VB6 Encryption Demos | Encryption using a password | Encrypting files using CryptoSys API | CryptoSys API Examples VB6 to VB.NET | Encryption Scheme using Triple DES in CBC mode | Encryption in C# and VB.NET | Using with Microsoft Excel | htpasswd using SHA-1 | MySecret with CryptoSys API | ASP/VBScript | More information and techniques | Contact
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.
There are a comprehensive set of tests provided in the distribution download, which give examples of how to use almost all the functions in the library.
You will find the latest versions of these files in the installation directory, usually C:\Program Files (x86)\CryptoSysAPI
.
The VB.NET project CryptoSys5
has a set of examples showing
how to encrypt with various algorithms and modes. The source code for this project is in the
file CryptoSys5.src.zip
provided with the standard installation setup.
The core workings are in frmCryptoSys5.vb.
More details at CryptoSys API Testbed Demo Help.
The older Visual Basic (VB6) project Cryptosys4
(which does the same as above) is in the file CryptoSys4.src.zip
.
The core workings are in CryptoSys.frm.
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.
Looking at using your package in a VB6 application. I want to be able to encrypt using AES256 files which are typically about 8K in size. I then need to load this file in another application and decrypt it into an internal array.
See Encrypting files using CryptoSys API.
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.
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.
The sample code in VB.NET and C# below also shows how to encrypt a string.
Example: 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.) See the example code in VB.NET below, which uses AES-128 in CBC mode.
Note this code is a for a Console Application and you need to set a reference in your project
to diCrSysAPINet.dll
.
using System; using CryptoSysAPI; //=================== 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"; // DO NOT HARDCODE PRODUCTION PASSWORDS! 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'
Example: Here is some code in VB.NET (VB2005/8/x) similar to the example above in C# showing how to encrypt and decrypt a string. This time we use AES-128 in CBC mode and generate a fresh Initialization Vector (IV) each time.
Note this code is a for a Console Application and you need to set a reference in your project
to diCrSysAPINet.dll
.
Imports System Imports CryptoSysAPI '=================== Console.WriteLine("CryptoSys API Version={0}", General.Version()) ' ENCRYPTION. ' INPUT: data string, key in hex format ' OUTPUT: ciphertext in hex format, initialization vector (IV) Console.WriteLine("ENCRYPTION:") ' 0. Use a "secret" key. AES-128 requires a key exactly 16 bytes long ' CAUTION: do not hard-code a production key like this! Dim keyHex As String = "FEDCBA9876543210FEDCBA9876543210" Dim data As String = "Some text" ' 1. Convert string to hex format Dim dataHex As String = Cnv.ToHex(data) ' 2. Pad it ready for encryption Dim plainHex As String = Aes128.Pad(dataHex) ' 3. Generate a random Initialization Vector (IV) Dim ivHex = Rng.NonceHex(Aes128.BlockSize) Console.WriteLine("Key ={0}", keyHex) Console.WriteLine("IV ={0}", ivHex) Console.WriteLine("Data ='{0}'", data) Console.WriteLine("In Hex ={0}", dataHex) Console.WriteLine("Input to cipher ={0}", plainHex) ' 4. Encrypt the plaintext using CBC mode Dim cipherHex As String = Aes128.Encrypt(plainHex, keyHex, Mode.CBC, ivHex) Console.WriteLine("Ciphertext ={0}", cipherHex) ' DECRYPTION. ' INPUT: ciphertext in hex format, key in hex format, IV in hex format ' OUTPUT: data string OR "decryption failed" error Console.WriteLine("DECRYPTION:") Console.WriteLine("Key ={0}", keyHex) Console.WriteLine("IV ={0}", ivHex) Console.WriteLine("Ciphertext ={0}", cipherHex) ' 1. Decrypt ciphertext plainHex = Aes128.Decrypt(cipherHex, keyHex, Mode.CBC, ivHex) Console.WriteLine("Output in hex ={0}", plainHex) ' 2. Unpad it dataHex = Aes128.Unpad(plainHex) Console.WriteLine("After unpadding ={0}", dataHex) ' 3. Check for error (see CryptoSysAPI.chm help) If dataHex.Length = plainHex.Length Then Console.WriteLine("decyryption error") Return End If ' 4. Convert from hex back to string, if OK data = Cnv.StringFromHex(dataHex) Console.WriteLine("Data ='{0}'", data)
This should result in output similar to:
CryptoSys API Version=440 ENCRYPTION: Key =FEDCBA9876543210FEDCBA9876543210 IV =9FBA1590B9A194115C83D3E67EFBF500 Data ='Some text' In Hex =536F6D652074657874 Input to cipher =536F6D65207465787407070707070707 Ciphertext =44C392D4ECC529D97955C5D38DE99E04 DECRYPTION: Key =FEDCBA9876543210FEDCBA9876543210 IV =9FBA1590B9A194115C83D3E67EFBF500 Ciphertext =44C392D4ECC529D97955C5D38DE99E04 Output in hex =536F6D65207465787407070707070707 After unpadding =536F6D652074657874 Data ='Some text'
9FBA1590B9A194115C83D3E67EFBF50044C392D4ECC529D97955C5D38DE99E04and the receiving party would split off the IV (they know exactly how long it is).
See Using CryptoSys API with Microsoft Excel for an example of how to call a function in CryptoSys API from an Excel spreadsheet.
If you need to create or verify passwords that use the Apache htpasswd {SHA} or {SSHA} format, you can use this VB htpasswd code.
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.
See the ActiveX Interface page for more information and examples including this extract from apiocxtests.asp which shows the basic VBScript code.
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:
For more information or to comment on this page, please send us a message.
This page last updated 15 August 2025