home *** CD-ROM | disk | FTP | other *** search
- // Xceed Encryption Library - Memory Encrypt sample
- // Copyright (c) 2001 Xceed Software Inc.
- //
- // [MemoryEncryptDlg.cpp]
- //
- // This form module contains the main form's code. It demonstrates how to
- // encrypt a chunk of memory data using different kinds of encryption methods,
- // and decrypt an Encrypted memory data. It specifically uses:
- // - The SetSecretKeyFromPassPhrase, SetRandomInitVector,
- // Encrypt and Decrypt methods.
- // - The HashSize, HashingMethod, EncryptionMode, PaddingMethod,
- // EncryptionMethod, PrivateKey and PublicKey properties.
- //
- // This file is part of the Xceed Encryption Library sample applications.
- // The source code in this file is only intended as a supplement to Xceed
- // Encryption Library's documentation, and is provided "as is", without
- // warranty of any kind, either expressed or implied.
-
-
- #include "stdafx.h"
- #include "MemoryEncrypt.h"
- #include "MemoryEncryptDlg.h"
-
- #include "OptionDlg.h"
- #include "KeyPair.h"
-
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
-
- /////////////////////////////////////////////////////////////////////////////
- // CAboutDlg dialog used for App About
-
- class CAboutDlg : public CDialog
- {
- public:
- CAboutDlg();
-
- // Dialog Data
- //{{AFX_DATA(CAboutDlg)
- enum { IDD = IDD_ABOUTBOX };
- //}}AFX_DATA
-
- // ClassWizard generated virtual function overrides
- //{{AFX_VIRTUAL(CAboutDlg)
- protected:
- virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
- //}}AFX_VIRTUAL
-
- // Implementation
- protected:
- //{{AFX_MSG(CAboutDlg)
- //}}AFX_MSG
- DECLARE_MESSAGE_MAP()
- };
-
- CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
- {
- //{{AFX_DATA_INIT(CAboutDlg)
- //}}AFX_DATA_INIT
- }
-
- void CAboutDlg::DoDataExchange(CDataExchange* pDX)
- {
- CDialog::DoDataExchange(pDX);
- //{{AFX_DATA_MAP(CAboutDlg)
- //}}AFX_DATA_MAP
- }
-
- BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
- //{{AFX_MSG_MAP(CAboutDlg)
- // No message handlers
- //}}AFX_MSG_MAP
- END_MESSAGE_MAP()
-
- /////////////////////////////////////////////////////////////////////////////
- // CMemoryEncryptDlg dialog
-
- CMemoryEncryptDlg::CMemoryEncryptDlg(CWnd* pParent /*=NULL*/)
- : CDialog(CMemoryEncryptDlg::IDD, pParent)
- {
- //{{AFX_DATA_INIT(CMemoryEncryptDlg)
- m_sDecryptedText = _T("");
- m_sEncryptedText = _T("");
- m_sPassPhrase = _T("");
- m_nAsymmetricEncryption = -1;
- m_nSymmetricEncryption = -1;
- m_sPrivateKeyFile = _T("");
- m_sPublicKeyFile = _T("");
- //}}AFX_DATA_INIT
- // Note that LoadIcon does not require a subsequent DestroyIcon in Win32
- m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
- }
-
- void CMemoryEncryptDlg::DoDataExchange(CDataExchange* pDX)
- {
- CDialog::DoDataExchange(pDX);
- //{{AFX_DATA_MAP(CMemoryEncryptDlg)
- DDX_Text(pDX, IDC_TXT_DECRYPTEDTEXT, m_sDecryptedText);
- DDX_Text(pDX, IDC_TXT_ENCRYPTEDTEXT, m_sEncryptedText);
- DDX_Text(pDX, IDC_TXT_PASSPHRASE, m_sPassPhrase);
- DDX_Radio(pDX, IDC_RDO_ASYMMETRICENCRYPTION, m_nAsymmetricEncryption);
- DDX_Radio(pDX, IDC_RDO_SYMMETRICENCRYPTION, m_nSymmetricEncryption);
- DDX_Text(pDX, IDC_TXT_PRIVATEKEYFILE, m_sPrivateKeyFile);
- DDX_Text(pDX, IDC_TXT_PUBLICKEYFILE, m_sPublicKeyFile);
- //}}AFX_DATA_MAP
- }
-
- BEGIN_MESSAGE_MAP(CMemoryEncryptDlg, CDialog)
- //{{AFX_MSG_MAP(CMemoryEncryptDlg)
- ON_WM_SYSCOMMAND()
- ON_WM_PAINT()
- ON_WM_QUERYDRAGICON()
- ON_BN_CLICKED(IDC_CMD_ENCRYPT, OnCmdEncrypt)
- ON_BN_CLICKED(IDQUIT, OnQuit)
- ON_EN_SETFOCUS(IDC_TXT_DECRYPTEDTEXT, OnSetfocusTxtDecryptedtext)
- ON_EN_SETFOCUS(IDC_TXT_ENCRYPTEDTEXT, OnSetfocusTxtEncryptedtext)
- ON_BN_CLICKED(IDC_CMD_DECRYPT, OnCmdDecrypt)
- ON_BN_CLICKED(IDC_CMD_OPTION, OnCmdOption)
- ON_BN_CLICKED(IDC_CMD_RANDOMKEYPAIR, OnCmdRandomkeypair)
- ON_BN_CLICKED(IDC_CMD_SELPRIVATEKEYFILE, OnCmdSelprivatekeyfile)
- ON_BN_CLICKED(IDC_CMD_SELPUBLICKEYFILE, OnCmdSelpublickeyfile)
- ON_BN_CLICKED(IDC_RDO_ASYMMETRICENCRYPTION, OnRdoAsymmetricencryption)
- ON_BN_CLICKED(IDC_RDO_SYMMETRICENCRYPTION, OnRdoSymmetricencryption)
- ON_EN_KILLFOCUS(IDC_TXT_PRIVATEKEYFILE, OnKillfocusTxtPrivatekeyfile)
- ON_EN_KILLFOCUS(IDC_TXT_PUBLICKEYFILE, OnKillfocusTxtPublickeyfile)
- //}}AFX_MSG_MAP
- END_MESSAGE_MAP()
-
- /////////////////////////////////////////////////////////////////////////////
- // CMemoryEncryptDlg message handlers
-
- BOOL CMemoryEncryptDlg::OnInitDialog()
- {
- //
- // Set default values for the text to Encrypt and the MaxLineLength text box
- //
- m_sDecryptedText = "This is a little test to show you how the memory Encryption works. \r\n"
- "And it is very easy to use.";
-
- LoadOption();
- EnableControls();
-
- CDialog::OnInitDialog();
-
- // Add "About..." menu item to system menu.
-
- // IDM_ABOUTBOX must be in the system command range.
- ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
- ASSERT(IDM_ABOUTBOX < 0xF000);
-
- CMenu* pSysMenu = GetSystemMenu(FALSE);
- if (pSysMenu != NULL)
- {
- CString strAboutMenu;
- strAboutMenu.LoadString(IDS_ABOUTBOX);
- if (!strAboutMenu.IsEmpty())
- {
- pSysMenu->AppendMenu(MF_SEPARATOR);
- pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
- }
- }
-
- // Set the icon for this dialog. The framework does this automatically
- // when the application's main window is not a dialog
- SetIcon(m_hIcon, TRUE); // Set big icon
- SetIcon(m_hIcon, FALSE); // Set small icon
-
- return TRUE; // return TRUE unless you set the focus to a control
- }
-
- void CMemoryEncryptDlg::OnSysCommand(UINT nID, LPARAM lParam)
- {
- if ((nID & 0xFFF0) == IDM_ABOUTBOX)
- {
- CAboutDlg dlgAbout;
- dlgAbout.DoModal();
- }
- else
- {
- CDialog::OnSysCommand(nID, lParam);
- }
- }
-
- // If you add a minimize button to your dialog, you will need the code below
- // to draw the icon. For MFC applications using the document/view model,
- // this is automatically done for you by the framework.
-
- void CMemoryEncryptDlg::OnPaint()
- {
- if (IsIconic())
- {
- CPaintDC dc(this); // device context for painting
-
- SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
-
- // Center icon in client rectangle
- int cxIcon = GetSystemMetrics(SM_CXICON);
- int cyIcon = GetSystemMetrics(SM_CYICON);
- CRect rect;
- GetClientRect(&rect);
- int x = (rect.Width() - cxIcon + 1) / 2;
- int y = (rect.Height() - cyIcon + 1) / 2;
-
- // Draw the icon
- dc.DrawIcon(x, y, m_hIcon);
- }
- else
- {
- CDialog::OnPaint();
- }
- }
-
- // The system calls this to obtain the cursor to display while the user drags
- // the minimized window.
- HCURSOR CMemoryEncryptDlg::OnQueryDragIcon()
- {
- return (HCURSOR) m_hIcon;
- }
-
- //====================================================================================
- //
- // EVENTS - triggered by the form and its controls
- //
- //====================================================================================
-
- void CMemoryEncryptDlg::OnSetfocusTxtDecryptedtext()
- {
- SetDefID( IDC_CMD_ENCRYPT );
- }
-
- void CMemoryEncryptDlg::OnSetfocusTxtEncryptedtext()
- {
- SetDefID( IDC_CMD_DECRYPT );
- }
-
- //------------------------------------------------------------------------------------
- // Quit the sample application
- //------------------------------------------------------------------------------------
- void CMemoryEncryptDlg::OnQuit()
- {
- EndDialog( IDOK );
- }
-
- //------------------------------------------------------------------------------------
- // Do the Encryption of the Decrypted text
- //------------------------------------------------------------------------------------
- void CMemoryEncryptDlg::OnCmdEncrypt()
- {
- if( UpdateData( TRUE ) )
- {
- CWaitCursor xWaitCursor;
-
- COleException* pExcept = new COleException;
- DXceedEncryption xEncryptor;
- COleVariant vaEncrypted;
- COleVariant vaToEncrypt;
-
- try
- {
- // Create an instance of the XceedEncryption class
- if( !xEncryptor.CreateDispatch( "Xceed.Encryption", pExcept ) )
- {
- throw pExcept;
- }
-
- // Create and prepare the Encryption Method
- // This function can throw an exception that will be caught here.
- PrepareEncryptionMethod( xEncryptor );
-
- // Store in the variant to Encrypt the content of the text box
- // as an ANSI String. We could have used a BSTR representation
- // of the string but it would have unnecessarily double the string size.
- // The Variant will report containing a VT_BSTR, but the actual data and
- // size will reflect the Ansi string format.
- vaToEncrypt.SetString( m_sDecryptedText, VT_BSTRT );
-
- // Encrypt the string in a single call (bEndOfData is TRUE)
- vaEncrypted = xEncryptor.Encrypt( &vaToEncrypt, TRUE );
-
- // We assign an empty string as a default Encrypted value.
- m_sEncryptedText = "";
-
- // We check that the Encrypted variant is really a byte array. For instance,
- // it won't be if there was no characters to Encrypt (empty string).
- if( vaEncrypted.vt == ( VT_ARRAY | VT_UI1 ) )
- {
- // Encode the encrypted variant in hexadecimal representation.
- m_sEncryptedText = BinaryToHex( &vaEncrypted );
- }
-
- // Update mainly the Encrypted text box
- UpdateData( FALSE );
- }
- catch( COleDispatchException* pEx )
- {
- pEx->ReportError();
- pEx->Delete();
- }
- catch( COleException* pEx )
- {
- pEx->ReportError();
- pEx->Delete();
- }
- catch( CException* pEx )
- {
- pEx->ReportError();
- pEx->Delete();
- }
-
- pExcept->Delete();
- }
- }
-
- //------------------------------------------------------------------------------------
- // Do the Decryption of the Encrypted text
- //------------------------------------------------------------------------------------
- void CMemoryEncryptDlg::OnCmdDecrypt()
- {
- if( UpdateData( TRUE ) )
- {
- CWaitCursor xWaitCursor;
-
- COleException* pEx = new COleException;
- DXceedEncryption xEncryptor;
- COleVariant vaEncrypted;
- COleVariant vaDecrypted;
-
- try
- {
- // Create an instance of the XceedEncryption class
- if( !xEncryptor.CreateDispatch( "Xceed.Encryption", pEx ) )
- {
- throw pEx;
- }
-
- // Create and prepare the Encryption Method
- // This function can throw an exception that will be caught here.
- PrepareEncryptionMethod( xEncryptor );
-
- // Store in the variant to Decrypt the content of the decrypted text box,
- // which is in hexadecimal representation, converted to binary form.
- vaEncrypted = HexToBinary( m_sEncryptedText );
-
- // Decrypt the string in a single call (bEndOfData is TRUE).
- vaDecrypted = xEncryptor.Decrypt( vaEncrypted, TRUE );
-
- // We assign an empty string as a default Decrypted value.
- m_sDecryptedText = "";
-
- // We check that the Decrypted variant is really a byte array. For instance,
- // it won't be if there was no characters to Decrypt (empty string).
- if( vaDecrypted.vt == ( VT_ARRAY | VT_UI1 ) )
- {
- LONG lHigh;
- LONG lLow;
-
- // We extracts the upper bound and the lower bound of the
- // byte array.
- SafeArrayGetUBound( vaDecrypted.parray, 1, &lHigh );
- SafeArrayGetLBound( vaDecrypted.parray, 1, &lLow );
-
- // This pointer will contain the address of the byte array
- char* pszDecrypted = NULL;
-
- // We get a pointer on the actual data in the safe array.
- SafeArrayAccessData( vaDecrypted.parray, (void**)&pszDecrypted );
-
- // We display the Decrypted string by assigning this Ansi string
- // to the CString member, using the constructor accepting a char* and a size.
- m_sDecryptedText = CString( pszDecrypted, lHigh - lLow + 1 );
-
- // We release the lock on the safe array
- SafeArrayUnaccessData( vaDecrypted.parray );
- }
-
- // Update mainly the Decrypted text box
- UpdateData( FALSE );
- }
- catch( COleDispatchException* pExcept )
- {
- pExcept->ReportError();
- pExcept->Delete();
- }
- catch( COleException* pExcept )
- {
- pExcept->ReportError();
- pExcept->Delete();
- }
- catch( CException* pExcept )
- {
- pExcept->ReportError();
- pExcept->Delete();
- }
-
- pEx->Delete();
- }
- }
-
- //------------------------------------------------------------------------------------
- // Display the options form, saving them if the user click OK
- //------------------------------------------------------------------------------------
- void CMemoryEncryptDlg::OnCmdOption()
- {
- COptionDlg dlgOption;
-
- if( dlgOption.ShowForm( &m_eEncryptionMethod, &m_eEncryptionMode, &m_eHashingMethod,
- &m_nSecretKeySize, &m_ePaddingMethod ) )
- {
- SaveOption();
- }
- }
-
- //------------------------------------------------------------------------------------
- // Generate new private and public keys stored in the file specified in the
- // respective text boxes.
- //------------------------------------------------------------------------------------
- void CMemoryEncryptDlg::OnCmdRandomkeypair()
- {
- CKeyPair dlgKeyPair;
-
- if( UpdateData( TRUE ) )
- {
- if( m_sPrivateKeyFile.IsEmpty() || m_sPublicKeyFile.IsEmpty() )
- {
- AfxMessageBox( "You must specify the key file names where will be stored the private and public keys" );
- }
- else
- {
- dlgKeyPair.ShowForm( m_sPrivateKeyFile, m_sPublicKeyFile );
- }
- }
- }
-
- //------------------------------------------------------------------------------------
- // Select the file name that contain (or will contain) the private key.
- //------------------------------------------------------------------------------------
- void CMemoryEncryptDlg::OnCmdSelprivatekeyfile()
- {
- CString sKeyFile;
-
- sKeyFile = SelectKeyFile();
- if( !sKeyFile.IsEmpty() )
- {
- m_sPrivateKeyFile = sKeyFile;
- UpdateData( FALSE );
- SavePrivateKeyFileSetting();
- }
- }
-
- //------------------------------------------------------------------------------------
- // Select the file name that contain (or will contain) the public key.
- //------------------------------------------------------------------------------------
- void CMemoryEncryptDlg::OnCmdSelpublickeyfile()
- {
- CString sKeyFile;
-
- sKeyFile = SelectKeyFile();
- if( !sKeyFile.IsEmpty() )
- {
- m_sPublicKeyFile = sKeyFile;
- UpdateData( FALSE );
- SavePublicKeyFileSetting();
- }
- }
-
- void CMemoryEncryptDlg::OnKillfocusTxtPrivatekeyfile()
- {
- SavePrivateKeyFileSetting();
- }
-
- void CMemoryEncryptDlg::OnKillfocusTxtPublickeyfile()
- {
- SavePublicKeyFileSetting();
- }
-
- //------------------------------------------------------------------------------------
- // The user selected asymmetric mode, disable controls associated with symmetric
- // encryption
- //------------------------------------------------------------------------------------
- void CMemoryEncryptDlg::OnRdoAsymmetricencryption()
- {
- if( UpdateData( TRUE ) )
- {
- m_nSymmetricEncryption = -1;
-
- UpdateData( FALSE );
-
- EnableControls();
- SaveEncryptionType();
- }
- }
-
- //------------------------------------------------------------------------------------
- // The user selected symmetric mode, disable controls associated with asymmetric
- // encryption
- //------------------------------------------------------------------------------------
- void CMemoryEncryptDlg::OnRdoSymmetricencryption()
- {
- if( UpdateData( TRUE ) )
- {
- m_nAsymmetricEncryption = -1;
-
- UpdateData( FALSE );
-
- EnableControls();
- SaveEncryptionType();
- }
- }
-
- //====================================================================================
- //
- // FUNCTIONS
- //
- //====================================================================================
-
- //------------------------------------------------------------------------------------
- // Enable the control associated with the selected encryption type (symmetric or
- // asymmetric). Disable the other controls.
- //------------------------------------------------------------------------------------
- void CMemoryEncryptDlg::EnableControls()
- {
- BOOL bSecretKeyEncryption = ( m_nSymmetricEncryption > -1 );
-
- GetDlgItem( IDC_TXT_PASSPHRASE )->EnableWindow( bSecretKeyEncryption );
- GetDlgItem( IDC_CMD_OPTION )->EnableWindow( bSecretKeyEncryption );
-
- GetDlgItem( IDC_TXT_PRIVATEKEYFILE )->EnableWindow( !bSecretKeyEncryption );
- GetDlgItem( IDC_CMD_SELPRIVATEKEYFILE )->EnableWindow( !bSecretKeyEncryption );
- GetDlgItem( IDC_TXT_PUBLICKEYFILE )->EnableWindow( !bSecretKeyEncryption );
- GetDlgItem( IDC_CMD_SELPUBLICKEYFILE )->EnableWindow( !bSecretKeyEncryption );
- GetDlgItem( IDC_CMD_RANDOMKEYPAIR )->EnableWindow( !bSecretKeyEncryption );
- }
-
- //------------------------------------------------------------------------------------
- // Load in the member variables the options saved in the registry the last
- // time this sample file manager was called.
- //------------------------------------------------------------------------------------
- void CMemoryEncryptDlg::LoadOption()
- {
- m_eEncryptionMethod = ( enuEncryptionMethod )AfxGetApp()->GetProfileInt( "Encryption", "EncryptionMethod", eemRijndael );
- m_eEncryptionMode = ( enuEncryptionMode )AfxGetApp()->GetProfileInt( "Encryption", "EncryptionMode", emoFreeBlocks );
- m_ePaddingMethod = ( enuPaddingMethod )AfxGetApp()->GetProfileInt( "Encryption", "PaddingMethod", epmFIPS81 );
- m_eHashingMethod = ( enuHashingMethod )AfxGetApp()->GetProfileInt( "Encryption", "HashingMethod", ehmHaval );
- m_nSecretKeySize = AfxGetApp()->GetProfileInt( "Encryption", "SecretKeySize", 128 );
- m_sPrivateKeyFile = AfxGetApp()->GetProfileString( "Encryption", "PrivateKeyFile", "");
- m_sPublicKeyFile = AfxGetApp()->GetProfileString( "Encryption", "PublicKeyFile", "");
- m_nAsymmetricEncryption = AfxGetApp()->GetProfileInt( "Encryption", "Asymmetric", -1 );
- m_nSymmetricEncryption = AfxGetApp()->GetProfileInt( "Encryption", "Symmetric", 0 );
-
- UpdateData( FALSE );
- }
-
- //------------------------------------------------------------------------------------
- // Prepare the Encryption Method according to the user selection
- //------------------------------------------------------------------------------------
- void CMemoryEncryptDlg::PrepareEncryptionMethod( DXceedEncryption& xEncryptor )
- {
- if( UpdateData( TRUE ) )
- {
- COleException* pEx = new COleException;
-
- if( m_nSymmetricEncryption > -1 )
- {
- // The user chose to perform symmetric encryption/decryption
-
- switch( m_eEncryptionMethod )
- {
- //
- // The user chose RIJNDAEL
- //
- case eemRijndael :
- {
- // Instanciate the Rijndael encryption method
- DXceedRijndaelEncryptionMethod xRijndael;
-
- if( !xRijndael.CreateDispatch( "Xceed.RijndaelEncryptionMethod", pEx ) )
- {
- throw pEx;
- }
-
- // Set the hashing method that will be used to set the key from
- // the pass phrase.
- switch( m_eHashingMethod )
- {
- case ehmHaval :
- {
- DXceedHavalHashingMethod xHaval;
- if( !xHaval.CreateDispatch( "Xceed.HavalHashingMethod", pEx ) )
- {
- throw pEx;
- }
- // Haval supports hash sizes equivalent to the supported
- // key size. So, we can assign the latter to the
- // former without problem.
- xHaval.SetHashSize( m_nSecretKeySize );
- xRijndael.SetRefHashingMethod( xHaval.m_lpDispatch );
- }
- break;
-
- case ehmSHA :
- {
- DXceedSHAHashingMethod xSHA;
- if( !xSHA.CreateDispatch( "Xceed.SHAHashingMethod", pEx ) )
- {
- throw pEx;
- }
- // We arbitrarily set the HashSize to the maximum key
- // size allowed so we don't have to worry that the hash
- // result of the pass phrase could be shorter than the
- // expected key (although the Xceed Encryption Library
- // would have deal with it).
- xSHA.SetHashSize( 256 );
- xRijndael.SetRefHashingMethod( xSHA.m_lpDispatch );
- }
- break;
- }
-
- // Set the secret key of the desired size using the user pass phrase
- xRijndael.SetSecretKeyFromPassPhrase( m_sPassPhrase, m_nSecretKeySize );
-
- // Set the encryption mode
- xRijndael.SetEncryptionMode( m_eEncryptionMode );
-
- // Set the padding method (for the last encrypted or decrypted
- // block)
- xRijndael.SetPaddingMethod( m_ePaddingMethod );
-
- if( m_eEncryptionMode == emoChainedBlocks )
- {
- // Will be useful only when encrypting since, in decryption,
- // the InitVector is read at the beginning of the encrypted text.
- xRijndael.SetRandomInitVector();
- }
-
- // Set the previously initialized Encryption Method of the Encryptor object
- // received as a parameter of this function.
- xEncryptor.SetRefEncryptionMethod( xRijndael.m_lpDispatch );
-
- // Free the temporary Encryption Method. The previous assignation adding
- // a reference to the Encryption Method object, this object will effectively
- // be freed by the xEncryptr object when the latter will be released.
- break;
- }
-
- //
- // The user chose TWOFISH
- //
- case eemTwofish :
- {
- // Instanciate the Twofish encryption method
- DXceedTwofishEncryptionMethod xTwofish;
-
- if( !xTwofish.CreateDispatch( "Xceed.TwofishEncryptionMethod", pEx ) )
- {
- throw pEx;
- }
-
- // Set the hashing method that will be used to set the key from
- // the pass phrase.
- switch( m_eHashingMethod )
- {
- case ehmHaval :
- {
- DXceedHavalHashingMethod xHaval;
- if( !xHaval.CreateDispatch( "Xceed.HavalHashingMethod", pEx ) )
- {
- throw pEx;
- }
- // Haval supports hash sizes equivalent to the supported
- // key size. So, we can assign the latter to the
- // former without problem.
- xHaval.SetHashSize( m_nSecretKeySize );
- xTwofish.SetRefHashingMethod( xHaval.m_lpDispatch );
- }
-
- case ehmSHA :
- {
- DXceedSHAHashingMethod xSHA;
- if( !xSHA.CreateDispatch( "Xceed.SHAHashingMethod", pEx ) )
- {
- throw pEx;
- }
- // We arbitrarily set the HashSize to the maximum key
- // size allowed so we don't have to worry that the hash
- // result of the pass phrase could be shorter than the
- // expected key (although the Xceed Encryption Library
- // would have deal with it).
- xSHA.SetHashSize( 256 );
- xTwofish.SetRefHashingMethod( xSHA.m_lpDispatch );
- }
- }
-
- // Set the secret key of the desired size using the user pass phrase
- xTwofish.SetSecretKeyFromPassPhrase( m_sPassPhrase, m_nSecretKeySize );
-
- // Set the encryption mode
- xTwofish.SetEncryptionMode( m_eEncryptionMode );
-
- // Set the padding method (for the last encrypted or decrypted
- // block)
- xTwofish.SetPaddingMethod( m_ePaddingMethod );
-
- if( m_eEncryptionMode == emoChainedBlocks )
- {
- // Will be useful only when encrypting since, in decryption,
- // the InitVector is read at the beginning of the encrypted text.
- xTwofish.SetRandomInitVector();
- }
-
- // Set the previously initialized Encryption Method of the Encryptor object
- // received as a parameter of this function.
- xEncryptor.SetRefEncryptionMethod( xTwofish.m_lpDispatch );
-
- // Free the temporary Encryption Method. The previous assignation adding
- // a reference to the Encryption Method object, this object will effectively
- // be freed by the xEncryptor object when the latter will be released.
- break;
- }
- }
- }
- else
- {
- // The user chose to perform asymmetric encryption/decryption
-
- if( m_nAsymmetricEncryption > -1 )
- {
- // Instanciate the RSA encryption method
- DXceedRSAEncryptionMethod xRSA;
-
- if( !xRSA.CreateDispatch( "Xceed.RSAEncryptionMethod", pEx ) )
- {
- throw pEx;
- }
-
- COleVariant vaPrivateKey;
- COleVariant vaPublicKey;
-
- // Initialize the private key (used when decrypting)
- ReadKeyFile( m_sPrivateKeyFile, &xEncryptor, &vaPrivateKey );
- xRSA.SetPrivateKey( &vaPrivateKey );
-
- // Initialize the public key (used when encrypting)
- ReadKeyFile( m_sPublicKeyFile, &xEncryptor, &vaPublicKey );
- xRSA.SetPublicKey( &vaPublicKey );
-
- xEncryptor.SetRefEncryptionMethod( xRSA.m_lpDispatch );
- }
- }
-
- pEx->Delete();
- }
- }
-
- //------------------------------------------------------------------------------------
- // Read the content of the specified file, allegedly containing a private or public
- // key in hexadecimal representation.
- // Return the key in the pvaKey parameter. This function can return Empty.
- //------------------------------------------------------------------------------------
- void CMemoryEncryptDlg::ReadKeyFile( LPCSTR sKeyFileName,
- DXceedEncryption* pxEncryptor,
- COleVariant* pvaKey )
- {
- pvaKey->Clear();
-
- CStdioFile xKeyFile;
-
- if( xKeyFile.Open( sKeyFileName, CFile::modeRead ) )
- {
- CString sBuffer;
- CString sTemp;
-
- sBuffer = "";
- while( xKeyFile.ReadString( sTemp ) )
- {
- sTemp.TrimRight();
- sBuffer += sTemp;
- }
-
- if( !sBuffer.IsEmpty() )
- {
- *pvaKey = HexToBinary( sBuffer );
- }
- }
- else
- {
- AfxMessageBox( "Error opening key file" );
- }
- }
-
- //------------------------------------------------------------------------------------
- // Save the current options from the member variables in the registry
- //------------------------------------------------------------------------------------
- void CMemoryEncryptDlg::SaveOption()
- {
- AfxGetApp()->WriteProfileInt( "Encryption", "EncryptionMethod", m_eEncryptionMethod );
- AfxGetApp()->WriteProfileInt( "Encryption", "EncryptionMode", m_eEncryptionMode );
- AfxGetApp()->WriteProfileInt( "Encryption", "PaddingMethod", m_ePaddingMethod );
- AfxGetApp()->WriteProfileInt( "Encryption", "HashingMethod", m_eHashingMethod );
- AfxGetApp()->WriteProfileInt( "Encryption", "SecretKeySize", m_nSecretKeySize );
- }
-
- //------------------------------------------------------------------------------------
- // Save the current selected (and deselected) encryption type (symmetric or asymmetric)
- //------------------------------------------------------------------------------------
- void CMemoryEncryptDlg::SaveEncryptionType()
- {
- if( UpdateData( TRUE ) )
- {
- AfxGetApp()->WriteProfileInt( "Encryption", "Asymmetric", m_nAsymmetricEncryption );
- AfxGetApp()->WriteProfileInt( "Encryption", "Symmetric", m_nSymmetricEncryption );
- }
- }
-
- //------------------------------------------------------------------------------------
- // Save the current private key file name
- //------------------------------------------------------------------------------------
- void CMemoryEncryptDlg::SavePrivateKeyFileSetting()
- {
- if( UpdateData( TRUE ) )
- {
- AfxGetApp()->WriteProfileString( "Encryption", "PrivateKeyFile", m_sPrivateKeyFile );
- }
- }
-
- //------------------------------------------------------------------------------------
- // Save the current public key file name
- //------------------------------------------------------------------------------------
- void CMemoryEncryptDlg::SavePublicKeyFileSetting()
- {
- if( UpdateData( TRUE ) )
- {
- AfxGetApp()->WriteProfileString( "Encryption", "PublicKeyFile", m_sPublicKeyFile );
- }
- }
-
- CString CMemoryEncryptDlg::SelectKeyFile()
- {
- CString szFileSelected;
- CFileDialog dlgOpen( TRUE, "key", NULL, 0, "Key file (*.key)|*.key|All type (*.*)|*.*||", this );
-
- if( dlgOpen.DoModal() == IDOK )
- {
- szFileSelected = dlgOpen.GetPathName();
- }
-
- return szFileSelected;
- }
-