home *** CD-ROM | disk | FTP | other *** search
/ Chip 2002 September / Chip_2002-09_cd1.bin / zkuste / vbasic / Data / Utils / XZipComp.exe / XceedEncryption.Cab / F112945_unKeyPair.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  2001-07-03  |  5.2 KB  |  173 lines

  1. {
  2.   Xceed Encryption Library - Memory Encryption Sample
  3.   Copyright (c) 2001 Xceed Software Inc
  4.  
  5.   [unKeyPair.pas]
  6.  
  7.   This unit allows the user to generate a new random key pair for RSA public
  8.   key encryption.
  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 supplment to the Xceed
  12.   Encryption Library's documentation and is provided "as is", without warranty
  13.   of any kind either expressed or implied.
  14. }
  15.  
  16. unit unKeyPair;
  17.  
  18. interface
  19.  
  20. uses
  21.   Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  22.   StdCtrls, XceedEncryptionLib_TLB;
  23.  
  24. type
  25.   TfrmKeyPair = class(TForm)
  26.     Label1        : TLabel;
  27.     Label2        : TLabel;
  28.     edtKeySize    : TEdit;
  29.     edtRandomSeed : TEdit;
  30.     btOk          : TButton;
  31.     btCancel      : TButton;
  32.     procedure btOkClick(Sender: TObject);
  33.     procedure btCancelClick(Sender: TObject);
  34.     procedure FormCreate(Sender: TObject);
  35.     procedure FormDestroy(Sender: TObject);
  36.   private
  37.     m_bOk : boolean;
  38.   public
  39.     function ShowForm( sPrivateKeyFile : string; sPublicKeyFile : string ) : boolean;
  40.     procedure GenerateKeyPair( sPrivateKeyFile : string; sPublicKeyFile : string );
  41.   end;
  42.  
  43. var
  44.   frmKeyPair: TfrmKeyPair;
  45.  
  46. implementation
  47.  
  48. uses
  49.   unUtility;
  50.  
  51. {$R *.DFM}
  52.  
  53. {***************************************************************}
  54. {                                                               }
  55. { FORM EVENTS                                                   }
  56. {                                                               }
  57. {***************************************************************}
  58.  
  59. {---------------------------------------------------------------}
  60. procedure TfrmKeyPair.btCancelClick(Sender: TObject);
  61. begin
  62.   Self.Hide();
  63.   Self.ModalResult := 1;
  64. end;
  65.  
  66. {---------------------------------------------------------------}
  67. procedure TfrmKeyPair.btOkClick(Sender: TObject);
  68. begin
  69.   if( StrToInt( edtKeySize.Text ) > 0 ) then
  70.   begin
  71.     m_bOk := true;
  72.     Self.Hide();
  73.   end
  74.   else
  75.     ShowMessage( 'Invalid key size!' );
  76.  
  77.   Self.ModalResult := 1;
  78. end;
  79.  
  80. {---------------------------------------------------------------}
  81. procedure TfrmKeyPair.FormCreate(Sender: TObject);
  82. begin
  83.   m_bOk := false;
  84. end;
  85.  
  86. {---------------------------------------------------------------}
  87. procedure TfrmKeyPair.FormDestroy(Sender: TObject);
  88. begin
  89.   btCancel.Click();
  90. end;
  91.  
  92. {***************************************************************}
  93. {                                                               }
  94. { FORM FUNCTIONS AND PROCEDURES                                 }
  95. {                                                               }
  96. {***************************************************************}
  97.  
  98. {---------------------------------------------------------------}
  99. { Do the key pair generation                                    }
  100. {---------------------------------------------------------------}
  101. procedure TfrmKeyPair.GenerateKeyPair( sPrivateKeyFile : string;
  102.                                        sPublicKeyFile : string );
  103. var
  104.   xRSA       : DXceedRSAEncryptionMethod;
  105.   xEncryptor : TXceedEncryption;
  106.   vaSeed     : OleVariant;
  107.   pcBuffer   : PChar;
  108.   nBufferLen : Cardinal;
  109.   pcData     : PChar;
  110.   xPrivate   : TextFile;
  111.   xPublic    : TextFile;
  112.   sPrivate   : String;
  113.   sPublic    : String;
  114. begin
  115.   try
  116.     //Create an instance of the Xceed RSA Encryption method
  117.     xRSA := CoXceedRSAEncryptionMethod.Create();
  118.  
  119.     //Create an instance of the Xceed Encryption Library
  120.     xEncryptor := TXceedEncryption.Create( self );
  121.  
  122.     //we set the seed value to empty by default
  123.     vaSeed := varEmpty;
  124.  
  125.     if( length( trim( edtRandomSeed.Text ) ) > 0 ) then
  126.       //The user specified a seed value for the pseudo-random generator
  127.       //used in the key pair generation. We use it.
  128.       vaSeed := edtRandomSeed.Text;
  129.  
  130.     //Generate the new key pair specifying:
  131.     //  - The wanted key size entered by the user
  132.     //  - The seed to the pseudo-random generator. If the seed is emtpy,
  133.     //    the RSA object will use it's own random seed generator.
  134.     xRSA.SetRandomKeyPair( StrToInt( edtKeySize.Text ), vaSeed );
  135.     BinaryToHex( xRSA.Get_PrivateKey, sPrivate );
  136.     BinaryToHex( xRSA.Get_PublicKey, sPublic );
  137.  
  138.     // write the private key to a file
  139.     AssignFile( xPrivate, sPrivateKeyFile );
  140.     Rewrite( xPrivate );
  141.     Write( xPrivate, sPrivate );
  142.     CloseFile( xPrivate );
  143.  
  144.     //write the public key to file
  145.     AssignFile( xPublic, sPublicKeyFile );
  146.     Rewrite( xPublic );
  147.     Write( xPublic, sPublic );
  148.     CloseFile( xPublic );
  149.  
  150.   except
  151.     on xErr : Exception do
  152.       ShowMessage( 'Error generating key pair : ' + xErr.Message );
  153.   end;
  154.   xEncryptor.Free();
  155. end;
  156.  
  157. {---------------------------------------------------------------}
  158. function TfrmKeyPair.ShowForm( sPrivateKeyFile : string; sPublicKeyFile : string ) : boolean;
  159. begin
  160.   //By default, a key size of 1024 is proposed
  161.   edtKeySize.Text := IntToStr( 1024 );
  162.   Self.ShowModal();
  163.  
  164.   if m_bOk then
  165.     //The user clicked on the OK button, generate a new key pair
  166.     GenerateKeyPair( sPrivateKeyFile, sPublicKeyFile );
  167.  
  168.   ShowForm := m_bOK;
  169.   Self.Close();
  170. end;
  171.  
  172. end.
  173.