home *** CD-ROM | disk | FTP | other *** search
- // Xceed Encryption Library - Memory Encrypt sample
- // Copyright (c) 2001 Xceed Software Inc.
- //
- // [KeyPair.cpp]
- //
- // This form module displays properties for generating a private and a
- // public key pair. It specifically demonstrates:
- // - The SetRandomKeyPair method.
- //
- // 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 "KeyPair.h"
-
- #include "xceedcry.h"
-
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
-
- /////////////////////////////////////////////////////////////////////////////
- // CKeyPair dialog
-
-
- CKeyPair::CKeyPair(CWnd* pParent /*=NULL*/)
- : CDialog(CKeyPair::IDD, pParent)
- {
- //{{AFX_DATA_INIT(CKeyPair)
- m_sKeySize = _T("");
- m_sRandomSeed = _T("");
- //}}AFX_DATA_INIT
- }
-
-
- void CKeyPair::DoDataExchange(CDataExchange* pDX)
- {
- CDialog::DoDataExchange(pDX);
- //{{AFX_DATA_MAP(CKeyPair)
- DDX_Text(pDX, IDC_TXT_KEYSIZE, m_sKeySize);
- DDV_MaxChars(pDX, m_sKeySize, 4);
- DDX_Text(pDX, IDC_TXT_RANDOMSEED, m_sRandomSeed);
- //}}AFX_DATA_MAP
- }
-
-
- BEGIN_MESSAGE_MAP(CKeyPair, CDialog)
- //{{AFX_MSG_MAP(CKeyPair)
- // NOTE: the ClassWizard will add message map macros here
- //}}AFX_MSG_MAP
- END_MESSAGE_MAP()
-
- //------------------------------------------------------------------------------------
- // Display the key pair generator form.
- //------------------------------------------------------------------------------------
- void CKeyPair::ShowForm( LPCSTR sPrivateKeyFile,
- LPCSTR sPublicKeyFile )
- {
- m_sKeySize = "1024";
-
- if( DoModal() == IDOK )
- {
- GenerateKeyPair( sPrivateKeyFile, sPublicKeyFile );
- }
- }
-
- /////////////////////////////////////////////////////////////////////////////
- // CKeyPair message handlers
-
- //------------------------------------------------------------------------------------
- // Do the key pair generation
- //------------------------------------------------------------------------------------
- void CKeyPair::GenerateKeyPair( LPCSTR sPrivateKeyFile,
- LPCSTR sPublicKeyFile )
- {
- DXceedRSAEncryptionMethod xRSA;
- DXceedEncryption xEncryptor;
-
- COleException* pEx = new COleException;
-
- try
- {
- // Create an instance of the XceedRSA Encryption Method
- if( !xRSA.CreateDispatch( "Xceed.RSAEncryptionMethod", pEx ) )
- {
- throw( pEx );
- }
-
- // Create an instance of the Xceed Encryption
- if( !xEncryptor.CreateDispatch( "Xceed.Encryption", pEx ) )
- {
- throw( pEx );
- }
-
- COleVariant vaSeed;
-
- if( !m_sRandomSeed.IsEmpty() )
- {
- // The user specified a seed value for the pseudo-random generator
- // used in the key pair generation. We use it.
- vaSeed = COleVariant( m_sRandomSeed );
- }
-
- // Generate a new key pair Specifying:
- // - The wanted key size entered by the user.
- // - The seed to the pseudo-random generator. If the seed is Empty,
- // the RSA object will use its own random seed generator.
- xRSA.SetRandomKeyPair( atoi( m_sKeySize ), &vaSeed );
-
- // Save the hexadecimal representation of the Private key
- // generated above, in the file specified.
- CFile xKeyFile;
-
- if( xKeyFile.Open( sPrivateKeyFile, CFile::modeWrite ) )
- {
- COleVariant vaKey;
- CString sKey;
-
- vaKey.Attach( xRSA.GetPrivateKey() );
- sKey = BinaryToHex( vaKey );
- xKeyFile.Write( (LPCSTR) sKey, sKey.GetLength() );
- xKeyFile.Close();
- }
- else
- {
- AfxMessageBox( "Error opening file " + CString( sPrivateKeyFile ) );
- }
-
- // Save the hexadecimal representation of the Public key
- // generated above, in the file specified.
- if( xKeyFile.Open( sPublicKeyFile, CFile::modeWrite ) )
- {
- COleVariant vaKey;
- CString sKey;
-
- vaKey.Attach( xRSA.GetPublicKey() );
- sKey = BinaryToHex( &vaKey );
- xKeyFile.Write( (LPCSTR) sKey, sKey.GetLength() );
- xKeyFile.Close();
- }
- else
- {
- AfxMessageBox( "Error opening file " + CString( sPublicKeyFile ) );
- }
- }
- catch( COleDispatchException* pExcept )
- {
- pExcept->ReportError();
- pExcept->Delete();
- }
- catch( COleException* pExcept )
- {
- pExcept->ReportError();
- pExcept->Delete();
- }
- catch( CException* pExcept )
- {
- pExcept->ReportError();
- pExcept->Delete();
- }
-
- pEx->Delete();
- }
-