Xceed Encryption Library Documentation
Encrypting and decrypting from file to file (VC++ example)
Examples > Encrypting and decrypting from file to file (VC++ example)

Here are two examples for Visual C++, one demonstrates encryption from file to file, the other demonstrates decryption from file to file. Both show how to use the Rijndael algorithm.

VC++ - Encryption 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 ); 
  piEnc->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 );  

  DWORD dwBytesRead; 
  DWORD dwBytesWritten;  

  piEnc->ProcessFile( "c:\\temp\\source.txt", 0, 0, efpEncrypt, TRUE,
                      "c:\\temp\\encrypted.aes", FALSE, &dwBytesRead, &dwBytesWritten );  

  MessageBox( NULL, "File encrypted successfully", "Encryption result", MB_OK ); 
}
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();

VC++ - Decryption 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 ); 
  piEnc->License( _bstr_t( L"your license key ) ); 

  IXceedRijndaelEncryptionMethodPtr piRijndael; 
  piRijndael.CreateInstance( CLSID_XceedRijndaelEncryptionMethod );  

  piEnc-> EncryptionMethod = IXceedEncryptDataPtr( piRijndael );  

  DWORD dwBytesRead; 
  DWORD dwBytesWritten;  

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

  piEnc->ProcessFile( "c:\\temp\\encrypted.aes", 0, 0, efpDecrypt, TRUE,
                      "c:\\temp\\decrypted.txt", FALSE, &dwBytesRead, &dwBytesWritten );  

  MessageBox( NULL, "File decrypted successfully", "Decryption result", MB_OK ); 
}
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();