Encrypting and Decryption memory (C++)
When using the library from C or C++ you can easily encrypt most types of data by working directly on memory. In VB and .NET you have to get involved in the horrors (to the C++ programmer) of safearrays. Since the library is packaged as a DLL you can just pass void* pointers into ENCX without worrying about marshalling.
You need to get hold of the dongle object from the library instance and the dongle needs to be active. See Dongle Free Security and Dongling for Command Line Applications if you are writing a command line application.
#import "encxcom.tlb" rename_namespace("encx")
void EncryptionTest()
{
try
{
encx::ILibraryPtr pEncx;
pEncx.CreateInstance(__uuidof(encx::Library));
// must have hardware dongle or DFS virtual dongle file in default location
pEncx->Construct("", "");
encx::IDonglePtr pDongle = pEncx->GetDongle();
// Wait for dongle (on another thread) to become active
// (this way is a little crude and not best practice!)
::Sleep(30000);
if(pDongle->State != encx::DS_Active)
{
// failed as not licensed
return;
}
// READY TO ENCRYPT AND DECRYPT
// we'll fill in this bit soon!
}
catch (const _com_error& e)
{
// handle errors
}
The calls Encrypt and Decrypt on the dongle object both take a void* pointing to the start of the memory and a long containing the number of bytes to be encrypted or decrypted.
const int nSize = 100;
BYTE* pData = new BYTE[nSize];
// Create some data to illustrate
for(int n = 0; n < nSize; ++n)
pData[n] = BYTE(n);
pDongle->Encrypt(pData, nSize);
Will turn the 'data' into nonsense and..
pDongle->Decrypt(pData, nSize);
Note: Data encrypted on one computer will not decrypt on another as the encryption is machine specific.
Encrypting and Decrypting Arrays (VB 6)
The following code sample assumes that you have already initialised the library and ensured that the dongle is active. Encryption and Decryption will not work otherwise.
A global variable g_encx is an instance of the ENCX library object.
The methods to call on the Dongle are EncryptArray and DecryptArray which both take a variant containing an array (a safe array). See the code sample below.
Dim d as encx.Dongle
Set d = g_encx.Dongle
' Create a variant containing an array
Dim arr As Variant
ReDim arr(10) As Long
Dim n As Integer
For n = 1 To 10
arr(n) = n
Next
dgl.EncryptArray arr
Dim bdiff As Boolean
bdiff = False
For n = 1 To 10
If Not arr(n) = n Then bdiff = True
Next
If Not bdiff Then MsgBox "did not encrypt"
dgl.DecryptArray arr
For n = 1 To 10
If Not arr(n) = n Then MsgBox "Did not decrypt"
Next
Andrew Nibbs has developed software and managed projects for Chersoft for 11 years. He specialises in C++ and does a bit of C# and even less VB. He once wrote a Python script.