Xceed Encryption Library Documentation
Encrypting and decrypting entirely in memory (VC++ example)
Examples > Encrypting and decrypting entirely in memory (VC++ example)

Here are is an example for Visual C++ that demonstrates encryption entirely in memory in a single pass. It shows how to use the Rijndael algorithm.

VC++ Copy Code

// This code uses the #import directive.

// Put the following line at the beginning of your module
#import "XCEEDCRY.DLL" no_namespace named_guids 

CoInitialize( NULL ); 

try
{
  IXceedEncryptionPtr piEnc; 

  piEnc.CreateInstance( CLSID_XceedEncryption ); 
  piSign->License( _bstr_t( L"your license key ) ); 

  IXceedRijndaelEncryptionMethodPtr piRijndael; 

  piRijndael.CreateInstance( CLSID_XceedRijndaelEncryptionMethod );  
  piRijndael->SetSecretKeyFromPassPhrase( "This is a weak pass phrase!", 128 ); 

  piEnc-> EncryptionMethod = IXceedEncryptDataPtr( piRijndael );  

  const char* pszSource = "This is the data to encrypt"; 
  DWORD dwSourceSize = lstrlen( pszSource ) + 1; // Let's say we want to encrypt the null-char too 
  BYTE* pcEncrypted = NULL; 
  DWORD dwEncryptedSize = 0;  

  piEnc->Encrypt( ( BYTE* )pszSource, dwSourceSize, TRUE, &pcEncrypted, &dwEncryptedSize );  

  MessageBox( NULL, "Data encrypted successfully", "Encryption result", MB_OK );  

  CoTaskMemFree( pcEncrypted ); 
}
catch( const _com_error& xErr )
{
  char szMsg[50]; 
  wsprintf( szMsg, "Error %08x\n", xErr.Error() ); 
  MessageBox( NULL, szMsg, "Error", MB_OK ); 
}
catch( ... )
{
  MessageBox( NULL, "Unknown error", "Error", MB_OK ); 

CoUninitialize();