home *** CD-ROM | disk | FTP | other *** search
/ Chip 2002 September / Chip_2002-09_cd1.bin / zkuste / vbasic / Data / Utils / XZipComp.exe / XceedEncryption.Cab / F113013_MemoryEncrypt.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2001-07-04  |  4.6 KB  |  161 lines

  1. // Xceed Encryption Library - Memory Encrypt sample
  2. // Copyright (c) 2001 Xceed Software Inc.
  3. //
  4. // [MemoryEncrypt.cpp]
  5. //
  6. // This file contains the application core, and some utility functions
  7. // like HexToBinary and BinaryToHex.
  8. //
  9. // This file is part of the Xceed Encryption Library sample applications.
  10. // The source code in this file is only intended as a supplement to Xceed
  11. // Encryption Library's documentation, and is provided "as is", without
  12. // warranty of any kind, either expressed or implied.
  13.  
  14.  
  15. #include "stdafx.h"
  16. #include "MemoryEncrypt.h"
  17. #include "MemoryEncryptDlg.h"
  18.  
  19. #ifdef _DEBUG
  20. #define new DEBUG_NEW
  21. #undef THIS_FILE
  22. static char THIS_FILE[] = __FILE__;
  23. #endif
  24.  
  25.  
  26. //------------------------------------------------------------------------------------
  27. // Convert a hexadecimal string to a byte array returned in a variant.
  28. //------------------------------------------------------------------------------------
  29. COleVariant HexToBinary( LPCSTR sHexValue )
  30. {
  31.   COleVariant vaNewValue;
  32.   long I = 0;
  33.  
  34.   long lHexLen = lstrlen( sHexValue );
  35.  
  36.   if( lHexLen > 0 )
  37.   {
  38.     BYTE* pcArray = new BYTE[ lHexLen / 2 + 1 ];
  39.     long lNbElem = 0;
  40.  
  41.     while( I < lstrlen( sHexValue ) )
  42.     {
  43.       int nTemp;
  44.  
  45.       sscanf( sHexValue + I, "%2x", &nTemp );
  46.       pcArray[ lNbElem ] = nTemp;
  47.       lNbElem++;
  48.       I += 2;
  49.     }
  50.  
  51.     SAFEARRAYBOUND saBound[1];
  52.  
  53.     saBound->lLbound = 0;
  54.     saBound->cElements = lNbElem;
  55.     vaNewValue.vt = ( VT_ARRAY | VT_UI1 );
  56.     vaNewValue.parray = SafeArrayCreate( VT_UI1, 1, saBound );
  57.     CopyMemory( vaNewValue.parray->pvData, pcArray, lNbElem );
  58.     delete [] pcArray;
  59.   }
  60.  
  61.   return vaNewValue;
  62. }
  63.  
  64. //------------------------------------------------------------------------------------
  65. // Convert a byte array in a variant to an hexadecimal string.
  66. //------------------------------------------------------------------------------------
  67. CString BinaryToHex( VARIANT* vaBinaryValue )
  68. {
  69.   CString sNewValue;
  70.  
  71.   if( vaBinaryValue->vt = ( VT_ARRAY | VT_UI1 ) )
  72.   {
  73.     long lHigh;
  74.     long lLow;
  75.  
  76.     SafeArrayGetUBound( vaBinaryValue->parray, 1, &lHigh );
  77.     SafeArrayGetLBound( vaBinaryValue->parray, 1, &lLow );
  78.     
  79.     // This pointer will contain the address of the byte array
  80.     char* pcByteArray = NULL;
  81.  
  82.     // We get a pointer on the actual data in the safe array.
  83.     SafeArrayAccessData( vaBinaryValue->parray, ( void** )&pcByteArray );
  84.  
  85.     for( long I = 0; I < lHigh - lLow + 1; I++ )
  86.     {
  87.       CString szTemp;
  88.       
  89.       szTemp.Format( "%02X", ( (BYTE*)vaBinaryValue->parray->pvData)[I] );
  90.       sNewValue += szTemp;
  91.     }
  92.  
  93.     // We release the lock on the safe array
  94.     SafeArrayUnaccessData( vaBinaryValue->parray );
  95.   }
  96.  
  97.   return sNewValue;
  98. }
  99.  
  100.  
  101. /////////////////////////////////////////////////////////////////////////////
  102. // CMemoryEncryptApp
  103.  
  104. BEGIN_MESSAGE_MAP(CMemoryEncryptApp, CWinApp)
  105.     //{{AFX_MSG_MAP(CMemoryEncryptApp)
  106.         // NOTE - the ClassWizard will add and remove mapping macros here.
  107.         //    DO NOT EDIT what you see in these blocks of generated code!
  108.     //}}AFX_MSG
  109.     ON_COMMAND(ID_HELP, CWinApp::OnHelp)
  110. END_MESSAGE_MAP()
  111.  
  112. /////////////////////////////////////////////////////////////////////////////
  113. // CMemoryEncryptApp construction
  114.  
  115. CMemoryEncryptApp::CMemoryEncryptApp()
  116. {
  117.     // TODO: add construction code here,
  118.     // Place all significant initialization in InitInstance
  119. }
  120.  
  121. /////////////////////////////////////////////////////////////////////////////
  122. // The one and only CMemoryEncryptApp object
  123.  
  124. CMemoryEncryptApp theApp;
  125.  
  126. /////////////////////////////////////////////////////////////////////////////
  127. // CMemoryEncryptApp initialization
  128.  
  129. BOOL CMemoryEncryptApp::InitInstance()
  130. {
  131.     SetRegistryKey( "Xceed" );
  132.  
  133.   // Initialize OLE libraries
  134.     if (!AfxOleInit())
  135.     {
  136.         AfxMessageBox("OLE initialization failed.  Make sure that the OLE libraries are the correct version.");
  137.         return FALSE;
  138.     }
  139.  
  140.     AfxEnableControlContainer();
  141.  
  142.     // Standard initialization
  143.     // If you are not using these features and wish to reduce the size
  144.     //  of your final executable, you should remove from the following
  145.     //  the specific initialization routines you do not need.
  146.  
  147. #ifdef _AFXDLL
  148.     Enable3dControls();            // Call this when using MFC in a shared DLL
  149. #else
  150.     Enable3dControlsStatic();    // Call this when linking to MFC statically
  151. #endif
  152.  
  153.     CMemoryEncryptDlg dlg;
  154.     m_pMainWnd = &dlg;
  155.     dlg.DoModal();
  156.  
  157.     // Since the dialog has been closed, return FALSE so that we exit the
  158.     //  application, rather than start the application's message pump.
  159.     return FALSE;
  160. }
  161.