Zip Component, Email Component, Encryption Component ActiveX Control for Zip Compression .NET Components for ASP.NET
ActiveX and .NET Components for Zip Compression, Encryption, Email, XML, S/MIME, HTML Email, Character Encoding, Digital Certificates, FTP, and more ASP Email ActiveX Component


Index of Chilkat Blog Posts

July 12, 2007

Using Chilkat .NET in Managed C++

In Visual Studio 2005, when you create a new C++ project you have a few choices, to say the least. Among them are to create a "Win32″ project, or a "CLR" project, such as a "CLR Console Application" or "Windows Forms Application". A Win32 project is a non-.NET project that compiles to unmanaged code and typically uses the Windows Platform SDK. You would typically include . For this kind of project you would download and use the Chilkat C++ static libraries for Visual C++ 8.0.

For a CLR project, however, you would want to use the Chilkat .NET assembly. (i.e. the Chilkat .NET component). You should already know that CLR stands for Common Language Runtime and your application will run as managed code.

This post provides the complete source code for a simple managed C++ console application that does 256-bit AES encryption. Before compiling, you’ll need to add a reference to the ChilkatDotNet2.dll. In the Visual Studio IDE, select Project–>Properties, then click on "Add New Reference", then click on the Browse tab and browse to the location of the ChilkatDotNet2.dll (usually installed under …\Program Files\Chilkat Software Inc\…

Once the reference to the Chilkat .NET DLL is added, you can use any of the Chilkat classes and you should see context-sensitive auto-completion when typing "Chilkat…".

Here’s the code, which also shows some of the Managed C++ syntax for arrays:

	
// ManagedCpp.cpp : main project file.
	
#include "stdafx.h"
	
using namespace System;
using namespace System::IO;
	
// Managed C++ to save a byte array to a binary file.
void SaveByteArray(array<unsigned char,1>^ bytArray, String^ fname)
	{
	FileStream^ fStream = gcnew FileStream(fname, FileMode::Create);
	
	BinaryWriter^ bw = gcnew BinaryWriter(fStream);
	
	bw->Write(bytArray);
	
	bw->Close();
	
	fStream->Close();
	}
	
void TestCrypt(void)
{
	Chilkat::Crypt2 crypt;
	crypt.UnlockComponent("30-day trial");
	
	crypt.CryptAlgorithm = "aes";
	crypt.KeyLength = 256;
	
	// The new array syntax in Managed C++ (for a CLI array) is
	// to declare a handle to it and then gcnew it.  The template parameter
	// is the element type of the array.
	
	// This declares an array of 32 bytes (32 * 8 bits/byte = 256 bits, which
	// is our key length).
	array<unsigned char,1>^ sKey = gcnew array<unsigned char,1>(32);
	
	int i;
	for (i=0; i<32; i++) sKey[i] = i;
	
	crypt.SecretKey = sKey;
	
	// Create some data to encrypt.
	array<unsigned char,1>^ myData = gcnew array<unsigned char,1>(100);
	for (i=0; i<100; i++) myData[i] = i;
	
	array<unsigned char,1> ^encryptedData = crypt.EncryptBytes(myData);
	
	SaveByteArray(encryptedData,"encrypted.dat");
	
	// Now decrypt it...
	array<unsigned char,1> ^decryptedData = crypt.DecryptBytes(encryptedData);
	SaveByteArray(decryptedData,"decrypted.dat");
	
}
	
int main(array<System::String ^> ^args)
{
    TestCrypt();
    Console::WriteLine(L"Hello World");
    return 0;
}
	


Privacy Statement. Copyright 2000-2011 Chilkat Software, Inc. All rights reserved.
Send feedback to support@chilkatsoft.com

Components for Microsoft Windows XP, 2000, 2003 Server, Vista, Windows 7, and Windows 95/98/NT4.