Xceed Encryption Library Documentation
Examples / Encrypting and decrypting from memory to file (VC++ example)
In This Topic
    Encrypting and decrypting from memory to file (VC++ example)
    In This Topic

    Here are is an example for Visual C++ that demonstrates encryption from memory to file 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 ); 
      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 );  

      const char* pszSource = "This is the data to encrypt"; 

      DWORD dwBytesWritten;  

      piEnc->WriteFile( ( BYTE* )pszSource, lstrlen( pszSource ), efpEncrypt,
                         TRUE, "c:\\temp\\encrypted.aes", FALSE, &dwBytesWritten );  

      MessageBox( NULL, "Data 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();