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

  1. // Xceed Encryption Library - Memory Encrypt sample
  2. // Copyright (c) 2001 Xceed Software Inc.
  3. //
  4. // [KeyPair.cpp]
  5. //
  6. // This form module displays properties for generating a private and a
  7. // public key pair. It specifically demonstrates:
  8. //  - The SetRandomKeyPair method.
  9. //
  10. // This file is part of the Xceed Encryption Library sample applications.
  11. // The source code in this file is only intended as a supplement to Xceed
  12. // Encryption Library's documentation, and is provided "as is", without
  13. // warranty of any kind, either expressed or implied.
  14.  
  15.  
  16. #include "stdafx.h"
  17. #include "memoryencrypt.h"
  18. #include "KeyPair.h"
  19.  
  20. #include "xceedcry.h"
  21.  
  22. #ifdef _DEBUG
  23. #define new DEBUG_NEW
  24. #undef THIS_FILE
  25. static char THIS_FILE[] = __FILE__;
  26. #endif
  27.  
  28. /////////////////////////////////////////////////////////////////////////////
  29. // CKeyPair dialog
  30.  
  31.  
  32. CKeyPair::CKeyPair(CWnd* pParent /*=NULL*/)
  33.     : CDialog(CKeyPair::IDD, pParent)
  34. {
  35.     //{{AFX_DATA_INIT(CKeyPair)
  36.     m_sKeySize = _T("");
  37.     m_sRandomSeed = _T("");
  38.     //}}AFX_DATA_INIT
  39. }
  40.  
  41.  
  42. void CKeyPair::DoDataExchange(CDataExchange* pDX)
  43. {
  44.     CDialog::DoDataExchange(pDX);
  45.     //{{AFX_DATA_MAP(CKeyPair)
  46.     DDX_Text(pDX, IDC_TXT_KEYSIZE, m_sKeySize);
  47.     DDV_MaxChars(pDX, m_sKeySize, 4);
  48.     DDX_Text(pDX, IDC_TXT_RANDOMSEED, m_sRandomSeed);
  49.     //}}AFX_DATA_MAP
  50. }
  51.  
  52.  
  53. BEGIN_MESSAGE_MAP(CKeyPair, CDialog)
  54.     //{{AFX_MSG_MAP(CKeyPair)
  55.         // NOTE: the ClassWizard will add message map macros here
  56.     //}}AFX_MSG_MAP
  57. END_MESSAGE_MAP()
  58.  
  59. //------------------------------------------------------------------------------------
  60. // Display the key pair generator form.
  61. //------------------------------------------------------------------------------------
  62. void CKeyPair::ShowForm( LPCSTR sPrivateKeyFile,
  63.                          LPCSTR sPublicKeyFile )
  64. {
  65.   m_sKeySize = "1024";
  66.  
  67.   if( DoModal() == IDOK )
  68.   {
  69.     GenerateKeyPair( sPrivateKeyFile, sPublicKeyFile );
  70.   }
  71. }
  72.  
  73. /////////////////////////////////////////////////////////////////////////////
  74. // CKeyPair message handlers
  75.  
  76. //------------------------------------------------------------------------------------
  77. // Do the key pair generation
  78. //------------------------------------------------------------------------------------
  79. void CKeyPair::GenerateKeyPair( LPCSTR sPrivateKeyFile, 
  80.                                 LPCSTR sPublicKeyFile )
  81. {
  82.   DXceedRSAEncryptionMethod xRSA;
  83.   DXceedEncryption xEncryptor;
  84.  
  85.   COleException* pEx = new COleException;
  86.  
  87.   try
  88.   {
  89.     // Create an instance of the XceedRSA Encryption Method
  90.     if( !xRSA.CreateDispatch( "Xceed.RSAEncryptionMethod", pEx ) )
  91.     {
  92.       throw( pEx );
  93.     }
  94.  
  95.     // Create an instance of the Xceed Encryption
  96.     if( !xEncryptor.CreateDispatch( "Xceed.Encryption", pEx ) )
  97.     {
  98.       throw( pEx );
  99.     }
  100.  
  101.     COleVariant vaSeed;
  102.  
  103.     if( !m_sRandomSeed.IsEmpty() )
  104.     {
  105.       // The user specified a seed value for the pseudo-random generator
  106.       // used in the key pair generation. We use it.
  107.       vaSeed = COleVariant( m_sRandomSeed );
  108.     }
  109.  
  110.     // Generate a new key pair Specifying:
  111.     //   - The wanted key size entered by the user.
  112.     //   - The seed to the pseudo-random generator. If the seed is Empty,
  113.     //     the RSA object will use its own random seed generator.
  114.     xRSA.SetRandomKeyPair( atoi( m_sKeySize ), &vaSeed );
  115.     
  116.     // Save the hexadecimal representation of the Private key
  117.     // generated above, in the file specified.
  118.     CFile xKeyFile;
  119.  
  120.     if( xKeyFile.Open( sPrivateKeyFile, CFile::modeWrite ) )
  121.     {
  122.       COleVariant vaKey;
  123.       CString sKey;
  124.  
  125.       vaKey.Attach( xRSA.GetPrivateKey() );
  126.       sKey = BinaryToHex( vaKey );
  127.       xKeyFile.Write( (LPCSTR) sKey, sKey.GetLength() );
  128.       xKeyFile.Close();
  129.     }
  130.     else
  131.     {
  132.       AfxMessageBox( "Error opening file " + CString( sPrivateKeyFile ) );
  133.     }
  134.  
  135.     // Save the hexadecimal representation of the Public key
  136.     // generated above, in the file specified.
  137.     if( xKeyFile.Open( sPublicKeyFile, CFile::modeWrite ) )
  138.     {
  139.       COleVariant vaKey;
  140.       CString sKey;
  141.  
  142.       vaKey.Attach( xRSA.GetPublicKey() );
  143.       sKey = BinaryToHex( &vaKey );
  144.       xKeyFile.Write( (LPCSTR) sKey, sKey.GetLength() );
  145.       xKeyFile.Close();
  146.     }
  147.     else
  148.     {
  149.       AfxMessageBox( "Error opening file " + CString( sPublicKeyFile ) );
  150.     }
  151.   }
  152.   catch( COleDispatchException* pExcept )
  153.   {
  154.     pExcept->ReportError();
  155.     pExcept->Delete();
  156.   }
  157.   catch( COleException* pExcept )
  158.   {
  159.     pExcept->ReportError();
  160.     pExcept->Delete();
  161.   }
  162.   catch( CException* pExcept )
  163.   {
  164.     pExcept->ReportError();
  165.     pExcept->Delete();
  166.   }
  167.  
  168.   pEx->Delete();
  169. }
  170.