home *** CD-ROM | disk | FTP | other *** search
- {
- Xceed Encryption Library - Memory Encryption Sample
- Copyright (c) 2001 Xceed Software Inc
-
- [unKeyPair.pas]
-
- This unit allows the user to generate a new random key pair for RSA public
- key encryption.
-
- This file is part of the Xceed Encryption Library sample applications.
- The source code in this file is only intended as a supplment to the Xceed
- Encryption Library's documentation and is provided "as is", without warranty
- of any kind either expressed or implied.
- }
-
- unit unKeyPair;
-
- interface
-
- uses
- Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
- StdCtrls, XceedEncryptionLib_TLB;
-
- type
- TfrmKeyPair = class(TForm)
- Label1 : TLabel;
- Label2 : TLabel;
- edtKeySize : TEdit;
- edtRandomSeed : TEdit;
- btOk : TButton;
- btCancel : TButton;
- procedure btOkClick(Sender: TObject);
- procedure btCancelClick(Sender: TObject);
- procedure FormCreate(Sender: TObject);
- procedure FormDestroy(Sender: TObject);
- private
- m_bOk : boolean;
- public
- function ShowForm( sPrivateKeyFile : string; sPublicKeyFile : string ) : boolean;
- procedure GenerateKeyPair( sPrivateKeyFile : string; sPublicKeyFile : string );
- end;
-
- var
- frmKeyPair: TfrmKeyPair;
-
- implementation
-
- uses
- unUtility;
-
- {$R *.DFM}
-
- {***************************************************************}
- { }
- { FORM EVENTS }
- { }
- {***************************************************************}
-
- {---------------------------------------------------------------}
- procedure TfrmKeyPair.btCancelClick(Sender: TObject);
- begin
- Self.Hide();
- Self.ModalResult := 1;
- end;
-
- {---------------------------------------------------------------}
- procedure TfrmKeyPair.btOkClick(Sender: TObject);
- begin
- if( StrToInt( edtKeySize.Text ) > 0 ) then
- begin
- m_bOk := true;
- Self.Hide();
- end
- else
- ShowMessage( 'Invalid key size!' );
-
- Self.ModalResult := 1;
- end;
-
- {---------------------------------------------------------------}
- procedure TfrmKeyPair.FormCreate(Sender: TObject);
- begin
- m_bOk := false;
- end;
-
- {---------------------------------------------------------------}
- procedure TfrmKeyPair.FormDestroy(Sender: TObject);
- begin
- btCancel.Click();
- end;
-
- {***************************************************************}
- { }
- { FORM FUNCTIONS AND PROCEDURES }
- { }
- {***************************************************************}
-
- {---------------------------------------------------------------}
- { Do the key pair generation }
- {---------------------------------------------------------------}
- procedure TfrmKeyPair.GenerateKeyPair( sPrivateKeyFile : string;
- sPublicKeyFile : string );
- var
- xRSA : DXceedRSAEncryptionMethod;
- xEncryptor : TXceedEncryption;
- vaSeed : OleVariant;
- pcBuffer : PChar;
- nBufferLen : Cardinal;
- pcData : PChar;
- xPrivate : TextFile;
- xPublic : TextFile;
- sPrivate : String;
- sPublic : String;
- begin
- try
- //Create an instance of the Xceed RSA Encryption method
- xRSA := CoXceedRSAEncryptionMethod.Create();
-
- //Create an instance of the Xceed Encryption Library
- xEncryptor := TXceedEncryption.Create( self );
-
- //we set the seed value to empty by default
- vaSeed := varEmpty;
-
- if( length( trim( edtRandomSeed.Text ) ) > 0 ) then
- //The user specified a seed value for the pseudo-random generator
- //used in the key pair generation. We use it.
- vaSeed := edtRandomSeed.Text;
-
- //Generate the new key pair specifying:
- // - The wanted key size entered by the user
- // - The seed to the pseudo-random generator. If the seed is emtpy,
- // the RSA object will use it's own random seed generator.
- xRSA.SetRandomKeyPair( StrToInt( edtKeySize.Text ), vaSeed );
- BinaryToHex( xRSA.Get_PrivateKey, sPrivate );
- BinaryToHex( xRSA.Get_PublicKey, sPublic );
-
- // write the private key to a file
- AssignFile( xPrivate, sPrivateKeyFile );
- Rewrite( xPrivate );
- Write( xPrivate, sPrivate );
- CloseFile( xPrivate );
-
- //write the public key to file
- AssignFile( xPublic, sPublicKeyFile );
- Rewrite( xPublic );
- Write( xPublic, sPublic );
- CloseFile( xPublic );
-
- except
- on xErr : Exception do
- ShowMessage( 'Error generating key pair : ' + xErr.Message );
- end;
- xEncryptor.Free();
- end;
-
- {---------------------------------------------------------------}
- function TfrmKeyPair.ShowForm( sPrivateKeyFile : string; sPublicKeyFile : string ) : boolean;
- begin
- //By default, a key size of 1024 is proposed
- edtKeySize.Text := IntToStr( 1024 );
- Self.ShowModal();
-
- if m_bOk then
- //The user clicked on the OK button, generate a new key pair
- GenerateKeyPair( sPrivateKeyFile, sPublicKeyFile );
-
- ShowForm := m_bOK;
- Self.Close();
- end;
-
- end.
-